/**
   * 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
   public static void main(String[]argv) throws Exception {
	   Method m = Goo.class.getDeclaredMethod("getStrings");
	   Type t = m.getGenericReturnType();
	   if (!t.toString().equals("java.util.List<java.lang.String>")) 
		   throw new RuntimeException("Incorrect signature. Signature is "+t);

	   m = IFace.class.getDeclaredMethod("getStrings");
	   t = m.getGenericReturnType();
	   if (!t.toString().equals("java.util.List<java.lang.String>")) 
		   throw new RuntimeException("Incorrect signature. Signature is "+t);
   }
    public Object invoke(Object target, Method method, Object[] params) throws Throwable {
      if (EQUALS_METHOD.equals(method)) {
        Object param = params[0];
        if (param == null || !Proxy.isProxyClass(param.getClass())) {
          return false;
        }
        InvocationHandler other = Proxy.getInvocationHandler(param);
        return equals(other);
      } else if (HASHCODE_METHOD.equals(method)) {
        return hashCode();
      }

      MethodInvocation invocation =
          new MethodInvocation(
              method.getName(),
              method.getReturnType(),
              method.getGenericReturnType(),
              method.getParameterTypes(),
              delegate,
              params);
      invoker.invoke(invocation);
      if (!invocation.found()) {
        String methodName =
            method.getDeclaringClass().getSimpleName() + "." + method.getName() + "()";
        throw Exceptions.unsupportedMethod(methodName);
      }
      return invocation.getResult();
    }
Example #4
0
  public static Class<?> determineGenericsType(Method method, boolean isReadMethod) {
    Class<?> result = null;
    if (isReadMethod) {
      Type parameterType = method.getGenericReturnType();
      result = determineGenericsType(parameterType);
    } else {
      Type[] parameterTypes = method.getGenericParameterTypes();
      if (parameterTypes != null) {
        result = determineGenericsType(parameterTypes[0]);
      }
    }

    return result;
  }
Example #5
0
  private Class<?> resolveReturnType(Method me) {
    Type returnType = me.getGenericReturnType();

    if (returnType instanceof ParameterizedType) {
      ParameterizedType type = (ParameterizedType) returnType;
      Type[] typeArguments = type.getActualTypeArguments();
      for (Type typeArgument : typeArguments) {
        if (typeArgument instanceof Class<?>) {
          return (Class<?>) typeArgument;
        }
      }
      return null;
    }
    return (Class<?>) returnType;
  }
Example #6
0
  /**
   * {@link InvocationHandler} implementation that allows strongly-typed access to the
   * configuration.
   *
   * <p>TODO: it might be a great performance improvement to have APT generate code that does this
   * during the development time by looking at the interface.
   */
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // serve java.lang.Object methods by ourselves
    Class<?> clazz = method.getDeclaringClass();
    if (clazz == Object.class) {
      try {
        return method.invoke(this, args);
      } catch (InvocationTargetException e) {
        throw e.getTargetException();
      }
    }

    if (method.getAnnotation(DuckTyped.class) != null) {
      return invokeDuckMethod(method, proxy, args);
    }
    if (method.getAnnotation(ConfigExtensionMethod.class) != null) {
      ConfigExtensionMethod cem = method.getAnnotation(ConfigExtensionMethod.class);
      ConfigExtensionHandler handler =
          (ConfigExtensionHandler)
              ((cem.value() != null)
                  ? getServiceLocator().getService(ConfigExtensionHandler.class, cem.value())
                  : getServiceLocator().getService(ConfigExtensionHandler.class));
      return invokeConfigExtensionMethod(handler, this, model.getProxyType(), args);
    }

    ConfigModel.Property p = model.toProperty(method);
    if (p == null)
      throw new IllegalArgumentException("No corresponding property found for method: " + method);

    if (args == null || args.length == 0) {
      // getter
      return getter(p, method.getGenericReturnType());
    } else {
      throw new PropertyVetoException(
          "Instance of "
              + getImplementation()
              + " named '"
              + getKey()
              + "' is not locked for writing when invoking method "
              + method.getName()
              + " you must use transaction semantics to access it.",
          null);
    }
  }
  private JSONArray getClassProperties(Class<?> clazz, int level) throws IntrospectionException {
    JSONArray arr = new JSONArray();
    TypeDiscoverer td = new TypeDiscoverer();
    try {
      for (PropertyDescriptor pd : Introspector.getBeanInfo(clazz).getPropertyDescriptors()) {
        Method readMethod = pd.getReadMethod();
        if (readMethod != null) {
          if (readMethod.getDeclaringClass() == java.lang.Enum.class) {
            // skip getDeclaringClass
            continue;
          } else if ("class".equals(pd.getName())) {
            // skip getClass
            continue;
          }
        } else {
          // yields com.datatorrent.api.Context on JDK6 and
          // com.datatorrent.api.Context.OperatorContext with JDK7
          if ("up".equals(pd.getName())
              && com.datatorrent.api.Context.class.isAssignableFrom(pd.getPropertyType())) {
            continue;
          }
        }
        // LOG.info("name: " + pd.getName() + " type: " + pd.getPropertyType());

        Class<?> propertyType = pd.getPropertyType();
        if (propertyType != null) {
          JSONObject propertyObj = new JSONObject();
          propertyObj.put("name", pd.getName());
          propertyObj.put("canGet", readMethod != null);
          propertyObj.put("canSet", pd.getWriteMethod() != null);
          if (readMethod != null) {
            for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
              OperatorClassInfo oci = classInfo.get(c.getName());
              if (oci != null) {
                MethodInfo getMethodInfo = oci.getMethods.get(readMethod.getName());
                if (getMethodInfo != null) {
                  addTagsToProperties(getMethodInfo, propertyObj);
                  break;
                }
              }
            }
            // type can be a type symbol or parameterized type
            td.setTypeArguments(clazz, readMethod.getGenericReturnType(), propertyObj);
          } else {
            if (pd.getWriteMethod() != null) {
              td.setTypeArguments(
                  clazz, pd.getWriteMethod().getGenericParameterTypes()[0], propertyObj);
            }
          }
          // if (!propertyType.isPrimitive() && !propertyType.isEnum() && !propertyType.isArray() &&
          // !propertyType.getName().startsWith("java.lang") && level < MAX_PROPERTY_LEVELS) {
          //  propertyObj.put("properties", getClassProperties(propertyType, level + 1));
          // }
          arr.put(propertyObj);
        }
      }
    } catch (JSONException ex) {
      throw new RuntimeException(ex);
    }
    return arr;
  }