Skip to content

Commit

Permalink
#45: fixes (also for annotation)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Feb 11, 2023
1 parent 3cf878a commit df54e7b
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -183,8 +187,13 @@ public Map<String, CodeExpression> 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);
}
Expand Down Expand Up @@ -235,11 +244,18 @@ protected void doWrite(Appendable sink, String newline, String defaultIndent, St
sink.append(language.getAnnotationEndIfEmpty());
} else {
String prefix = "(";
for (Entry<String, CodeExpression> entry : this.parameters.entrySet()) {
Set<Entry<String, CodeExpression>> entrySet = this.parameters.entrySet();
int size = entrySet.size();
for (Entry<String, CodeExpression> 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(')');
Expand All @@ -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<CodeExpression> values = ((BaseArrayInstatiation) value).getValues();
if (values.size() == 1) {
return formatValue(values.get(0));
}
}
return value.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions base/src/main/java/io/github/mmm/code/base/type/BaseType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,6 +20,30 @@ public class JavaFactory extends BaseFactory {
@Override
public BaseExpression createExpression(Object value, boolean primitive) {

if (value != null) {
Class<? extends Object> valueClass = value.getClass();
if (valueClass.isArray()) {
Class<?> componentType = valueClass.getComponentType();
primitive = componentType.isPrimitive();
List<CodeExpression> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +23,8 @@
*/
public abstract class JavaConstant<T> extends BaseExpression implements CodeConstant, JavaExpression {

private final T value;
/** @see #getValue() */
protected final T value;

/**
* The constructor.
Expand Down Expand Up @@ -68,7 +70,8 @@ public JavaConstant<T> 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());
}
Expand All @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> {
public class JavaConstantBoolean extends JavaConstant<Boolean> {

/** {@link JavaConstantBoolean} for {@link Boolean#TRUE}. */
public static final JavaConstantBoolean TRUE = new JavaConstantBoolean(Boolean.TRUE);
Expand All @@ -35,7 +35,7 @@ public JavaConstant<Boolean> withValue(Boolean newValue) {
@Override
public String getSourceCode() {

return "Boolean.valueOf(" + getValue().toString() + ")";
return "Boolean.valueOf(" + this.value.toString() + ")";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Byte> {
public class JavaConstantByte extends JavaConstant<Byte> {

/**
* The constructor.
Expand All @@ -29,7 +29,7 @@ public JavaConstant<Byte> withValue(Byte newValue) {
@Override
public String getSourceCode() {

return "Byte.valueOf((byte) " + getValue().toString() + ")";
return "Byte.valueOf((byte) " + this.value.toString() + ")";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Character> {
public class JavaConstantCharacter extends JavaConstant<Character> {

/**
* The constructor.
Expand All @@ -31,7 +31,7 @@ public JavaConstant<Character> withValue(Character newValue) {
@Override
public String getSourceCode() {

return "Character.valueOf(" + JavaLiteralChar.toCharLiteral(getValue().charValue()) + ")";
return "Character.valueOf(" + JavaLiteralChar.toCharLiteral(this.value.charValue()) + ")";
}

/**
Expand Down
Loading

0 comments on commit df54e7b

Please sign in to comment.