Skip to content

Commit 62327a9

Browse files
committed
Allow an interface to be annoated with @GenIgnore(GenIgnore.PERMITTED_TYPE) to automatically promote a method that declares this type among parameters or return types.
1 parent 9fbd15d commit 62327a9

File tree

12 files changed

+242
-32
lines changed

12 files changed

+242
-32
lines changed

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/ClassModel.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,13 @@ private void traverseType(Element elem) {
454454
if (allowAnyJavaType) {
455455
anyJavaTypeMethods.put(elt, meth);
456456
} else {
457-
methodAnnotationsMap.put(meth.getName(), elt.getAnnotationMirrors().stream().map(annotationValueInfoFactory::processAnnotation).collect(Collectors.toList()));
458-
methods.put(elt, meth);
457+
boolean isAnyJavaType = TypeValidator.isAnyJavaType(meth);
458+
if (isAnyJavaType) {
459+
anyJavaTypeMethods.put(elt, meth);
460+
} else {
461+
methodAnnotationsMap.put(meth.getName(), elt.getAnnotationMirrors().stream().map(annotationValueInfoFactory::processAnnotation).collect(Collectors.toList()));
462+
methods.put(elt, meth);
463+
}
459464
}
460465
}
461466
});

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/TypeValidator.java

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package io.vertx.codegen.processor;
22

3-
import io.vertx.codegen.processor.type.ClassKind;
4-
import io.vertx.codegen.processor.type.ParameterizedTypeInfo;
5-
import io.vertx.codegen.processor.type.TypeInfo;
6-
import io.vertx.codegen.processor.type.TypeVariableInfo;
3+
import io.vertx.codegen.processor.type.*;
74

