示例#1
0
  /**
   * Returns the resolved generic exception types thrown by {@code constructor}.
   *
   * @param methodOrConstructor a method or constructor defined by this or any supertype.
   * @since 2.0
   */
  public List<TypeLiteral<?>> getExceptionTypes(Member methodOrConstructor) {
    Type[] genericExceptionTypes;

    if (methodOrConstructor instanceof Method) {
      Method method = (Method) methodOrConstructor;
      Assert.checkArgument(
          method.getDeclaringClass().isAssignableFrom(rawType),
          "%s is not defined by a supertype of %s",
          method,
          type);
      genericExceptionTypes = method.getGenericExceptionTypes();

    } else if (methodOrConstructor instanceof Constructor) {
      Constructor<?> constructor = (Constructor<?>) methodOrConstructor;
      Assert.checkArgument(
          constructor.getDeclaringClass().isAssignableFrom(rawType),
          "%s does not construct a supertype of %s",
          constructor,
          type);
      genericExceptionTypes = constructor.getGenericExceptionTypes();

    } else {
      throw new IllegalArgumentException("Not a method or a constructor: " + methodOrConstructor);
    }

    return resolveAll(genericExceptionTypes);
  }
  protected void loadClassDependencies(Class aClass) throws ClassNotFoundException {
    String name = aClass.getName();
    if (myVisited.add(aClass)) {
      try {
        for (Method method : aClass.getDeclaredMethods()) {
          loadTypeDependencies(method.getGenericReturnType());
          for (Type type : method.getGenericExceptionTypes()) {
            loadTypeDependencies(type);
          }
          for (Type type : method.getGenericParameterTypes()) {
            loadTypeDependencies(type);
          }
        }
        for (Constructor method : aClass.getDeclaredConstructors()) {
          for (Type type : method.getGenericExceptionTypes()) {
            loadTypeDependencies(type);
          }
          for (Type type : method.getGenericParameterTypes()) {
            loadTypeDependencies(type);
          }
        }

        for (Field field : aClass.getDeclaredFields()) {
          loadTypeDependencies(field.getGenericType());
        }

        Type superclass = aClass.getGenericSuperclass();
        if (superclass != null) {
          loadClassDependencies(aClass);
        }

        for (Type intf : aClass.getGenericInterfaces()) {
          loadTypeDependencies(intf);
        }

        aClass.getAnnotations();
        Package aPackage = aClass.getPackage();
        if (aPackage != null) {
          aPackage.getAnnotations();
        }
      } catch (LinkageError e) {
        throw new ClassNotFoundException(name);
      } catch (TypeNotPresentException e) {
        throw new ClassNotFoundException(name);
      }
    }
  }
 void init(Constructor constructor) {
   for (TypeVariable param : constructor.getTypeParameters()) {
     typeParameters.add(ElementFactory.getTypeParameterElement(param));
   }
   type = TypeMirrorFactory.get(constructor);
   enclosingElement = ElementFactory.get(constructor.getDeclaringClass());
   returnType = Typ.getNoType(TypeKind.VOID);
   Type[] genericParameterTypes = constructor.getGenericParameterTypes();
   Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
   int index = 0;
   for (Type param : genericParameterTypes) {
     parameters.add(ElementFactory.getVariableElement(param, parameterAnnotations[index++]));
   }
   varArgs = constructor.isVarArgs();
   for (Type type : constructor.getGenericExceptionTypes()) {
     thrownTypes.add(TypeMirrorFactory.get(type));
   }
 }
示例#4
0
  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);
    }
  }
 /** Generates the signature for the given constructor */
 private String signature(Constructor<?> constructor) {
   StringBuilder builder = new StringBuilder();
   if (constructor.getTypeParameters().length > 0) {
     builder.append('<');
     for (TypeVariable<?> typeVariable : constructor.getTypeParameters()) {
       builder.append(typeVariable.getName());
       for (java.lang.reflect.Type bound : typeVariable.getBounds()) {
         builder.append(':');
         visitType(bound, builder);
       }
     }
     builder.append('>');
   }
   builder.append('(');
   for (java.lang.reflect.Type paramType : constructor.getGenericParameterTypes()) {
     visitType(paramType, builder);
   }
   builder.append(")V");
   for (java.lang.reflect.Type exceptionType : constructor.getGenericExceptionTypes()) {
     builder.append('^');
     visitType(exceptionType, builder);
   }
   return builder.toString();
 }
示例#6
0
文件: Java5.java 项目: TTwxj/groovy
  public void configureClassNode(CompileUnit compileUnit, ClassNode classNode) {
    try {
      Class clazz = classNode.getTypeClass();
      Field[] fields = clazz.getDeclaredFields();
      for (Field f : fields) {
        ClassNode ret = makeClassNode(compileUnit, f.getGenericType(), f.getType());
        FieldNode fn = new FieldNode(f.getName(), f.getModifiers(), ret, classNode, null);
        setAnnotationMetaData(f.getAnnotations(), fn);
        classNode.addField(fn);
      }
      Method[] methods = clazz.getDeclaredMethods();
      for (Method m : methods) {
        ClassNode ret = makeClassNode(compileUnit, m.getGenericReturnType(), m.getReturnType());
        Parameter[] params =
            makeParameters(
                compileUnit,
                m.getGenericParameterTypes(),
                m.getParameterTypes(),
                m.getParameterAnnotations());
        ClassNode[] exceptions =
            makeClassNodes(compileUnit, m.getGenericExceptionTypes(), m.getExceptionTypes());
        MethodNode mn =
            new MethodNode(m.getName(), m.getModifiers(), ret, params, exceptions, null);
        mn.setSynthetic(m.isSynthetic());
        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(),
                ctor.getParameterAnnotations());
        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);
      }
    } catch (NoClassDefFoundError e) {
      throw new NoClassDefFoundError(
          "Unable to load class "
              + classNode.toString(false)
              + " due to missing dependency "
              + e.getMessage());
    } catch (MalformedParameterizedTypeException e) {
      throw new RuntimeException(
          "Unable to configure class node for class "
              + classNode.toString(false)
              + " due to malformed parameterized types",
          e);
    }
  }