From 22837d0db5a65a3b32b288b8addf107c14c8bd51 Mon Sep 17 00:00:00 2001 From: David Sargent Date: Sun, 12 Aug 2018 23:08:31 -0400 Subject: [PATCH] Don't write the full name if we don't have to --- .../gent/stubjars/components/JarClass.java | 60 +++++++----- .../stubjars/components/JarConstructor.java | 10 +- .../gent/stubjars/components/JarField.java | 12 +-- .../gent/stubjars/components/JarMethod.java | 32 +++---- .../gent/stubjars/components/JarType.java | 95 ++++++++++--------- .../gent/stubjars/components/Value.java | 14 +-- .../expressions/ClassHeaderExpression.java | 42 ++++---- .../components/expressions/Expressions.java | 3 +- .../expressions/StringExpression.java | 9 ++ .../stubjars/components/writer/Constants.java | 9 ++ 10 files changed, 155 insertions(+), 131 deletions(-) diff --git a/src/main/java/davidsar/gent/stubjars/components/JarClass.java b/src/main/java/davidsar/gent/stubjars/components/JarClass.java index aadc185..4179830 100644 --- a/src/main/java/davidsar/gent/stubjars/components/JarClass.java +++ b/src/main/java/davidsar/gent/stubjars/components/JarClass.java @@ -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; @@ -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."); } @@ -407,7 +417,7 @@ && implementsInterfaces().length == 1)) { return null; } - return JarType.toExpression(x); + return JarType.toExpression(x, this); }), StringExpression.SPACE ); @@ -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 @@ -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 @@ -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[] getEnumConstants() { @@ -489,10 +499,10 @@ static T[] getEnumConstantsFor(@NotNull Class clazz) { } } - private class ClassExpression extends Expression { + private static class ClassExpression extends Expression { private final List 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) )); diff --git a/src/main/java/davidsar/gent/stubjars/components/JarConstructor.java b/src/main/java/davidsar/gent/stubjars/components/JarConstructor.java index 16ab587..83531f7 100644 --- a/src/main/java/davidsar/gent/stubjars/components/JarConstructor.java +++ b/src/main/java/davidsar/gent/stubjars/components/JarConstructor.java @@ -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); @@ -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 children; private JarConstructorExpression(Expression security, Expression nameS, Expression parametersS, Expression stubMethod) { diff --git a/src/main/java/davidsar/gent/stubjars/components/JarField.java b/src/main/java/davidsar/gent/stubjars/components/JarField.java index 213e92e..0653c12 100644 --- a/src/main/java/davidsar/gent/stubjars/components/JarField.java +++ b/src/main/java/davidsar/gent/stubjars/components/JarField.java @@ -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; @@ -62,7 +62,7 @@ public Expression compileToExpression() { Expressions.fromString(" = "), Expressions.forType( genericReturnType(), - Value.defaultValueForType(genericReturnType()) + Value.defaultValueForType(genericReturnType(), getClazz()) ) ); } else { diff --git a/src/main/java/davidsar/gent/stubjars/components/JarMethod.java b/src/main/java/davidsar/gent/stubjars/components/JarMethod.java index 905828e..b72a3b2 100644 --- a/src/main/java/davidsar/gent/stubjars/components/JarMethod.java +++ b/src/main/java/davidsar/gent/stubjars/components/JarMethod.java @@ -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]; @@ -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(); } } @@ -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[] typeParameters = typeParameters(); - genericS = Expressions.fromString(JarType.convertTypeParametersToString(typeParameters)); + genericS = JarType.convertTypeParametersToExpression(typeParameters, parentClazz); // What should the method body be? final Expression stubMethod; @@ -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 ); diff --git a/src/main/java/davidsar/gent/stubjars/components/JarType.java b/src/main/java/davidsar/gent/stubjars/components/JarType.java index 3c04a19..da140b4 100644 --- a/src/main/java/davidsar/gent/stubjars/components/JarType.java +++ b/src/main/java/davidsar/gent/stubjars/components/JarType.java @@ -39,53 +39,57 @@ public JarType(@NotNull Type type) { this.type = type; } - static String convertTypeParametersToString(TypeVariable[] typeParameters) { - String genericS; + static Expression convertTypeParametersToExpression(TypeVariable[] typeParameters, JarClass against) { if (typeParameters.length == 0) { - genericS = Constants.SPACE; - } else { - Expression typeParams = Utils.arrayToListExpression(typeParameters, typeParam -> { - if (typeParam.getBounds()[0] == Object.class) { - return Expressions.fromString(typeParam.getName()); - } else { - return Expressions.fromString(typeParam.getName() + " extends " + JarType.toString(typeParam.getBounds()[0])); - } - }); - genericS = "<" + typeParams + "> "; + return StringExpression.SPACE; } - return genericS; + Expression typeParams = Utils.arrayToListExpression(typeParameters, typeParam -> { + if (typeParam.getBounds()[0] == Object.class) { + return Expressions.fromString(typeParam.getName()); + } else { + return Expressions.of( + Expressions.fromString(typeParam.getName()), + StringExpression.SPACE, + StringExpression.EXTENDS, + StringExpression.SPACE, + JarType.toExpression(typeParam.getBounds()[0], against)); + } + }); + return Expressions.of( + StringExpression.LESS_THAN, + typeParams, + StringExpression.GREATER_THAN + ); } @NotNull @Override public String toString() { - return toString(type); + return toString(type, null); } @NotNull - static String toString(@NotNull Type type) { - return toString(type, false, null); + static String toString(@NotNull Type type, JarClass against) { + return toString(type, against, false, null); } - @NotNull - static TypeExpression toExpression(@NotNull Type type) { - return toExpression(type, false, null); + static TypeExpression toExpression(@NotNull Type type, JarClass against) { + return toExpression(type, against, false, null); } @NotNull - public static String toString(@NotNull Type type, boolean keepSimple, @Nullable Function resolver) { - return toExpression(type, keepSimple, resolver).toString(); + public static String toString(@NotNull Type type, @NotNull JarClass against, boolean keepSimple, @Nullable Function resolver) { + return toExpression(type, against, keepSimple, resolver).toString(); } - @NotNull - public static TypeExpression toExpression(@NotNull Type type, boolean keepSimple, @Nullable Function resolver) { + public static TypeExpression toExpression(@NotNull Type type, JarClass against, boolean keepSimple, @Nullable Function resolver) { if (type instanceof Class) { - return JarClass.safeFullNameForClass((Class) type); + return JarClass.safeFullNameForClass((Class) type, against); } if (type instanceof ParameterizedType) { - return parameterizedTypeToExpression((ParameterizedType) type, keepSimple); + return parameterizedTypeToExpression((ParameterizedType) type, against, keepSimple); } if (type instanceof TypeVariable) { @@ -93,26 +97,26 @@ public static TypeExpression toExpression(@NotNull Type type, boolean keepSimple } if (type instanceof GenericArrayType) { - return genericArrayTypeToExpression(type, keepSimple, resolver); + return genericArrayTypeToExpression(type, against, keepSimple, resolver); } if (type instanceof WildcardType) { - return wildcardTypeToExpression((WildcardType) type, keepSimple, resolver); + return wildcardTypeToExpression((WildcardType) type, against, keepSimple, resolver); } throw new UnsupportedOperationException(type.getClass().getName()); } @NotNull - private static TypeExpression parameterizedTypeToExpression(@NotNull ParameterizedType type, boolean keepSimple) { + private static TypeExpression parameterizedTypeToExpression(@NotNull ParameterizedType type, JarClass against, boolean keepSimple) { TypeExpression ownerTypeExpression; if (type.getOwnerType() != null) { - ownerTypeExpression = handleOwnerTypeOfParameterizedType(type); + ownerTypeExpression = handleOwnerTypeOfParameterizedType(type, against); } else { - ownerTypeExpression = handleRawTypeOfParameterizedType(type); + ownerTypeExpression = handleRawTypeOfParameterizedType(type, against); } - Expression typeArgumentExpression = handleTypeArgumentsOfParameterizedType(type, keepSimple); + Expression typeArgumentExpression = handleTypeArgumentsOfParameterizedType(type, against, keepSimple); if (typeArgumentExpression == null) { return ownerTypeExpression; @@ -121,15 +125,14 @@ private static TypeExpression parameterizedTypeToExpression(@NotNull Parameteriz } } - @NotNull - private static TypeExpression handleRawTypeOfParameterizedType(@NotNull ParameterizedType type) { + private static TypeExpression handleRawTypeOfParameterizedType(@NotNull ParameterizedType type, JarClass against) { return Expressions.forType(type.getRawType(), - JarClass.safeFullNameForClass((Class) type.getRawType()) + JarClass.safeFullNameForClass((Class) type.getRawType(), against) ); } @Nullable - private static Expression handleTypeArgumentsOfParameterizedType(@NotNull ParameterizedType type, boolean keepSimple) { + private static Expression handleTypeArgumentsOfParameterizedType(@NotNull ParameterizedType type, JarClass against, boolean keepSimple) { Type[] actualTypeArguments = type.getActualTypeArguments(); Expression typeArgumentExpression = null; if (!keepSimple && (actualTypeArguments != null && actualTypeArguments.length > 0)) { @@ -141,7 +144,7 @@ private static Expression handleTypeArgumentsOfParameterizedType(@NotNull Parame || typeArg instanceof TypeVariable || typeArg instanceof GenericArrayType || typeArg instanceof WildcardType) { - return toExpression(typeArg); + return toExpression(typeArg, against); } throw new UnsupportedOperationException(typeArg.getClass().getName()); })), @@ -152,14 +155,14 @@ private static Expression handleTypeArgumentsOfParameterizedType(@NotNull Parame } @NotNull - private static TypeExpression handleOwnerTypeOfParameterizedType(@NotNull ParameterizedType type) { + private static TypeExpression handleOwnerTypeOfParameterizedType(@NotNull ParameterizedType type, JarClass against) { TypeExpression ownerTypeExpression; if (type.getOwnerType() instanceof Class) { - ownerTypeExpression = handleRawTypeOfParameterizedType(type); + ownerTypeExpression = handleRawTypeOfParameterizedType(type, against); } else if (type.getOwnerType() instanceof ParameterizedType) { Class rawTypeOfOwner = (Class) ((ParameterizedType) type.getOwnerType()).getRawType(); ownerTypeExpression = Expressions.forType(rawTypeOfOwner, Expressions.of( - toExpression(type.getOwnerType()), + toExpression(type.getOwnerType(), against), StringExpression.PERIOD, Expressions.forType(type.getRawType(), Expressions.fromString( ((Class) type.getRawType()).getName() @@ -184,23 +187,23 @@ private static TypeExpression typeVariableToExpression(@NotNull Type type, @Null } @NotNull - private static TypeExpression genericArrayTypeToExpression(@NotNull Type type, boolean keepSimple, @Nullable Function resolver) { + private static TypeExpression genericArrayTypeToExpression(@NotNull Type type, JarClass against, boolean keepSimple, @Nullable Function resolver) { return Expressions.forType(type, Expressions.of( - toExpression(((GenericArrayType) type).getGenericComponentType(), keepSimple, resolver), + toExpression(((GenericArrayType) type).getGenericComponentType(), against, keepSimple, resolver), Expressions.fromString("[]") ) ); } @NotNull - private static TypeExpression wildcardTypeToExpression(@NotNull WildcardType type, boolean keepSimple, @Nullable Function resolver) { + private static TypeExpression wildcardTypeToExpression(@NotNull WildcardType type, JarClass against, boolean keepSimple, @Nullable Function resolver) { if (type.getLowerBounds() != null && type.getLowerBounds().length > 0) { - return new WildcardSuperType(type, toExpression(type.getLowerBounds()[0], keepSimple, resolver)); + return new WildcardSuperType(type, toExpression(type.getLowerBounds()[0], against, keepSimple, resolver)); } else if (type.getUpperBounds()[0] == Object.class) { return new WildcardTypeExpression(); } else { - return new WildcardExtendsType(type, toExpression(type.getUpperBounds()[0], keepSimple, resolver)); + return new WildcardExtendsType(type, toExpression(type.getUpperBounds()[0], against, keepSimple, resolver)); } } @@ -260,9 +263,9 @@ private static class WildcardTypeExpression extends TypeExpression { } static class ArrayType extends TypeExpression { - public ArrayType(@NotNull Class clazz) { + public ArrayType(@NotNull Class clazz, JarClass against) { super(clazz, Expressions.of( - JarClass.safeFullNameForClass(clazz.getComponentType()), + JarClass.safeFullNameForClass(clazz.getComponentType(), against), StringExpression.LEFT_BRACE, StringExpression.RIGHT_BRACE) ); diff --git a/src/main/java/davidsar/gent/stubjars/components/Value.java b/src/main/java/davidsar/gent/stubjars/components/Value.java index 457c77a..070a4a1 100644 --- a/src/main/java/davidsar/gent/stubjars/components/Value.java +++ b/src/main/java/davidsar/gent/stubjars/components/Value.java @@ -27,8 +27,8 @@ class Value { * @param type a {@link Type} to get the default value for * @return a String with the default type for the parameter type */ - static Expression defaultValueForType(Type type) { - return defaultValueForType(type, false); + static Expression defaultValueForType(Type type, JarClass against) { + return defaultValueForType(type, against, false); } /** @@ -38,13 +38,13 @@ static Expression defaultValueForType(Type type) { * @param constant {@code true} if the returned value should be a constant * @return a String with the default type for the parameter type */ - static Expression defaultValueForType(Type type, boolean constant) { + static Expression defaultValueForType(Type type, JarClass against, boolean constant) { if (!(type instanceof Class)) { if (type instanceof ParameterizedType) { - return defaultValueForType(((ParameterizedType) type).getRawType()); + return defaultValueForType(((ParameterizedType) type).getRawType(), against); } - return defaultValueForType(Object.class); + return defaultValueForType(Object.class, against); } if (type.equals(int.class) || type.equals(Integer.class)) { @@ -73,7 +73,7 @@ static Expression defaultValueForType(Type type, boolean constant) { return Expressions.of( Expressions.fromString("new"), StringExpression.SPACE, - JarType.toExpression(type), + JarType.toExpression(type, against), StringExpression.SPACE, Expressions.emptyBlock()); } else if (((Class) type).isEnum()) { @@ -88,7 +88,7 @@ static Expression defaultValueForType(Type type, boolean constant) { } return Expressions.of( - JarType.toExpression(type), + JarType.toExpression(type, against), StringExpression.PERIOD, Expressions.fromString(enumConstants[0].name()) ); diff --git a/src/main/java/davidsar/gent/stubjars/components/expressions/ClassHeaderExpression.java b/src/main/java/davidsar/gent/stubjars/components/expressions/ClassHeaderExpression.java index 8fae546..5b591e3 100644 --- a/src/main/java/davidsar/gent/stubjars/components/expressions/ClassHeaderExpression.java +++ b/src/main/java/davidsar/gent/stubjars/components/expressions/ClassHeaderExpression.java @@ -16,34 +16,32 @@ import davidsar.gent.stubjars.components.JarClass; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class ClassHeaderExpression extends Expression { - private final Expression annotationS; - private final Expression security; - private final Expression staticS; - private final Expression abstractS; - private final Expression finalS; - private final Expression typeS; - private final Expression nameS; - private final Expression genericS; - private final Expression extendsS; - private final Expression implementsS; + private final List children; public ClassHeaderExpression(JarClass jarClass) { - this.annotationS = jarClass.compileHeaderAnnotation(); - this.security = jarClass.security().expression(); - this.finalS = Expressions.whenWithSpace(jarClass.isFinal() && !jarClass.isEnum(), "final"); - this.staticS = Expressions.whenWithSpace(jarClass.isStatic() && !jarClass.isEnum(), "static"); - this.abstractS = Expressions.whenWithSpace(jarClass.isAbstract() + Expression annotationS = jarClass.compileHeaderAnnotation(); + Expression security = jarClass.security().expression(); + Expression finalS = Expressions.whenWithSpace(jarClass.isFinal() && !jarClass.isEnum(), + StringExpression.FINAL); + Expression staticS = Expressions.whenWithSpace(jarClass.isStatic() && !jarClass.isEnum(), + StringExpression.STATIC); + Expression abstractS = Expressions.whenWithSpace(jarClass.isAbstract() && !jarClass.isEnum() && !jarClass.isAnnotation() - && !jarClass.isInterface(), "abstract"); - this.typeS = Expressions.forType(jarClass.getClazz(), JarClass.typeString(jarClass, jarClass.isEnum())); - this.genericS = Expressions.fromString(jarClass.compileTypeParameters()); - this.nameS = Expressions.fromString(jarClass.name()); - this.extendsS = Expressions.fromString(jarClass.compileHeaderExtends()); - this.implementsS = Expressions.of(jarClass.compileHeaderImplements()); + && !jarClass.isInterface(), StringExpression.ABSTRACT); + Expression typeS = JarClass.typeString(jarClass); + Expression genericS = jarClass.compileTypeParameters(); + Expression nameS = Expressions.fromString(jarClass.name()); + Expression extendsS = jarClass.compileHeaderExtends(); + Expression implementsS = jarClass.compileHeaderImplements(); + + children = Collections.unmodifiableList(Arrays.asList( + annotationS, security, staticS, abstractS, finalS, typeS, StringExpression.SPACE, nameS, genericS, StringExpression.SPACE, extendsS, implementsS + )); } @Override @@ -53,6 +51,6 @@ public boolean hasChildren() { @Override public List children() { - return Arrays.asList(annotationS, security, staticS, abstractS, finalS, typeS, nameS, genericS, extendsS, implementsS); + return children; } } diff --git a/src/main/java/davidsar/gent/stubjars/components/expressions/Expressions.java b/src/main/java/davidsar/gent/stubjars/components/expressions/Expressions.java index da785e2..cfed547 100644 --- a/src/main/java/davidsar/gent/stubjars/components/expressions/Expressions.java +++ b/src/main/java/davidsar/gent/stubjars/components/expressions/Expressions.java @@ -108,8 +108,7 @@ public static Expression when(boolean condition, Expression expression) { return StringExpression.EMPTY; } - @NotNull - public static Expression whenWithSpace(boolean condition, String string) { + public static Expression whenWithSpace(boolean condition, Expression string) { if (condition) { return toSpaceAfter(string); } diff --git a/src/main/java/davidsar/gent/stubjars/components/expressions/StringExpression.java b/src/main/java/davidsar/gent/stubjars/components/expressions/StringExpression.java index f382126..74c34f6 100644 --- a/src/main/java/davidsar/gent/stubjars/components/expressions/StringExpression.java +++ b/src/main/java/davidsar/gent/stubjars/components/expressions/StringExpression.java @@ -26,6 +26,15 @@ public final class StringExpression extends Expression { public static final Expression RIGHT_BRACE = new StringExpression(Constants.RIGHT_BRACE); public static final Expression EXTENDS = new StringExpression(Constants.EXTENDS); public static final Expression SUPER = new StringExpression(Constants.SUPER); + public static final Expression ANNOTATION_TYPE = new StringExpression(Constants.ANNOTATION_TYPE); + public static final Expression INTERFACE = new StringExpression(Constants.INTERFACE); + public static final Expression ENUM = new StringExpression(Constants.ENUM); + public static final Expression CLASS = new StringExpression(Constants.CLASS); + public static final Expression FINAL = new StringExpression(Constants.FINAL); + public static final Expression STATIC = new StringExpression(Constants.STATIC); + public static final Expression ABSTRACT = new StringExpression(Constants.ABSTRACT); + public static final Expression VOLATILE = new StringExpression(Constants.VOLATILE); + public static final Expression TRANSIENT = new StringExpression(Constants.TRANSIENT); static final Expression LEFT_PAREN = new StringExpression(Constants.LEFT_PAREN); static final Expression RIGHT_PAREN = new StringExpression(Constants.RIGHT_PAREN); public static final Expression SPACE = new StringExpression(Constants.SPACE); diff --git a/src/main/java/davidsar/gent/stubjars/components/writer/Constants.java b/src/main/java/davidsar/gent/stubjars/components/writer/Constants.java index 2679bf1..0d8798f 100644 --- a/src/main/java/davidsar/gent/stubjars/components/writer/Constants.java +++ b/src/main/java/davidsar/gent/stubjars/components/writer/Constants.java @@ -34,4 +34,13 @@ public class Constants { public static final String RIGHT_BRACE = "]"; public static final String EXTENDS = "extends"; public static final String SUPER = "super"; + public static final String ANNOTATION_TYPE = "@interface"; + public static final String INTERFACE = "interface"; + public static final String ENUM = "enum"; + public static final String CLASS = "class"; + public static final String FINAL = "final"; + public static final String STATIC = "static"; + public static final String ABSTRACT = "abstract"; + public static final String TRANSIENT = "transient"; + public static final String VOLATILE = "volatile"; }