private void configureAnnotationFromDefinition(AnnotationNode definition, AnnotationNode root) { ClassNode type = definition.getClassNode(); if (!type.isResolved()) return; Class clazz = type.getTypeClass(); if (clazz == Retention.class) { Expression exp = definition.getMember("value"); if (!(exp instanceof PropertyExpression)) return; PropertyExpression pe = (PropertyExpression) exp; String name = pe.getPropertyAsString(); RetentionPolicy policy = RetentionPolicy.valueOf(name); setRetentionPolicy(policy, root); } else if (clazz == Target.class) { Expression exp = definition.getMember("value"); if (!(exp instanceof ListExpression)) return; ListExpression le = (ListExpression) exp; int bitmap = 0; for (Expression e : le.getExpressions()) { PropertyExpression element = (PropertyExpression) e; String name = element.getPropertyAsString(); ElementType value = ElementType.valueOf(name); bitmap |= getElementCode(value); } root.setAllowedTargets(bitmap); } }
public void configureAnnotation(AnnotationNode node) { ClassNode type = node.getClassNode(); List<AnnotationNode> annotations = type.getAnnotations(); for (AnnotationNode an : annotations) { configureAnnotationFromDefinition(an, node); } configureAnnotationFromDefinition(node, node); }
private ClassNode makeClassNode(CompileUnit cu, Type t, Class c) { ClassNode back = null; if (cu != null) back = cu.getClass(c.getName()); if (back == null) back = ClassHelper.make(c); if (!(t instanceof Class)) { ClassNode front = configureType(t); front.setRedirect(back); return front; } return back; }
private void makeInterfaceTypes(CompileUnit cu, ClassNode classNode, Class clazz) { Type[] interfaceTypes = clazz.getGenericInterfaces(); if (interfaceTypes.length == 0) { classNode.setInterfaces(ClassNode.EMPTY_ARRAY); } else { Class[] interfaceClasses = clazz.getInterfaces(); ClassNode[] ret = new ClassNode[interfaceTypes.length]; for (int i = 0; i < interfaceTypes.length; i++) { ret[i] = makeClassNode(cu, interfaceTypes[i], interfaceClasses[i]); } classNode.setInterfaces(ret); } }
private GenericsType[] configureTypeArguments(Type[] ta) { if (ta.length == 0) return null; GenericsType[] gts = new GenericsType[ta.length]; for (int i = 0; i < ta.length; i++) { ClassNode t = configureType(ta[i]); if (ta[i] instanceof WildcardType) { GenericsType[] gen = t.getGenericsTypes(); gts[i] = gen[0]; } else { gts[i] = new GenericsType(t); } } return gts; }
private GenericsType configureTypeVariableDefinition(TypeVariable tv) { ClassNode base = configureTypeVariableReference(tv); ClassNode redirect = base.redirect(); base.setRedirect(null); Type[] tBounds = tv.getBounds(); GenericsType gt; if (tBounds.length == 0) { gt = new GenericsType(base); } else { ClassNode[] cBounds = configureTypes(tBounds); gt = new GenericsType(base, cBounds, null); gt.setName(base.getName()); gt.setPlaceholder(true); } base.setRedirect(redirect); return gt; }
private ClassNode configureWildcardType(WildcardType wildcardType) { ClassNode base = ClassHelper.makeWithoutCaching("?"); base.setRedirect(ClassHelper.OBJECT_TYPE); // TODO: more than one lower bound for wildcards? ClassNode[] lowers = configureTypes(wildcardType.getLowerBounds()); ClassNode lower = null; // TODO: is it safe to remove this? What was the original intention? if (lower != null) lower = lowers[0]; ClassNode[] upper = configureTypes(wildcardType.getUpperBounds()); GenericsType t = new GenericsType(base, upper, lower); t.setWildcard(true); ClassNode ref = ClassHelper.makeWithoutCaching(Object.class, false); ref.setGenericsTypes(new GenericsType[] {t}); return ref; }
private ClassNode configureTypeVariableReference(TypeVariable tv) { ClassNode cn = ClassHelper.makeWithoutCaching(tv.getName()); cn.setGenericsPlaceHolder(true); ClassNode cn2 = ClassHelper.makeWithoutCaching(tv.getName()); cn2.setGenericsPlaceHolder(true); GenericsType[] gts = new GenericsType[] {new GenericsType(cn2)}; cn.setGenericsTypes(gts); cn.setRedirect(ClassHelper.OBJECT_TYPE); return cn; }
public void configureClassNode(CompileUnit compileUnit, ClassNode classNode) { Class clazz = classNode.getTypeClass(); Field[] fields = clazz.getDeclaredFields(); for (Field f : fields) { ClassNode ret = makeClassNode(compileUnit, f.getGenericType(), f.getType()); classNode.addField(f.getName(), f.getModifiers(), ret, null); } Method[] methods = clazz.getDeclaredMethods(); for (Method m : methods) { ClassNode ret = makeClassNode(compileUnit, m.getGenericReturnType(), m.getReturnType()); Parameter[] params = makeParameters(compileUnit, m.getGenericParameterTypes(), m.getParameterTypes()); ClassNode[] exceptions = makeClassNodes(compileUnit, m.getGenericExceptionTypes(), m.getExceptionTypes()); MethodNode mn = new MethodNode(m.getName(), m.getModifiers(), ret, params, exceptions, null); setMethodDefaultValue(mn, m); setAnnotationMetaData(m.getAnnotations(), mn); mn.setGenericsTypes(configureTypeVariable(m.getTypeParameters())); classNode.addMethod(mn); } Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor ctor : constructors) { Parameter[] params = makeParameters(compileUnit, ctor.getGenericParameterTypes(), ctor.getParameterTypes()); ClassNode[] exceptions = makeClassNodes(compileUnit, ctor.getGenericExceptionTypes(), ctor.getExceptionTypes()); classNode.addConstructor(ctor.getModifiers(), params, exceptions, null); } Class sc = clazz.getSuperclass(); if (sc != null) classNode.setUnresolvedSuperClass( makeClassNode(compileUnit, clazz.getGenericSuperclass(), sc)); makeInterfaceTypes(compileUnit, classNode, clazz); setAnnotationMetaData(classNode.getTypeClass().getAnnotations(), classNode); PackageNode packageNode = classNode.getPackage(); if (packageNode != null) { setAnnotationMetaData(classNode.getTypeClass().getPackage().getAnnotations(), packageNode); } }
private void setGenericsTypes(ClassNode cn) { TypeVariable[] tvs = cn.getTypeClass().getTypeParameters(); GenericsType[] gts = configureTypeVariable(tvs); cn.setGenericsTypes(gts); }
private ClassNode configureParameterizedType(ParameterizedType parameterizedType) { ClassNode base = configureType(parameterizedType.getRawType()); GenericsType[] gts = configureTypeArguments(parameterizedType.getActualTypeArguments()); base.setGenericsTypes(gts); return base; }
private ClassNode configureGenericArray(GenericArrayType genericArrayType) { Type component = genericArrayType.getGenericComponentType(); ClassNode node = configureType(component); return node.makeArray(); }