Skip to content

Commit

Permalink
Don't write the full name if we don't have to
Browse files Browse the repository at this point in the history
  • Loading branch information
dmssargent committed Aug 13, 2018
1 parent 572044e commit 22837d0
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 131 deletions.
60 changes: 35 additions & 25 deletions src/main/java/davidsar/gent/stubjars/components/JarClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package davidsar.gent.stubjars.components;

import static davidsar.gent.stubjars.components.writer.Constants.EMPTY_STRING;
import static davidsar.gent.stubjars.components.writer.Constants.SPACE;

import davidsar.gent.stubjars.Utils;
import davidsar.gent.stubjars.components.expressions.ClassHeaderExpression;
Expand Down Expand Up @@ -130,16 +129,27 @@ static boolean hasSafeName(@NotNull Class<?> clazz) {
return !clazz.isSynthetic() && !clazz.isAnonymousClass();
}

static TypeExpression safeFullNameForClass(@NotNull Class<?> clazz) {
static TypeExpression safeFullNameForClass(@NotNull Class<?> clazz, JarClass<?> against) {
if (!hasSafeName(clazz)) {
throw new IllegalArgumentException("Class does not have safe name.");
}

if (clazz.isArray()) {
return new JarType.ArrayType(clazz);
return new JarType.ArrayType(clazz, against);
}

String s = clazz.getName().replaceAll("\\$\\d*", ".");
String rawName;
final Package againstPackage = against != null ? against.getClazz().getPackage() : null;
if (againstPackage != null && clazz.getPackage() != null && clazz.getDeclaringClass() == null
&& clazz.getPackage().getName().equals(againstPackage.getName())) {
rawName = clazz.getSimpleName();
} else if (clazz.getPackage() != null && clazz.getPackage().getName().equals("java.lang")) {
rawName = clazz.getSimpleName();
} else {
rawName = clazz.getName();
}

String s = rawName.replaceAll("\\$\\d*", ".");
if (s.endsWith(".")) {
throw new IllegalArgumentException("Class does not have safe name.");
}
Expand Down Expand Up @@ -407,7 +417,7 @@ && implementsInterfaces().length == 1)) {
return null;
}

return JarType.toExpression(x);
return JarType.toExpression(x, this);
}),
StringExpression.SPACE
);
Expand All @@ -417,19 +427,21 @@ && implementsInterfaces().length == 1)) {
}

