From df54e7b622a2f6845ebfa48cef923ab3351d2cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Sat, 11 Feb 2023 18:11:49 +0100 Subject: [PATCH] #45: fixes (also for annotation) --- .../api/language/AbstractCodeLanguage.java | 14 +++- .../mmm/code/api/type/CodeTypeCategory.java | 8 +++ .../code/base/annoation/BaseAnnotation.java | 37 ++++++++-- .../mmm/code/base/member/BaseMethod.java | 3 +- .../mmm/code/base/type/BaseSuperTypes.java | 8 ++- .../github/mmm/code/base/type/BaseType.java | 10 +-- .../mmm/code/impl/java/JavaFactory.java | 30 +++++++++ .../expression/constant/JavaConstant.java | 48 +++++++++++-- .../constant/JavaConstantBoolean.java | 6 +- .../expression/constant/JavaConstantByte.java | 6 +- .../constant/JavaConstantCharacter.java | 6 +- .../constant/JavaConstantDouble.java | 6 +- .../constant/JavaConstantFloat.java | 6 +- .../constant/JavaConstantInteger.java | 6 +- .../expression/constant/JavaConstantLong.java | 6 +- .../constant/JavaConstantShort.java | 6 +- .../expression/constant/JavaEnumConstant.java | 9 ++- .../constant/JavaFactoryConstant.java | 52 -------------- .../java/expression/literal/JavaLiteral.java | 2 +- .../expression/literal/JavaLiteralClass.java | 22 +++--- .../expression/literal/JavaLiteralEnum.java | 67 +++++++++++++++++++ .../code/impl/java/JavaCodeWriterTest.java | 17 +++++ 22 files changed, 257 insertions(+), 118 deletions(-) delete mode 100644 java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaFactoryConstant.java create mode 100644 java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralEnum.java diff --git a/api/src/main/java/io/github/mmm/code/api/language/AbstractCodeLanguage.java b/api/src/main/java/io/github/mmm/code/api/language/AbstractCodeLanguage.java index 005c2d4..1b57d05 100644 --- a/api/src/main/java/io/github/mmm/code/api/language/AbstractCodeLanguage.java +++ b/api/src/main/java/io/github/mmm/code/api/language/AbstractCodeLanguage.java @@ -241,7 +241,7 @@ public void writeDeclaration(CodeType type, Appendable sink, String newline, Str String currentIndent) throws IOException { sink.append(currentIndent); - sink.append(type.getModifiers().toString()); + sink.append(getTypeModifiers(type).toString()); CodeTypeCategory category = type.getCategory(); sink.append(getKeywordForCategory(category)); sink.append(' '); @@ -259,6 +259,15 @@ public void writeDeclaration(CodeType type, Appendable sink, String newline, Str type.getSuperTypes().write(sink, null, null); } + private CodeModifiers getTypeModifiers(CodeType type) { + + CodeModifiers modifiers = type.getModifiers(); + if (type.isAnnotation() || type.isInterface()) { + modifiers = modifiers.removeModifier(CodeModifiers.KEY_ABSTRACT); + } + return modifiers; + } + @Override public void writeBody(CodeType type, Appendable sink, String newline, String defaultIndent, String currentIndent) throws IOException { @@ -419,9 +428,8 @@ protected void writeTypeReferenceEnd(CodeGenericType type, Appendable sink, Stri protected CodeModifiers getMethodModifiers(CodeMethod method) { CodeType declaringType = method.getDeclaringType(); - CodeTypeCategory category = declaringType.getCategory(); CodeModifiers modifiers = method.getModifiers(); - if (category == CodeTypeCategory.INTERFACE) { + if (declaringType.getCategory().isInterfaceOrAnnotation()) { if (modifiers.isPublic()) { modifiers = modifiers.changeVisibility(CodeVisibility.DEFAULT); } diff --git a/api/src/main/java/io/github/mmm/code/api/type/CodeTypeCategory.java b/api/src/main/java/io/github/mmm/code/api/type/CodeTypeCategory.java index b1e790c..0ee9bf3 100644 --- a/api/src/main/java/io/github/mmm/code/api/type/CodeTypeCategory.java +++ b/api/src/main/java/io/github/mmm/code/api/type/CodeTypeCategory.java @@ -57,6 +57,14 @@ public boolean isInterface() { return (this == INTERFACE); } + /** + * @return {@code true} for {@link #INTERFACE} or {@link #ANNOTATION}, {@code false} otherwise. + */ + public boolean isInterfaceOrAnnotation() { + + return (this == INTERFACE) || (this == ANNOTATION); + } + /** * @return {@code true} for {@link #ENUMERAION}, {@code false} otherwise. */ diff --git a/base/src/main/java/io/github/mmm/code/base/annoation/BaseAnnotation.java b/base/src/main/java/io/github/mmm/code/base/annoation/BaseAnnotation.java index 86d066b..89076d2 100644 --- a/base/src/main/java/io/github/mmm/code/base/annoation/BaseAnnotation.java +++ b/base/src/main/java/io/github/mmm/code/base/annoation/BaseAnnotation.java @@ -7,8 +7,10 @@ import java.lang.reflect.Method; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,6 +24,8 @@ import io.github.mmm.code.api.language.CodeLanguage; import io.github.mmm.code.api.type.CodeGenericType; import io.github.mmm.code.api.type.CodeType; +import io.github.mmm.code.base.expression.BaseArrayInstatiation; +import io.github.mmm.code.base.expression.BaseExpression; import io.github.mmm.code.base.node.BaseNodeItem; /** @@ -183,8 +187,13 @@ public Map getParameters() { String key = method.getName(); try { Object value = method.invoke(this.reflectiveObject, (Object[]) null); - Class returnType = method.getReturnType(); - this.parameters.put(key, getContext().getFactory().createExpression(value, returnType.isPrimitive())); + Object defaultValue = method.getDefaultValue(); + if (value != defaultValue) { + Class returnType = method.getReturnType(); + BaseExpression valueExpression = getContext().getFactory().createExpression(value, + returnType.isPrimitive()); + map.put(key, valueExpression); + } } catch (Exception e) { LOG.warn("Failed to read attribute {} of annotation {}.", key, this.reflectiveObject, e); } @@ -235,11 +244,18 @@ protected void doWrite(Appendable sink, String newline, String defaultIndent, St sink.append(language.getAnnotationEndIfEmpty()); } else { String prefix = "("; - for (Entry entry : this.parameters.entrySet()) { + Set> entrySet = this.parameters.entrySet(); + int size = entrySet.size(); + for (Entry entry : entrySet) { sink.append(prefix); - sink.append(entry.getKey()); - sink.append(" = "); - sink.append(formatValue(entry.getValue())); + String key = entry.getKey(); + // TODO move to CodeLanguage + if ((size > 1) || !"value".equals(key)) { + sink.append(key); + sink.append(" = "); + } + CodeExpression value = entry.getValue(); + sink.append(formatValue(value)); prefix = ", "; } sink.append(')'); @@ -249,6 +265,15 @@ protected void doWrite(Appendable sink, String newline, String defaultIndent, St private String formatValue(CodeExpression value) { + if (value == null) { + return "null"; + } + if (value instanceof BaseArrayInstatiation) { + List values = ((BaseArrayInstatiation) value).getValues(); + if (values.size() == 1) { + return formatValue(values.get(0)); + } + } return value.toString(); } diff --git a/base/src/main/java/io/github/mmm/code/base/member/BaseMethod.java b/base/src/main/java/io/github/mmm/code/base/member/BaseMethod.java index 86cbd8e..8a68679 100644 --- a/base/src/main/java/io/github/mmm/code/base/member/BaseMethod.java +++ b/base/src/main/java/io/github/mmm/code/base/member/BaseMethod.java @@ -177,7 +177,8 @@ public boolean canHaveBody() { if (modifiers.isAbstract()) { return false; } - if (getDeclaringType().getCategory().isInterface() && !modifiers.isDefaultModifier()) { + // TODO this is actually more a decision of CodeLanguage + if (getDeclaringType().getCategory().isInterfaceOrAnnotation() && !modifiers.isDefaultModifier()) { return false; } return true; diff --git a/base/src/main/java/io/github/mmm/code/base/type/BaseSuperTypes.java b/base/src/main/java/io/github/mmm/code/base/type/BaseSuperTypes.java index e6b9deb..15c18df 100644 --- a/base/src/main/java/io/github/mmm/code/base/type/BaseSuperTypes.java +++ b/base/src/main/java/io/github/mmm/code/base/type/BaseSuperTypes.java @@ -70,9 +70,11 @@ protected void doInitialize() { if (superclass != null) { addInternal(context.getType(superclass, this.parent)); } - Type[] genericInterfaces = reflectiveObject.getGenericInterfaces(); - for (Type superInterface : genericInterfaces) { - addInternal(context.getType(superInterface, this.parent)); + if (!reflectiveObject.isAnnotation()) { + Type[] genericInterfaces = reflectiveObject.getGenericInterfaces(); + for (Type superInterface : genericInterfaces) { + addInternal(context.getType(superInterface, this.parent)); + } } } } diff --git a/base/src/main/java/io/github/mmm/code/base/type/BaseType.java b/base/src/main/java/io/github/mmm/code/base/type/BaseType.java index c0f13b1..1af2974 100644 --- a/base/src/main/java/io/github/mmm/code/base/type/BaseType.java +++ b/base/src/main/java/io/github/mmm/code/base/type/BaseType.java @@ -123,11 +123,11 @@ public BaseType(BaseFile file, String simpleName, BaseType declaringType, Class< this.properties = new BaseProperties(this); if (this.reflectiveObject != null) { int mods = this.reflectiveObject.getModifiers(); - this.modifiers = CodeModifiers.of(mods); this.category = getCategory(this.reflectiveObject); + this.modifiers = CodeModifiers.of(mods); } else { - this.modifiers = CodeModifiers.MODIFIERS_PUBLIC; this.category = CodeTypeCategory.CLASS; + this.modifiers = CodeModifiers.MODIFIERS_PUBLIC; } } @@ -562,15 +562,15 @@ public boolean isBoolean() { static CodeTypeCategory getCategory(Class clazz) { CodeTypeCategory category; - if (clazz.isInterface()) { + if (clazz.isAnnotation()) { + category = CodeTypeCategory.ANNOTATION; + } else if (clazz.isInterface()) { category = CodeTypeCategory.INTERFACE; } else if (clazz.isEnum()) { category = CodeTypeCategory.ENUMERAION; // TODO requires Java 16+ // } else if (clazz.isRecord()) { // category = CodeTypeCategory.RECORD; - } else if (clazz.isAnnotation()) { - category = CodeTypeCategory.ANNOTATION; } else { category = CodeTypeCategory.CLASS; } diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/JavaFactory.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/JavaFactory.java index 54ebe3f..b1b5fa0 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/JavaFactory.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/JavaFactory.java @@ -1,6 +1,12 @@ package io.github.mmm.code.impl.java; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +import io.github.mmm.code.api.expression.CodeExpression; import io.github.mmm.code.base.BaseFactory; +import io.github.mmm.code.base.expression.BaseArrayInstatiation; import io.github.mmm.code.base.expression.BaseExpression; import io.github.mmm.code.impl.java.expression.constant.JavaConstant; @@ -14,6 +20,30 @@ public class JavaFactory extends BaseFactory { @Override public BaseExpression createExpression(Object value, boolean primitive) { + if (value != null) { + Class valueClass = value.getClass(); + if (valueClass.isArray()) { + Class componentType = valueClass.getComponentType(); + primitive = componentType.isPrimitive(); + List list; + if (primitive) { + int length = Array.getLength(value); + list = new ArrayList<>(length); + for (int i = 0; i < length; i++) { + Object item = Array.get(value, i); + list.add(createExpression(item, primitive)); + } + } else { + Object[] array = (Object[]) value; + int length = array.length; + list = new ArrayList<>(length); + for (int i = 0; i < length; i++) { + list.add(createExpression(array[i], primitive)); + } + } + return new BaseArrayInstatiation(list); + } + } return JavaConstant.of(value, primitive); } diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstant.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstant.java index c19f4a0..f2469c2 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstant.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstant.java @@ -10,6 +10,7 @@ import io.github.mmm.code.impl.java.expression.JavaExpression; import io.github.mmm.code.impl.java.expression.literal.JavaLiteral; import io.github.mmm.code.impl.java.expression.literal.JavaLiteralClass; +import io.github.mmm.code.impl.java.expression.literal.JavaLiteralEnum; import io.github.mmm.code.impl.java.expression.literal.JavaLiteralNull; import io.github.mmm.code.impl.java.expression.literal.JavaLiteralString; @@ -22,7 +23,8 @@ */ public abstract class JavaConstant extends BaseExpression implements CodeConstant, JavaExpression { - private final T value; + /** @see #getValue() */ + protected final T value; /** * The constructor. @@ -68,7 +70,8 @@ public JavaConstant evaluate() { public abstract String getSourceCode(); @Override - protected void doWrite(Appendable sink, String newline, String defaultIndent, String currentIndent, CodeLanguage language) throws IOException { + protected void doWrite(Appendable sink, String newline, String defaultIndent, String currentIndent, + CodeLanguage language) throws IOException { sink.append(getSourceCode()); } @@ -83,11 +86,25 @@ protected void doWrite(Appendable sink, String newline, String defaultIndent, St * @param value the literal value. * @param primitive {@code true} if the value is primitive and a {@link JavaLiteral#isPrimitive() primitve} * {@link JavaLiteral} shall be created, {@code false} otherwise. - * @return the {@link JavaConstant} for the given {@code value}. May be {@code null} if the type of the - * given {@code value} is not supported. + * @return the {@link JavaConstant} for the given {@code value}. May be {@code null} if the type of the given + * {@code value} is not supported. */ public static JavaConstant of(Object value, boolean primitive) { + return of(value, primitive, false); + } + + /** + * @param value the literal value. + * @param primitive {@code true} if the value is primitive and a {@link JavaLiteral#isPrimitive() primitve} + * {@link JavaLiteral} shall be created, {@code false} otherwise. + * @param qualified - {@code true} to use the {@link Class#getName() qualified name}, {@code false} otherwise (for + * {@link Class#getSimpleName() simple name}). Only relevant for {@link Class} or {@link Enum} literals. + * @return the {@link JavaConstant} for the given {@code value}. May be {@code null} if the type of the given + * {@code value} is not supported. + */ + public static JavaConstant of(Object value, boolean primitive, boolean qualified) { + if (value == null) { assert (!primitive); return JavaLiteralNull.NULL; @@ -96,9 +113,26 @@ public static JavaConstant of(Object value, boolean primitive) { } else if (value instanceof String) { return JavaLiteralString.of((String) value); } else if (value instanceof Class) { - return JavaLiteralClass.of((Class) value); - } else { - return JavaFactoryConstant.of(value); + return JavaLiteralClass.of((Class) value, qualified); + } else if (value instanceof Enum) { + return JavaLiteralEnum.of((Enum) value, qualified); + } else if (value instanceof Boolean) { + return JavaConstantBoolean.of((Boolean) value); + } else if (value instanceof Character) { + return JavaConstantCharacter.of((Character) value); + } else if (value instanceof Integer) { + return JavaConstantInteger.of((Integer) value); + } else if (value instanceof Long) { + return JavaConstantLong.of((Long) value); + } else if (value instanceof Double) { + return JavaConstantDouble.of((Double) value); + } else if (value instanceof Float) { + return JavaConstantFloat.of((Float) value); + } else if (value instanceof Short) { + return JavaConstantShort.of((Short) value); + } else if (value instanceof Byte) { + return JavaConstantByte.of((Byte) value); } + return null; } } diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantBoolean.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantBoolean.java index 8412565..df54377 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantBoolean.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantBoolean.java @@ -3,12 +3,12 @@ package io.github.mmm.code.impl.java.expression.constant; /** - * Implementation of {@link JavaFactoryConstant} for {@link Boolean} using {@link Boolean#valueOf(boolean)}. + * Implementation of {@link JavaConstant} for {@link Boolean} using {@link Boolean#valueOf(boolean)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantBoolean extends JavaFactoryConstant { +public class JavaConstantBoolean extends JavaConstant { /** {@link JavaConstantBoolean} for {@link Boolean#TRUE}. */ public static final JavaConstantBoolean TRUE = new JavaConstantBoolean(Boolean.TRUE); @@ -35,7 +35,7 @@ public JavaConstant withValue(Boolean newValue) { @Override public String getSourceCode() { - return "Boolean.valueOf(" + getValue().toString() + ")"; + return "Boolean.valueOf(" + this.value.toString() + ")"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantByte.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantByte.java index f7eb687..7d14706 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantByte.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantByte.java @@ -3,12 +3,12 @@ package io.github.mmm.code.impl.java.expression.constant; /** - * Implementation of {@link JavaFactoryConstant} for {@link Byte} using {@link Byte#valueOf(byte)}. + * Implementation of {@link JavaConstant} for {@link Byte} using {@link Byte#valueOf(byte)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantByte extends JavaFactoryConstant { +public class JavaConstantByte extends JavaConstant { /** * The constructor. @@ -29,7 +29,7 @@ public JavaConstant withValue(Byte newValue) { @Override public String getSourceCode() { - return "Byte.valueOf((byte) " + getValue().toString() + ")"; + return "Byte.valueOf((byte) " + this.value.toString() + ")"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantCharacter.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantCharacter.java index ebde8eb..a698089 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantCharacter.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantCharacter.java @@ -5,12 +5,12 @@ import io.github.mmm.code.impl.java.expression.literal.JavaLiteralChar; /** - * Implementation of {@link JavaFactoryConstant} for {@link Character} using {@link Character#valueOf(char)}. + * Implementation of {@link JavaConstant} for {@link Character} using {@link Character#valueOf(char)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantCharacter extends JavaFactoryConstant { +public class JavaConstantCharacter extends JavaConstant { /** * The constructor. @@ -31,7 +31,7 @@ public JavaConstant withValue(Character newValue) { @Override public String getSourceCode() { - return "Character.valueOf(" + JavaLiteralChar.toCharLiteral(getValue().charValue()) + ")"; + return "Character.valueOf(" + JavaLiteralChar.toCharLiteral(this.value.charValue()) + ")"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantDouble.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantDouble.java index db0553e..4c7a57c 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantDouble.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantDouble.java @@ -3,12 +3,12 @@ package io.github.mmm.code.impl.java.expression.constant; /** - * Implementation of {@link JavaFactoryConstant} for {@link Double} using {@link Double#valueOf(double)}. + * Implementation of {@link JavaConstant} for {@link Double} using {@link Double#valueOf(double)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantDouble extends JavaFactoryConstant { +public class JavaConstantDouble extends JavaConstant { /** * The constructor. @@ -29,7 +29,7 @@ public JavaConstant withValue(Double newValue) { @Override public String getSourceCode() { - return "Double.valueOf(" + getValue().toString() + "D)"; + return "Double.valueOf(" + this.value.toString() + "D)"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantFloat.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantFloat.java index 5313135..db8af3d 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantFloat.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantFloat.java @@ -3,12 +3,12 @@ package io.github.mmm.code.impl.java.expression.constant; /** - * Implementation of {@link JavaFactoryConstant} for {@link Float} using {@link Float#valueOf(float)}. + * Implementation of {@link JavaConstant} for {@link Float} using {@link Float#valueOf(float)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantFloat extends JavaFactoryConstant { +public class JavaConstantFloat extends JavaConstant { /** * The constructor. @@ -29,7 +29,7 @@ public JavaConstant withValue(Float newValue) { @Override public String getSourceCode() { - return "Float.valueOf(" + getValue().toString() + "F)"; + return "Float.valueOf(" + this.value.toString() + "F)"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantInteger.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantInteger.java index 34bfa73..1f08f4f 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantInteger.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantInteger.java @@ -3,12 +3,12 @@ package io.github.mmm.code.impl.java.expression.constant; /** - * Implementation of {@link JavaFactoryConstant} for {@link Integer} using {@link Integer#valueOf(int)}. + * Implementation of {@link JavaConstant} for {@link Integer} using {@link Integer#valueOf(int)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantInteger extends JavaFactoryConstant { +public class JavaConstantInteger extends JavaConstant { /** * The constructor. @@ -29,7 +29,7 @@ public JavaConstant withValue(Integer newValue) { @Override public String getSourceCode() { - return "Integer.valueOf(" + getValue().toString() + ")"; + return "Integer.valueOf(" + this.value.toString() + ")"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantLong.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantLong.java index ddbd891..fcbe4b3 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantLong.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantLong.java @@ -3,12 +3,12 @@ package io.github.mmm.code.impl.java.expression.constant; /** - * Implementation of {@link JavaFactoryConstant} for {@link Long} using {@link Long#valueOf(long)}. + * Implementation of {@link JavaConstant} for {@link Long} using {@link Long#valueOf(long)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantLong extends JavaFactoryConstant { +public class JavaConstantLong extends JavaConstant { /** * The constructor. @@ -29,7 +29,7 @@ public JavaConstant withValue(Long newValue) { @Override public String getSourceCode() { - return "Long.valueOf(" + getValue().toString() + "L)"; + return "Long.valueOf(" + this.value.toString() + "L)"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantShort.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantShort.java index 7ad0593..975e0d7 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantShort.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaConstantShort.java @@ -3,12 +3,12 @@ package io.github.mmm.code.impl.java.expression.constant; /** - * Implementation of {@link JavaFactoryConstant} for {@link Short} using {@link Short#valueOf(short)}. + * Implementation of {@link JavaConstant} for {@link Short} using {@link Short#valueOf(short)}. * * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @since 1.0.0 */ -public class JavaConstantShort extends JavaFactoryConstant { +public class JavaConstantShort extends JavaConstant { /** * The constructor. @@ -29,7 +29,7 @@ public JavaConstant withValue(Short newValue) { @Override public String getSourceCode() { - return "Short.valueOf((short) " + getValue().toString() + ")"; + return "Short.valueOf((short) " + this.value.toString() + ")"; } /** diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaEnumConstant.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaEnumConstant.java index d095f04..b454966 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaEnumConstant.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaEnumConstant.java @@ -22,15 +22,14 @@ private JavaEnumConstant(T value, boolean qualified) { @Override public String getSourceCode() { - T value = getValue(); - Class enumClass = value.getClass(); + Class enumClass = this.value.getClass(); String name; if (this.unqualified) { name = enumClass.getSimpleName(); } else { name = enumClass.getName(); } - return name + "." + value.name(); + return name + "." + this.value.name(); } @Override @@ -42,8 +41,8 @@ public JavaConstant withValue(T newValue) { /** * @param type of the {@link Enum} {@link #getValue() value}. * @param value the constant value. - * @param unqualified - {@code true} to use the {@link Class#getSimpleName() simple name}, {@code false} - * otherwise (for {@link Class#getName() qualified name}). + * @param unqualified - {@code true} to use the {@link Class#getSimpleName() simple name}, {@code false} otherwise + * (for {@link Class#getName() qualified name}). * @return the {@link JavaConstant} for the given {@code value}. */ public static > JavaEnumConstant of(T value, boolean unqualified) { diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaFactoryConstant.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaFactoryConstant.java deleted file mode 100644 index 7652abd..0000000 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/constant/JavaFactoryConstant.java +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 - * http://www.apache.org/licenses/LICENSE-2.0 */ -package io.github.mmm.code.impl.java.expression.constant; - -/** - * Implementation of {@link JavaConstant} using a standard factory method such as - * {@link Integer#valueOf(int)}. - * - * @author Joerg Hohwiller (hohwille at users.sourceforge.net) - * @since 1.0.0 - * @param type of {@link #getValue() value}. - */ -public abstract class JavaFactoryConstant extends JavaConstant { - - /** - * The constructor. - * - * @param value the {@link #getValue() value}. - */ - public JavaFactoryConstant(T value) { - - super(value); - } - - /** - * @param value the literal value. - * @return the {@link JavaConstant} for the given {@code value}. May be {@code null} if the type of the - * given {@code value} is not supported. - */ - public static JavaConstant of(Object value) { - - if (value instanceof Boolean) { - return JavaConstantBoolean.of((Boolean) value); - } else if (value instanceof Character) { - return JavaConstantCharacter.of((Character) value); - } else if (value instanceof Integer) { - return JavaConstantInteger.of((Integer) value); - } else if (value instanceof Long) { - return JavaConstantLong.of((Long) value); - } else if (value instanceof Double) { - return JavaConstantDouble.of((Double) value); - } else if (value instanceof Float) { - return JavaConstantFloat.of((Float) value); - } else if (value instanceof Short) { - return JavaConstantShort.of((Short) value); - } else if (value instanceof Byte) { - return JavaConstantByte.of((Byte) value); - } - return null; - } - -} diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteral.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteral.java index f9adb45..555f838 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteral.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteral.java @@ -11,7 +11,7 @@ /** * Implementation of {@link CodeLiteral} for Java. * - * @author hohwille + * @author Joerg Hohwiller (hohwille at users.sourceforge.net) * @param type of {@link #getValue()}. * @since 1.0.0 */ diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralClass.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralClass.java index 41077d0..a90de5b 100644 --- a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralClass.java +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralClass.java @@ -13,18 +13,18 @@ */ public final class JavaLiteralClass extends JavaLiteral> { - private final boolean unqualified; + private final boolean qualified; - private JavaLiteralClass(Class value, boolean unqualified) { + private JavaLiteralClass(Class value, boolean qualified) { super(value); - this.unqualified = unqualified; + this.qualified = qualified; } @Override public JavaLiteralClass withValue(Class newValue) { - return new JavaLiteralClass<>(newValue, this.unqualified); + return new JavaLiteralClass<>(newValue, this.qualified); } @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -38,10 +38,10 @@ public Class> getJavaClass() { public String getSourceCode() { String name; - if (this.unqualified) { - name = getValue().getSimpleName(); + if (this.qualified) { + name = this.value.getSimpleName(); } else { - name = getValue().getName(); + name = this.value.getName(); } return name + ".class"; } @@ -65,12 +65,12 @@ public static JavaLiteralClass of(Class value) { /** * @param the type of the {@link Class} {@code value}. * @param value the literal value. May not be {@code null}. - * @param unqualified - {@code true} to use the {@link Class#getSimpleName() simple name}, {@code false} - * otherwise (for {@link Class#getName() qualified name}). + * @param qualified - {@code true} to use the {@link Class#getName() qualified name}, {@code false} otherwise (for + * {@link Class#getSimpleName() simple name}). * @return the {@link CodeLiteral} for the given {@code value}. */ - public static JavaLiteralClass of(Class value, boolean unqualified) { + public static JavaLiteralClass of(Class value, boolean qualified) { - return new JavaLiteralClass<>(value, unqualified); + return new JavaLiteralClass<>(value, qualified); } } diff --git a/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralEnum.java b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralEnum.java new file mode 100644 index 0000000..96fc3cc --- /dev/null +++ b/java/impl/src/main/java/io/github/mmm/code/impl/java/expression/literal/JavaLiteralEnum.java @@ -0,0 +1,67 @@ +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.code.impl.java.expression.literal; + +import io.github.mmm.code.impl.java.expression.constant.JavaConstant; + +/** + * Implementation of {@link JavaConstant} for {@link Enum} using {@link Enum#valueOf(Class, String)}. + * + * @author Joerg Hohwiller (hohwille at users.sourceforge.net) + * @since 1.0.0 + */ +public final class JavaLiteralEnum extends JavaLiteral> { + + private final boolean qualified; + + private JavaLiteralEnum(Enum value, boolean qualified) { + + super(value); + this.qualified = qualified; + } + + @Override + public JavaLiteralEnum withValue(Enum newValue) { + + return new JavaLiteralEnum(newValue, this.qualified); + } + + @Override + public String getSourceCode() { + + String name; + if (this.qualified) { + name = this.value.getDeclaringClass().getName(); + } else { + name = this.value.getDeclaringClass().getSimpleName(); + } + return name + "." + this.value.name(); + } + + @Override + public boolean isPrimitive() { + + return false; + } + + /** + * @param value the constant value. + * @return the {@link JavaConstant} for the given {@code value}. + */ + public static JavaLiteralEnum of(Enum value) { + + return new JavaLiteralEnum(value, false); + } + + /** + * @param value the constant value. + * @param qualified - {@code true} to use the {@link Class#getName() qualified name}, {@code false} otherwise (for + * {@link Class#getSimpleName() simple name}). + * @return the {@link JavaConstant} for the given {@code value}. + */ + public static JavaLiteralEnum of(Enum value, boolean qualified) { + + return new JavaLiteralEnum(value, qualified); + } + +} diff --git a/java/impl/src/test/java/io/github/mmm/code/impl/java/JavaCodeWriterTest.java b/java/impl/src/test/java/io/github/mmm/code/impl/java/JavaCodeWriterTest.java index c08ae06..9848285 100644 --- a/java/impl/src/test/java/io/github/mmm/code/impl/java/JavaCodeWriterTest.java +++ b/java/impl/src/test/java/io/github/mmm/code/impl/java/JavaCodeWriterTest.java @@ -1,5 +1,7 @@ package io.github.mmm.code.impl.java; +import java.lang.annotation.Retention; + import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @@ -9,6 +11,7 @@ import io.github.mmm.code.base.BaseFile; import io.github.mmm.code.base.member.BaseField; import io.github.mmm.code.base.member.BaseMethod; +import io.github.mmm.code.base.type.BaseGenericType; import io.github.mmm.code.base.type.BaseType; /** @@ -110,4 +113,18 @@ public void testWriteInterface() { + "}\n"); } + @Test + public void testWriteAnnotation() { + + JavaRootContext context = JavaRootContext.get(); + BaseGenericType retention = context.getType(Retention.class); + assertThat(retention.write()).isEqualTo("@Documented\n" // + + "@Retention(RetentionPolicy.RUNTIME)\n" // + + "@Target(ElementType.ANNOTATION_TYPE)\n" // + + "public @interface Retention {\n" // + + "\n" // + + " RetentionPolicy value();\n" // + + "}\n"); + } + }