private String genTypeArg(TypeVariableInfo typeVar, MethodInfo method) { if (typeVar.isClassParam()) { return "__typeArg_" + typeVar.getParam().getIndex(); } else { TypeArgExpression typeArg = method.resolveTypeArg(typeVar); if (typeArg != null) { if (typeArg.isClassType()) { return "io.vertx.lang.rx.TypeArg.of(" + typeArg.getParam().getName() + ")"; } else { return typeArg.getParam().getName() + ".__typeArg_" + typeArg.getIndex(); } } } return null; }
private boolean isLegalClassTypeParam(ExecutableElement elt, TypeInfo type) { if (type.getKind() == ClassKind.CLASS_TYPE && type.isParameterized()) { ParameterizedTypeInfo parameterized = (ParameterizedTypeInfo) type; TypeInfo arg = parameterized.getArg(0); if (arg.isVariable()) { TypeVariableInfo variable = (TypeVariableInfo) arg; for (TypeParameterElement typeParamElt : elt.getTypeParameters()) { if (typeParamElt.getSimpleName().toString().equals(variable.getName())) { return true; } } } } return false; }
public TypeVariableInfo create(TypeUse use, TypeVariable type) { TypeParameterElement elt = (TypeParameterElement) type.asElement(); TypeParamInfo param = TypeParamInfo.create(elt); return new TypeVariableInfo(param, use != null && use.isNullable(), elt.getSimpleName().toString()); }
private TypeVariableInfo assertTypeVariable(TypeInfo type, String expectedName) { assertEquals(TypeVariableInfo.class, type.getClass()); TypeVariableInfo classType = (TypeVariableInfo) type; assertEquals(ClassKind.OBJECT, classType.getKind()); assertEquals(expectedName, classType.getName()); return classType; }
checkParam(params1.get(0), "t", new TypeLiteral<T>(){}); assertTrue(params1.get(0).getType() instanceof TypeVariableInfo); assertEquals(t, ((TypeVariableInfo)params1.get(0).getType()).getParam()); assertTrue(((TypeVariableInfo)params1.get(0).getType()).isClassParam()); assertFalse(((TypeVariableInfo)params1.get(0).getType()).isMethodParam()); assertNull(methods.get(0).resolveClassTypeParam((TypeVariableInfo) params1.get(0).getType())); checkParam(params1.get(1), "handler", new TypeLiteral<Handler<T>>(){}); checkParam(params2.get(0), "r", new TypeLiteral<R>(){}); assertTrue(params2.get(0).getType() instanceof TypeVariableInfo); assertEquals(methods.get(1).getTypeParams().get(0), ((TypeVariableInfo) params2.get(0).getType()).getParam()); assertFalse(((TypeVariableInfo) params2.get(0).getType()).isClassParam()); assertTrue(((TypeVariableInfo) params2.get(0).getType()).isMethodParam()); assertNull(methods.get(1).resolveClassTypeParam((TypeVariableInfo) params2.get(0).getType())); checkParam(params2.get(1), "handler", new TypeLiteral<Handler<R>>(){});
public TypeArgExpression resolveTypeArg(TypeVariableInfo typeVar) { for (TypeParamInfo.Method typeParam : typeParams) { if (typeParam.getName().equals(typeVar.getName())) { for (ParamInfo param : params) { if (param.getType().getKind() == ClassKind.CLASS_TYPE && if (arg_.isVariable()) { TypeVariableInfo ttt = (TypeVariableInfo) arg_; if (ttt.getParam().equals(typeParam)) { return new TypeArgExpression(TypeArgExpression.CLASS_TYPE_ARG, ttt, param, 0); if (i instanceof TypeVariableInfo) { TypeVariableInfo tt = (TypeVariableInfo) i; if (tt.getParam().equals(typeParam)) { return new TypeArgExpression(TypeArgExpression.API_ARG, tt, param, index);
private boolean isReified(TypeVariableInfo typeVar, MethodInfo method) { if (typeVar.isClassParam()) { return true; } else { TypeArgExpression typeArg = method.resolveTypeArg(typeVar); return typeArg != null && typeArg.isClassType(); } }
@Test public void testParameterizedVariableSuperType() throws Exception { ClassModel model = new GeneratorHelper().generateClass(InterfaceWithParameterizedVariableSupertype.class); assertEquals(InterfaceWithParameterizedVariableSupertype.class.getName() + "<T>", model.getIfaceFQCN()); assertEquals(InterfaceWithParameterizedVariableSupertype.class.getSimpleName(), model.getIfaceSimpleName()); assertEquals(1, model.getReferencedTypes().size()); assertTrue(model.getReferencedTypes().contains(GenericInterfaceInfo)); assertEquals(1, model.getSuperTypes().size()); assertTrue(model.getSuperTypes().contains(TypeReflectionFactory.create(InterfaceWithParameterizedVariableSupertype.class.getGenericInterfaces()[0]))); List<TypeInfo> superTypeArgs = model.getSuperTypeArguments(); assertEquals(1, superTypeArgs.size()); TypeVariableInfo superTypeArg = (TypeVariableInfo) superTypeArgs.get(0); assertEquals("T", superTypeArg.getName()); assertTrue(superTypeArg.isClassParam()); }
@Test public void testTypeParam() throws Exception { doTest(TypeParamHolder.class, map -> { TypeParamInfo.Class classTypeParam = (TypeParamInfo.Class) assertTypeVariable(map.get("classTypeParam"), "ClassTypeParam").getParam(); assertEquals("ClassTypeParam", classTypeParam.getName()); TypeParamInfo.Method methodTypeParam = (TypeParamInfo.Method) assertTypeVariable(map.get("methodTypeParam"), "MethodTypeParam").getParam(); assertEquals("MethodTypeParam", methodTypeParam.getName()); }); }
private boolean isReified(TypeVariableInfo typeVar, MethodInfo method) { if (typeVar.isClassParam()) { return true; } else { TypeArgExpression typeArg = method.resolveTypeArg(typeVar); return typeArg != null && typeArg.isClassType(); } }
@Test public void testApi() throws Exception { doTest(ApiHolder.class, map -> { ApiTypeInfo api = assertApi(map.get("api"), ApiObject.class.getName()); assertEquals(ApiObject.class.getName(), api.getName()); ParameterizedTypeInfo apiParameterizedByClass = assertParameterized(map.get("apiParameterizedByClass"), GenericInterface.class.getName() + "<java.lang.String>", ClassKind.API); assertClass(apiParameterizedByClass.getArg(0), "java.lang.String", ClassKind.STRING); ParameterizedTypeInfo apiParameterizedByClassTypeParam = assertParameterized(map.get("apiParameterizedByClassTypeParam"), GenericInterface.class.getName() + "<ClassTypeParam>", ClassKind.API); TypeParamInfo.Class classTypeParam = (TypeParamInfo.Class) assertTypeVariable(apiParameterizedByClassTypeParam.getArg(0), "ClassTypeParam").getParam(); assertEquals("ClassTypeParam", classTypeParam.getName()); ParameterizedTypeInfo apiParameterizedByMethodTypeParam = assertParameterized(map.get("apiParameterizedByMethodTypeParam"), GenericInterface.class.getName() + "<MethodTypeParam>", ClassKind.API); TypeParamInfo.Method methodTypeParam = (TypeParamInfo.Method) assertTypeVariable(apiParameterizedByMethodTypeParam.getArg(0), "MethodTypeParam").getParam(); assertEquals("MethodTypeParam", methodTypeParam.getName()); }); }
private String genTypeArg(TypeVariableInfo typeVar, MethodInfo method) { if (typeVar.isClassParam()) { return "__typeArg_" + typeVar.getParam().getIndex(); } else { TypeArgExpression typeArg = method.resolveTypeArg(typeVar); if (typeArg != null) { if (typeArg.isClassType()) { return "io.vertx.lang.rx.TypeArg.of(" + typeArg.getParam().getName() + ")"; } else { return typeArg.getParam().getName() + ".__typeArg_" + typeArg.getIndex(); } } } return null; }
if (unwrappedType.isVariable()) { TypeVariableInfo type = (TypeVariableInfo) unwrappedType; if (type.isClassParam()) { writer.format("j_%s.unwrap(%s)", unwrappedType.getName(), unwrappedName); } else {
assertEquals(col, ofMethodTypeParam.getRaw()); assertEquals(1 + typeParamIndex, ofMethodTypeParam.getArgs().size()); TypeParamInfo.Method methodTypeParam = (TypeParamInfo.Method) ((TypeVariableInfo) ofMethodTypeParam.getArg(typeParamIndex)).getParam(); assertEquals("MethodTypeParam", methodTypeParam.getName()); ParameterizedTypeInfo ofDataObject = (ParameterizedTypeInfo) map.get(colType + "OfDataObject");
@Test public void testMethodWithTypeVarParamByGenericType() throws Exception { Runnable test = () -> { try { ClassModel model = new GeneratorHelper().generateClass(MethodWithTypeVarParamByGenericType.class); MethodInfo meth = model.getMethods().get(0); ParamInfo param = meth.getParam(0); ParameterizedTypeInfo handler = (ParameterizedTypeInfo) param.getType(); assertEquals(Handler.class.getName(), handler.getRaw().getName()); ParameterizedTypeInfo genericInt2 = (ParameterizedTypeInfo) handler.getArg(0); assertEquals(GenericInterface2.class.getName(), genericInt2.getRaw().getName()); TypeVariableInfo k = (TypeVariableInfo) genericInt2.getArg(0); assertEquals("K", k.getName()); TypeVariableInfo v = (TypeVariableInfo) genericInt2.getArg(1); assertEquals("V", v.getName()); } catch (Exception e) { throw new AssertionError(e); } }; blacklist(test, Stream.of(WriteStream.class)); test.run(); }
java.lang.reflect.TypeVariable typeVar = (java.lang.reflect.TypeVariable) type; TypeParamInfo param = TypeParamInfo.create(typeVar); return new TypeVariableInfo(param, false, ((java.lang.reflect.TypeVariable) type).getName()); } else { throw new IllegalArgumentException("Unsupported type " + type);
if (param.getType().isVariable() && ((TypeVariableInfo) param.getType()).isClassParam()) { writer.format("j_%s.accept(__args[%s])", param.getType().getName(), cnt); } else {
@Test public void testGenericInterfaceExtendingReadStreamAndWriteStream() throws Exception { ClassModel model = new GeneratorHelper().generateClass(GenericInterfaceExtentingReadStreamAndWriteStream.class); ApiTypeInfo apiType = (ApiTypeInfo) model.getType().getRaw(); assertTrue(apiType.isReadStream()); TypeVariableInfo readStreamArg = (TypeVariableInfo) apiType.getReadStreamArg(); assertEquals("U", readStreamArg.getName()); assertTrue(apiType.isWriteStream()); TypeVariableInfo writeStreamArg = (TypeVariableInfo) apiType.getWriteStreamArg(); assertEquals("U", writeStreamArg.getName()); assertFalse(apiType.isHandler()); }
@Test public void testGenericInterfaceExtendingReadStream() throws Exception { ClassModel model = new GeneratorHelper().generateClass(GenericInterfaceExtentingReadStream.class); ApiTypeInfo apiType = (ApiTypeInfo) model.getType().getRaw(); assertTrue(apiType.isReadStream()); TypeVariableInfo readStreamArg = (TypeVariableInfo) apiType.getReadStreamArg(); assertEquals("U", readStreamArg.getName()); assertFalse(apiType.isWriteStream()); assertNull(apiType.getWriteStreamArg()); assertFalse(apiType.isHandler()); }
@Test public void testGenericInterfaceExtendingWriteStream() throws Exception { ClassModel model = new GeneratorHelper().generateClass(GenericInterfaceExtentingWriteStream.class); ApiTypeInfo apiType = (ApiTypeInfo) model.getType().getRaw(); assertFalse(apiType.isReadStream()); assertNull(apiType.getReadStreamArg()); assertTrue(apiType.isWriteStream()); TypeVariableInfo writeStreamArg = (TypeVariableInfo) apiType.getWriteStreamArg(); assertEquals("U", writeStreamArg.getName()); assertFalse(apiType.isHandler()); }