@NotNull
public String compileHeaderExtends() {
final String extendsS;
public Expression compileHeaderExtends() {
Class<?> extendsClazz = extendsClass();
if (extendsClazz != null && !(extendsClazz.equals(Enum.class))) {
extendsS = "extends " + JarType.toString(extendsGenericClass()) + SPACE;
return Expressions.of(
StringExpression.EXTENDS.asSpaceAfter(),
JarType.toExpression(extendsGenericClass(), this),
StringExpression.SPACE
);
} else {
extendsS = EMPTY_STRING;
return StringExpression.EMPTY;
}
return extendsS;
}

public String compileTypeParameters() {
return JarType.convertTypeParametersToString(getClazz().getTypeParameters());
public Expression compileTypeParameters() {
return JarType.convertTypeParametersToExpression(getClazz().getTypeParameters(), this);
}

@NotNull
Expand All @@ -439,9 +451,9 @@ public Expression compileHeaderAnnotation() {
RetentionPolicy retentionPolicy = getClazz().getAnnotation(Retention.class).value();
annotationS = Expressions.of(
StringExpression.AT,
Expressions.forType(Retention.class, JarClass.safeFullNameForClass(Retention.class)),
Expressions.forType(Retention.class, JarClass.safeFullNameForClass(Retention.class, this)),
Expressions.asParenthetical(Expressions.of(
Expressions.of(safeFullNameForClass(RetentionPolicy.class)),
Expressions.of(safeFullNameForClass(RetentionPolicy.class, this)),
StringExpression.PERIOD,
Expressions.fromString(retentionPolicy.name()))),
StringExpression.SPACE
Expand All @@ -453,18 +465,16 @@ public Expression compileHeaderAnnotation() {
}

@NotNull
public static Expression typeString(@NotNull JarClass<?> clazz, boolean enumTypeClass) {
final String typeS;
public static Expression typeString(@NotNull JarClass<?> clazz) {
if (clazz.isAnnotation()) {
typeS = "@interface";
return StringExpression.ANNOTATION_TYPE;
} else if (clazz.isInterface()) {
typeS = "interface";
} else if (enumTypeClass) {
typeS = "enum";
} else {
typeS = "class";
return StringExpression.INTERFACE;
} else if (clazz.isEnum()) {
return StringExpression.ENUM;
}
return Expressions.toSpaceAfter(typeS);

return StringExpression.CLASS;
}

private <E extends Enum<?>> E[] getEnumConstants() {
Expand All @@ -489,10 +499,10 @@ static <T extends Enum> T[] getEnumConstantsFor(@NotNull Class<T> clazz) {
}
}

private class ClassExpression extends Expression {
private static class ClassExpression extends Expression {
private final List<Expression> children;

public ClassExpression(Expression methods, Expression fields, Expression constructors, Expression innerClasses, Expression clazzHeader) {
ClassExpression(Expression methods, Expression fields, Expression constructors, Expression innerClasses, Expression clazzHeader) {
children = Collections.unmodifiableList(Arrays.asList(
clazzHeader, Expressions.blockWith(fields, constructors, methods, innerClasses)
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ boolean shouldIncludeCotr() {
Expression[] parameters() {
return Arrays.stream(constructor.getParameters())
.map(parameter -> Expressions.of(
JarType.toExpression(parameter.getParameterizedType()),
JarType.toExpression(parameter.getParameterizedType(), clazz),
StringExpression.SPACE,
Expressions.fromString(parameter.getName()))
).toArray(Expression[]::new);
Expand Down Expand Up @@ -232,13 +232,13 @@ static Expression castedDefaultType(Type paramType, JarClass<?> clazz) {
return null;
}

return Expressions.toCast(JarType.toExpression(correctType, true, type -> {
return Expressions.toCast(JarType.toExpression(correctType, clazz, true, type -> {
Type obj = typeArgumentForClass(type, clazz.getClazz());
return JarType.toString(obj != null ? obj : type);
}), Value.defaultValueForType(correctType));
return JarType.toString(obj != null ? obj : type, clazz);
}), Value.defaultValueForType(correctType, clazz));
}

private class JarConstructorExpression extends Expression {
private static class JarConstructorExpression extends Expression {
private List<Expression> children;

private JarConstructorExpression(Expression security, Expression nameS, Expression parametersS, Expression stubMethod) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/davidsar/gent/stubjars/components/JarField.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public Expression compileToExpression() {
final Expression security = Expressions.of(Expressions.fromString(security().getModifier()),
Expressions.when(security() != SecurityModifier.PACKAGE,
StringExpression.SPACE));
final Expression finalS = Expressions.whenWithSpace(isFinal(), "final");
final Expression staticS = Expressions.whenWithSpace(isStatic(), "static");
final Expression volatileS = Expressions.whenWithSpace(isVolatile(), "volatile");
final Expression transientS = Expressions.whenWithSpace(isTransient(), "transient");
final Expression returnTypeS = Expressions.fromString(JarType.toString(genericReturnType()));
final Expression finalS = Expressions.whenWithSpace(isFinal(), StringExpression.FINAL);
final Expression staticS = Expressions.whenWithSpace(isStatic(), StringExpression.STATIC);
final Expression volatileS = Expressions.whenWithSpace(isVolatile(), StringExpression.VOLATILE);
final Expression transientS = Expressions.whenWithSpace(isTransient(), StringExpression.TRANSIENT);
final Expression returnTypeS = Expressions.fromString(JarType.toString(genericReturnType(), getClazz()));
final Expression nameS = Expressions.fromString(name());

final Expression assignmentS;
Expand All @@ -62,7 +62,7 @@ public Expression compileToExpression() {
Expressions.fromString(" = "),
Expressions.forType(
genericReturnType(),
Value.defaultValueForType(genericReturnType())
Value.defaultValueForType(genericReturnType(), getClazz())
)
);
} else {
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/davidsar/gent/stubjars/components/JarMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ private String[] parameters() {
}
Parameter[] parameters = method.getParameters();
String[] stringifiedParameters = Arrays.stream(parameters)
.map(parameter -> JarType.toString(parameter.getParameterizedType()) + Constants.SPACE + parameter.getName())
.toArray(String[]::new);
.map(parameter -> JarType.toString(
parameter.getParameterizedType(), parentClazz) + Constants.SPACE + parameter.getName()
).toArray(String[]::new);

if (method.isVarArgs()) {
Parameter varArgsParameter = parameters[parameters.length - 1];
Expand All @@ -68,11 +69,11 @@ private String[] parameters() {
if (parameterizedType instanceof GenericArrayType) {
stringifiedParameters[parameters.length - 1] =
JarType.toString(
((GenericArrayType) parameterizedType).getGenericComponentType())
+ "... " + varArgsParameter.getName();
((GenericArrayType) parameterizedType).getGenericComponentType(), parentClazz
) + "... " + varArgsParameter.getName();
} else if (parameterizedType instanceof Class) {
stringifiedParameters[parameters.length - 1] =
JarType.toString(((Class) parameterizedType).getComponentType()) + "... "
JarType.toString(((Class) parameterizedType).getComponentType(), parentClazz) + "... "
+ varArgsParameter.getName();
}
}
Expand All @@ -92,33 +93,28 @@ private Expression buildMethod(boolean isEnumField) {
if (parentClazz.isInterface()) {
security = StringExpression.EMPTY;
} else {
security = Expressions.of(
Expressions.fromString(security().getModifier()),
Expressions.whenWithSpace(
security() != SecurityModifier.PACKAGE, Constants.SPACE
)
);
security = security().expression();
}
final Expression finalS = Expressions.whenWithSpace(isFinal(), "final");
final Expression staticS = Expressions.whenWithSpace(isStatic(), "static");
final Expression finalS = Expressions.whenWithSpace(isFinal(), StringExpression.FINAL);
final Expression staticS = Expressions.whenWithSpace(isStatic(), StringExpression.STATIC);
final Expression abstractS;
if (parentClazz.isInterface() || isEnumField) {
abstractS = StringExpression.EMPTY;
} else {
abstractS = Expressions.whenWithSpace(isAbstract(), "abstract");
abstractS = Expressions.whenWithSpace(isAbstract(), StringExpression.ABSTRACT);
}

final Expression returnTypeS = Expressions.forType(
genericReturnType(), JarType.toExpression(genericReturnType())
genericReturnType(), JarType.toExpression(genericReturnType(), parentClazz)
);
final Expression nameS = Expressions.fromString(name());
final Expression parametersS = Utils.arrayToListExpression(parameters(), Expressions::fromString);
final Expression throwsS = Expressions.fromString(requiresThrowsSignature()
? " throws " + Utils.arrayToListExpression(throwsTypes(), JarType::toExpression)
? " throws " + Utils.arrayToListExpression(throwsTypes(), x -> JarType.toExpression(x, parentClazz))
: Constants.EMPTY_STRING);
final Expression genericS;
TypeVariable<Method>[] typeParameters = typeParameters();
genericS = Expressions.fromString(JarType.convertTypeParametersToString(typeParameters));
genericS = JarType.convertTypeParametersToExpression(typeParameters, parentClazz);

// What should the method body be?
final Expression stubMethod;
Expand Down Expand Up @@ -154,7 +150,7 @@ private Expression buildMethod(boolean isEnumField) {
Expressions.toSpaceAfter("default"),
Expressions.forType(
defaultValue().getClass(), Value.defaultValueForType(defaultValue().getClass(),
true)
parentClazz, true)
),
StringExpression.SEMICOLON
);
Expand Down
Loading

0 comments on commit 22837d0

Please # to comment.