@SuppressWarnings("rawtypes") private Type getGenericParameter(Class<?> cl, java.lang.reflect.Type genericType, int i) { java.lang.reflect.Type parameter = ReflectionUtils.getTypeParameter(genericType, i); if (parameter instanceof TypeVariable) { TypeVariable variable = (TypeVariable) parameter; Type rv = get(ReflectionUtils.getTypeParameterAsClass(genericType, i), null, parameter); return new TypeExtends(variable.getName(), rv); } else if (parameter instanceof WildcardType && ((WildcardType) parameter).getUpperBounds()[0].equals(Object.class) && ((WildcardType) parameter).getLowerBounds().length == 0) { return ANY; } else { Type rv = get(ReflectionUtils.getTypeParameterAsClass(genericType, i), null, parameter); if (parameter instanceof WildcardType) { rv = new TypeExtends(rv); } return rv; } }
private static String getVarName(Type type) { if (type instanceof TypeExtends) { return ((TypeExtends) type).getVarName(); } else if (type instanceof TypeSuper) { return ((TypeSuper) type).getVarName(); } else { return null; } }
@Test public void comparableEntity() { Type type = factory.getEntityType(ComparableEntity.class); //ComparableEntity<T extends Comparable<? super T>> implements Serializable assertEquals(1, type.getParameters().size()); TypeExtends t = (TypeExtends) type.getParameters().get(0); assertEquals("T", t.getVarName()); assertEquals(1, t.getParameters().size()); }
@Override public String getGenericName(boolean asArgType) { return getGenericName(asArgType, Collections.<String> emptySet(), Collections.<String> emptySet()); }
public Type getQueryType(Type type, EntityType model, Type exprType, boolean raw, boolean rawParameters, boolean extend) { TypeCategory category = type.getCategory(); if (raw && category != TypeCategory.ENTITY && category != TypeCategory.CUSTOM) { return exprType; } else if (category == TypeCategory.STRING || category == TypeCategory.BOOLEAN) { return exprType; } else { if (rawParameters) { type = new SimpleType(type); } if (!type.isFinal() && extend) { type = new TypeExtends(type); } return new SimpleType(exprType, type); } }
@Test public void base_base2() throws SecurityException, NoSuchFieldException { TypeFactory typeFactory = new TypeFactory(); Field field = Base.class.getDeclaredField("base2"); Type type = typeFactory.get(field.getType(), field.getGenericType()); assertEquals(2, type.getParameters().size()); assertNull(((TypeExtends) type.getParameters().get(0)).getVarName()); assertNull(((TypeExtends) type.getParameters().get(1)).getVarName()); }
@Test public void comparableEntity() { Type type = factory.getEntityType(ComparableEntity.class); //ComparableEntity<T extends Comparable<? super T>> implements Serializable assertEquals(1, type.getParameters().size()); TypeExtends t = (TypeExtends) type.getParameters().get(0); assertEquals("T", t.getVarName()); assertEquals(1, t.getParameters().size()); }
value = new TypeSuper(tv.getName(), value); } else { value = new TypeExtends(tv.getName(), value);
@Test public void abstractEntity_code() throws SecurityException, NoSuchFieldException { Field field = EmbeddedTest.AbstractEntity.class.getDeclaredField("code"); Type type = factory.get(field.getType(), field.getGenericType()); assertTrue(type instanceof TypeExtends); assertEquals("C", ((TypeExtends) type).getVarName()); }
@Test public void ComparableEntity() { Type type = factory.getEntityType(ComparableEntity.class); //ComparableEntity<T extends Comparable<? super T>> implements Serializable assertEquals(1, type.getParameters().size()); TypeExtends t = (TypeExtends)type.getParameters().get(0); assertEquals("T", t.getVarName()); assertEquals(1, t.getParameters().size()); }
@Override public Parameter apply(Parameter p) { Type type; if (!asExpr) { type = typeMappings.getExprType( p.getType(), model, false, false, true); } else if (p.getType().isFinal()) { type = new ClassType(Expression.class, p.getType()); } else { type = new ClassType(Expression.class, new TypeExtends(p.getType())); } return new Parameter(p.getName(), type); } });
@Test public void generics_typeVariable() { Type type = factory.getEntityType(Generic2Test.AbstractCollectionAttribute.class); assertEquals(TypeExtends.class, type.getParameters().get(0).getClass()); TypeExtends t = (TypeExtends) type.getParameters().get(0); assertEquals("T", t.getVarName()); }
@Override public Parameter apply(Parameter p) { Type type; if (!asExpr) { type = typeMappings.getExprType(p.getType(), model, false, false, true); } else if (p.getType().isFinal()) { type = new ClassType(Expression.class, p.getType()); } else { type = new ClassType(Expression.class, new TypeExtends(p.getType())); } return new Parameter(p.getName(), type); } });
@Test public void generics_wildcard() throws SecurityException, NoSuchFieldException { Field field = DefaultQueryMetadata.class.getDeclaredField("exprInJoins"); Type type = factory.get(field.getType(), field.getGenericType()); assertEquals(TypeCategory.SET, type.getCategory()); Type parameter = type.getParameters().get(0); assertEquals(Expression.class, parameter.getJavaClass()); parameter = parameter.getParameters().get(0); assertEquals(TypeExtends.class, parameter.getClass()); assertNull(((TypeExtends) parameter).getVarName()); }
@Override public Type visitTypeVariable(TypeVariable typeVariable, Boolean p) { String varName = typeVariable.toString(); if (typeVariable.getUpperBound() != null) { Type type = visit(typeVariable.getUpperBound(), p); return new TypeExtends(varName, type); } else if (typeVariable.getLowerBound() != null && !(typeVariable.getLowerBound() instanceof NullType)) { Type type = visit(typeVariable.getLowerBound(), p); return new TypeSuper(varName, type); } else { return null; } }
@Test public void AbstractEntity_Code() throws SecurityException, NoSuchFieldException { Field field = EmbeddedTest.AbstractEntity.class.getDeclaredField("code"); Type type = factory.get(field.getType(), field.getGenericType()); assertTrue(type instanceof TypeExtends); assertEquals("C", ((TypeExtends)type).getVarName()); }
@Override public Type visitWildcard(WildcardType wildcardType, Boolean p) { if (wildcardType.getExtendsBound() != null) { Type type = visit(wildcardType.getExtendsBound(), p); return new TypeExtends(type); } else if (wildcardType.getSuperBound() != null) { Type type = visit(wildcardType.getSuperBound(), p); return new TypeSuper(type); } else { return null; } }
@Test public void abstractEntity_code() throws SecurityException, NoSuchFieldException { Field field = EmbeddedTest.AbstractEntity.class.getDeclaredField("code"); Type type = factory.get(field.getType(), field.getGenericType()); assertTrue(type instanceof TypeExtends); assertEquals("C", ((TypeExtends) type).getVarName()); }
public ExtendedTypeFactory( ProcessingEnvironment env, Set<Class<? extends Annotation>> annotations, TypeMappings typeMappings, QueryTypeFactory queryTypeFactory, Function<EntityType, String> variableNameFunction) { this.env = env; this.defaultType = new TypeExtends(Types.OBJECT); this.entityAnnotations = annotations; this.objectType = getErasedType(Object.class); this.numberType = getErasedType(Number.class); this.comparableType = getErasedType(Comparable.class); this.collectionType = getErasedType(Collection.class); this.listType = getErasedType(List.class); this.setType = getErasedType(Set.class); this.mapType = getErasedType(Map.class); this.typeMappings = typeMappings; this.queryTypeFactory = queryTypeFactory; this.variableNameFunction = variableNameFunction; }
@Test public void Base_base2() throws SecurityException, NoSuchFieldException { TypeFactory typeFactory = new TypeFactory(); Field field = Base.class.getDeclaredField("base2"); Type type = typeFactory.get(field.getType(), field.getGenericType()); assertEquals(2, type.getParameters().size()); assertNull(((TypeExtends)type.getParameters().get(0)).getVarName()); assertNull(((TypeExtends)type.getParameters().get(1)).getVarName()); }