/**
   * Resolve current method generic return type to a {@link GenericMetadataSupport}.
   *
   * @param method Method to resolve the return type.
   * @return {@link GenericMetadataSupport} representing this generic return type.
   */
  public GenericMetadataSupport resolveGenericReturnType(Method method) {
    Type genericReturnType = method.getGenericReturnType();
    // logger.log("Method '" + method.toGenericString() + "' has return type : " +
    // genericReturnType.getClass().getInterfaces()[0].getSimpleName() + " : " + genericReturnType);

    if (genericReturnType instanceof Class) {
      return new NotGenericReturnTypeSupport(genericReturnType);
    }
    if (genericReturnType instanceof ParameterizedType) {
      return new ParameterizedReturnType(
          this, method.getTypeParameters(), (ParameterizedType) method.getGenericReturnType());
    }
    if (genericReturnType instanceof TypeVariable) {
      return new TypeVariableReturnType(
          this, method.getTypeParameters(), (TypeVariable) genericReturnType);
    }

    throw new MockitoException(
        "Ouch, it shouldn't happen, type '"
            + genericReturnType.getClass().getCanonicalName()
            + "' on method : '"
            + method.toGenericString()
            + "' is not supported : "
            + genericReturnType);
  }
Example #2
0
 private Method findBoundsMethod(Class<?> type) {
   for (Method method : type.getDeclaredMethods()) {
     if (method.getAnnotation(UIBounds.class) != null
         && method.getTypeParameters().length == 0
         && method.getReturnType() == Rectangle.class) {
       return method;
     }
   }
   Class<?> supertype = type.getSuperclass();
   if (supertype != null && supertype != Widget.class) {
     Method method = findBoundsMethod(supertype);
     if (method != null) {
       return method;
     }
   }
   for (Class<?> anInterface : type.getInterfaces()) {
     Method method = findBoundsMethod(anInterface);
     if (method != null) {
       return method;
     }
   }
   try {
     Method method = type.getDeclaredMethod("getBounds");
     if (method.getReturnType() == Rectangle.class) {
       return method;
     }
   } catch (Exception e) {
   }
   return null;
 }
  private void validateRuleMethod(
      MethodRuleDefinition<?, ?> ruleDefinition,
      Method ruleMethod,
      ValidationProblemCollector problems) {
    if (Modifier.isPrivate(ruleMethod.getModifiers())) {
      problems.add(ruleMethod, "A rule method cannot be private");
    }
    if (Modifier.isAbstract(ruleMethod.getModifiers())) {
      problems.add(ruleMethod, "A rule method cannot be abstract");
    }

    if (ruleMethod.getTypeParameters().length > 0) {
      problems.add(ruleMethod, "Cannot have type variables (i.e. cannot be a generic method)");
    }

    // TODO validations on method: synthetic, bridge methods, varargs, abstract, native
    ModelType<?> returnType = ModelType.returnType(ruleMethod);
    if (returnType.isRawClassOfParameterizedType()) {
      problems.add(
          ruleMethod,
          "Raw type "
              + returnType
              + " used for return type (all type parameters must be specified of parameterized type)");
    }

    for (int i = 0; i < ruleDefinition.getReferences().size(); i++) {
      ModelReference<?> reference = ruleDefinition.getReferences().get(i);
      if (reference.getType().isRawClassOfParameterizedType()) {
        problems.add(
            ruleMethod,
            "Raw type "
                + reference.getType()
                + " used for parameter "
                + (i + 1)
                + " (all type parameters must be specified of parameterized type)");
      }
      if (reference.getPath() != null) {
        try {
          ModelPath.validatePath(reference.getPath().toString());
        } catch (Exception e) {
          problems.add(
              ruleDefinition,
              "The declared model element path '"
                  + reference.getPath()
                  + "' used for parameter "
                  + (i + 1)
                  + " is not a valid path",
              e);
        }
      }
    }
  }
Example #4
0
 private static boolean isValidMethod(InjectableMethod injectableMethod, Errors errors) {
   boolean result = true;
   if (injectableMethod.jsr330) {
     Method method = injectableMethod.method;
     if (Modifier.isAbstract(method.getModifiers())) {
       errors.cannotInjectAbstractMethod(method);
       result = false;
     }
     if (method.getTypeParameters().length > 0) {
       errors.cannotInjectMethodWithTypeParameters(method);
       result = false;
     }
   }
   return result;
 }
Example #5
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);
    }
  }
 void init(Method method) {
   for (TypeVariable param : method.getTypeParameters()) {
     typeParameters.add(ElementFactory.getTypeParameterElement(param));
   }
   type = TypeMirrorFactory.get(method);
   enclosingElement = ElementFactory.get(method.getDeclaringClass());
   returnType = TypeMirrorFactory.get(method.getGenericReturnType());
   Type[] genericParameterTypes = method.getGenericParameterTypes();
   Annotation[][] parameterAnnotations = method.getParameterAnnotations();
   int index = 0;
   for (Type param : genericParameterTypes) {
     parameters.add(ElementFactory.getVariableElement(param, parameterAnnotations[index++]));
   }
   varArgs = method.isVarArgs();
   for (Type type : method.getGenericExceptionTypes()) {
     thrownTypes.add(TypeMirrorFactory.get(type));
   }
   Object defValue = method.getDefaultValue();
   if (defValue != null) {
     defaultValue = new AnnotationValueImpl(defValue);
   }
 }
  @Override
  public boolean preHandle(
      HttpServletRequest _request, HttpServletResponse _response, Object _handler)
      throws Exception {
    // TODO Auto-generated method stub

    String uri = _request.getRequestURI();
    System.out.println(uri);

    long beginTime = System.currentTimeMillis(); // 1、开始时间
    startTimeThreadLocal.set(beginTime); // 线程绑定变量(该数据只有当前请求的线程可见)	
    if (_handler instanceof HandlerMethod) {
      log.info("--------------------------");
      HandlerMethod hm = (HandlerMethod) _handler;
      Method method = hm.getMethod();
      TypeVariable[] tvs = method.getTypeParameters();
      Type[] types = method.getGenericParameterTypes();
      log.info("--------------------------");
    }
    boolean rtn = super.preHandle(_request, _response, _handler);

    log.info(rtn);
    return rtn;
  }
Example #8
0
  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);
    }
  }