85
import javax.lang.model.element.Element;
96
import javax.lang.model.element.ElementKind;
@@ -21,6 +18,62 @@
2118
*/
2219
class TypeValidator {
2320

21+
static boolean isAnyJavaType(MethodInfo method) {
22+
for (ParamInfo param : method.getParams()) {
23+
if (isAnyJavaType(param.getType())) {
24+
return true;
25+
}
26+
}
27+
return isAnyJavaType(method.getReturnType());
28+
}
29+
30+
static boolean isAnyJavaType(TypeInfo type) {
31+
switch (type.getKind()) {
32+
case API:
33+
case BOXED_PRIMITIVE:
34+
case ENUM:
35+
case PRIMITIVE:
36+
case JSON_OBJECT:
37+
case JSON_ARRAY:
38+
case VOID:
39+
case OBJECT:
40+
case CLASS_TYPE:
41+
case STRING:
42+
case THROWABLE:
43+
return false;
44+
case MAP:
45+
ParameterizedTypeInfo mapType = (ParameterizedTypeInfo) type;
46+
return isAnyJavaType(mapType.getArg(1));
47+
case LIST:
48+
ParameterizedTypeInfo listType = (ParameterizedTypeInfo) type;
49+
return isAnyJavaType(listType.getArg(0));
50+
case SET:
51+
ParameterizedTypeInfo setType = (ParameterizedTypeInfo) type;
52+
return isAnyJavaType(setType.getArg(0));
53+
case FUTURE:
54+
ParameterizedTypeInfo futureType = (ParameterizedTypeInfo) type;
55+
return isAnyJavaType(futureType.getArg(0));
56+
case SUPPLIER:
57+
ParameterizedTypeInfo supplierType = (ParameterizedTypeInfo) type;
58+
return isAnyJavaType(supplierType.getArg(0));
59+
case HANDLER:
60+
ParameterizedTypeInfo handlerType = (ParameterizedTypeInfo) type;
61+
return isAnyJavaType(handlerType.getArg(0));
62+
case FUNCTION:
63+
ParameterizedTypeInfo functionType = (ParameterizedTypeInfo) type;
64+
return isAnyJavaType(functionType.getArg(0)) || isAnyJavaType(functionType.getArg(1));
65+
case OTHER:
66+
if (type instanceof ParameterizedTypeInfo) {
67+
return isAnyJavaType(type.getRaw());
68+
} else {
69+
ClassTypeInfo otherType = (ClassTypeInfo) type;
70+
return otherType.isPermitted();
71+
}
72+
default:
73+
throw new UnsupportedOperationException("" + type.getKind());
74+
}
75+
}
76+
2477
static void validateParamType(ExecutableElement elem, TypeInfo typeInfo, boolean allowAnyJavaType) {
2578
if (isValidNonCallableType(elem, typeInfo, true, false, true, allowAnyJavaType)) {
2679
return;
@@ -161,7 +214,14 @@ private static boolean isValidVertxGenTypeArgument(Element elem, TypeInfo arg, b
161214
}
162215

163216
private static boolean isValidOtherType(TypeInfo type, boolean allowAnyJavaType) {
164-
return allowAnyJavaType && type.getKind() == ClassKind.OTHER;
217+
if (type instanceof ClassTypeInfo) {
218+
allowAnyJavaType |= ((ClassTypeInfo)type).isPermitted();
219+
return allowAnyJavaType && type.getKind() == ClassKind.OTHER;
220+
} else if (type instanceof ParameterizedTypeInfo) {
221+
return isValidOtherType(type.getRaw(), allowAnyJavaType);
222+
} else {
223+
return allowAnyJavaType;
224+
}
165225
}
166226

167227
private static boolean isValidVertxGenInterface(Element elem, TypeInfo type, boolean allowParameterized, boolean allowAnyJavaType) {

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/type/ApiTypeInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ApiTypeInfo(
2323
boolean nullable,
2424
boolean proxyGen,
2525
DataObjectInfo dataObject) {
26-
super(ClassKind.API, fqcn, module, nullable, params, dataObject);
26+
super(ClassKind.API, fqcn, module, nullable, params, false, dataObject);
2727
this.concrete = concrete;
2828
this.proxyGen = proxyGen;
2929
this.handlerArg = handlerArg;

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/type/ClassTypeInfo.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ClassTypeInfo extends TypeInfo {
2323
Float.class, Double.class, Character.class};
2424
for (Class<?> boxe : boxes) {
2525
String name = boxe.getName();
26-
PRIMITIVES.put(name, new ClassTypeInfo(ClassKind.BOXED_PRIMITIVE, name, null, false, Collections.emptyList(), null));
26+
PRIMITIVES.put(name, new ClassTypeInfo(ClassKind.BOXED_PRIMITIVE, name, null, false, Collections.emptyList(), false, null));
2727
}
2828
}
2929

@@ -35,18 +35,24 @@ public class ClassTypeInfo extends TypeInfo {
3535
final boolean nullable;
3636
final List<TypeParamInfo.Class> params;
3737
final DataObjectInfo dataObject;
38+
final boolean permitted;
3839

39-
public ClassTypeInfo(ClassKind kind, String name, ModuleInfo module, boolean nullable, List<TypeParamInfo.Class> params, DataObjectInfo dataObject) {
40+
public ClassTypeInfo(ClassKind kind, String name, ModuleInfo module, boolean nullable, List<TypeParamInfo.Class> params, boolean permitted, DataObjectInfo dataObject) {
4041
this.kind = kind;
4142
this.name = name;
4243
this.simpleName = Helper.getSimpleName(name);
4344
this.packageName = Helper.getPackageName(name);
4445
this.module = module;
4546
this.nullable = nullable;
4647
this.params = params;
48+
this.permitted = permitted;
4749
this.dataObject = dataObject;
4850
}
4951

52+
public boolean isPermitted() {
53+
return permitted;
54+
}
55+
5056
public List<TypeParamInfo.Class> getParams() {
5157
return params;
5258
}

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/type/EnumTypeInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class EnumTypeInfo extends ClassTypeInfo {
1515

1616
public EnumTypeInfo(String fqcn, boolean gen, List<String> values,
1717
ModuleInfo module, boolean nullable, DataObjectInfo dataObject) {
18-
super(ClassKind.ENUM, fqcn, module, nullable, Collections.emptyList(), dataObject);
18+
super(ClassKind.ENUM, fqcn, module, nullable, Collections.emptyList(), false, dataObject);
1919

2020
this.gen = gen;
2121
this.values = values;

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/type/TypeMirrorFactory.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.vertx.codegen.processor.type;
22

33
import io.vertx.codegen.annotations.DataObject;
4+
import io.vertx.codegen.annotations.GenIgnore;
45
import io.vertx.codegen.annotations.ProxyGen;
56
import io.vertx.codegen.annotations.VertxGen;
67
import io.vertx.codegen.processor.*;
@@ -23,8 +24,8 @@
2324
public class TypeMirrorFactory {
2425

2526
public static final ModuleInfo VERTX_CORE_MOD = new ModuleInfo("io.vertx.core", "vertx", "io.vertx");
26-
public static final ClassTypeInfo JSON_OBJECT = new ClassTypeInfo(ClassKind.JSON_OBJECT, "io.vertx.core.json.JsonObject", VERTX_CORE_MOD, false, Collections.emptyList(), null);
27-
public static final ClassTypeInfo STRING = new ClassTypeInfo(ClassKind.STRING, "java.lang.String", null, false, Collections.emptyList(), null);
27+
public static final ClassTypeInfo JSON_OBJECT = new ClassTypeInfo(ClassKind.JSON_OBJECT, "io.vertx.core.json.JsonObject", VERTX_CORE_MOD, false, Collections.emptyList(), false, null);
28+
public static final ClassTypeInfo STRING = new ClassTypeInfo(ClassKind.STRING, "java.lang.String", null, false, Collections.emptyList(), false, null);
2829

2930
final Elements elementUtils;
3031
final Types typeUtils;
@@ -133,7 +134,7 @@ public TypeInfo create(TypeUse use, DeclaredType type, boolean checkTypeArgs) {
133134
if (kind == ClassKind.BOXED_PRIMITIVE) {
134135
raw = ClassTypeInfo.PRIMITIVES.get(fqcn);
135136
if (nullable) {
136-
raw = new ClassTypeInfo(raw.kind, raw.name, raw.module, true, raw.params, null);
137+
raw = new ClassTypeInfo(raw.kind, raw.name, raw.module, true, raw.params, false, null);
137138
}
138139
} else {
139140
MapperInfo serializer = serializers.get(fqcn);
@@ -152,6 +153,13 @@ public TypeInfo create(TypeUse use, DeclaredType type, boolean checkTypeArgs) {
152153
dataObject = new DataObjectInfo(annotated, serializer, deserializer);
153154
}
154155

156+
boolean permitted;
157+
if (elt.getAnnotation(GenIgnore.class) != null) {
158+
permitted = true;
159+
} else {
160+
permitted = false;
161+
}
162+
155163
List<TypeParamInfo.Class> typeParams = createTypeParams(type);
156164
if (kind == ClassKind.API) {
157165
VertxGen genAnn = elt.getAnnotation(VertxGen.class);
@@ -173,7 +181,7 @@ public TypeInfo create(TypeUse use, DeclaredType type, boolean checkTypeArgs) {
173181
}
174182
raw = new ApiTypeInfo(fqcn, genAnn.concrete(), typeParams, handlerArg, module, nullable, proxyGen, dataObject);
175183
} else {
176-
raw = new ClassTypeInfo(kind, fqcn, module, nullable, typeParams, dataObject);
184+
raw = new ClassTypeInfo(kind, fqcn, module, nullable, typeParams, permitted, dataObject);
177185
}
178186
}
179187
return raw;

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/type/TypeReflectionFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.vertx.codegen.processor.type;
22

3+
import io.vertx.codegen.annotations.GenIgnore;
34
import io.vertx.codegen.processor.Helper;
45
import io.vertx.codegen.processor.MapperKind;
56
import io.vertx.codegen.processor.ModuleInfo;
@@ -81,14 +82,21 @@ public static TypeInfo create(Type type) {
8182
return new ApiTypeInfo(fqcn, true, typeParams, handlerArg != null ? create(handlerArg) : null, module, false, false, null);
8283
} else {
8384
DataObjectInfo dataObject;
85+
boolean permitted;
8486
if (classType.getDeclaredAnnotation(DataObject.class) != null) {
8587
MapperInfo serializer = getDataObjectSerializer(classType);
8688
MapperInfo deserializer = getDataObjectDeserializer(classType);
8789
dataObject = new DataObjectInfo(true, serializer, deserializer);
90+
permitted = false;
8891
} else {
8992
dataObject = null;
93+
if (classType.getDeclaredAnnotation(GenIgnore.class) != null) {
94+
permitted = true;
95+
} else {
96+
permitted = false;
97+
}
9098
}
91-
return new ClassTypeInfo(kind, fqcn, module, false, typeParams, dataObject);
99+
return new ClassTypeInfo(kind, fqcn, module, false, typeParams, permitted, dataObject);
92100
}
93101
}
94102
}

vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/type/TypeVariableInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public boolean isNullable() {
4848

4949
@Override
5050
public TypeInfo getErased() {
51-
return new ClassTypeInfo(ClassKind.OBJECT, Object.class.getName(), null, false, Collections.emptyList(), null);
51+
return new ClassTypeInfo(ClassKind.OBJECT, Object.class.getName(), null, false, Collections.emptyList(), false, null);
5252
}
5353

5454
@Override

0 commit comments

Comments
 (0)