Esempio n. 1
0
 /** Checks whether the constructor parsed from API xml file and Java reflection are compliant. */
 @SuppressWarnings("unchecked")
 private void checkConstructorCompliance() {
   for (JDiffConstructor con : jDiffConstructors) {
     try {
       Constructor<?> c = findMatchingConstructor(con);
       if (c == null) {
         mResultObserver.notifyFailure(
             SignatureTestActivity.FAILURE_TYPE.MISSING_METHOD,
             con.toReadableString(mAbsoluteClassName),
             "No method with correct signature found:" + con.toSignatureString());
       } else {
         if (c.isVarArgs()) { // some method's parameter are variable args
           con.mModifier |= METHOD_MODIFIER_VAR_ARGS;
         }
         if (c.getModifiers() != con.mModifier) {
           mResultObserver.notifyFailure(
               SignatureTestActivity.FAILURE_TYPE.MISMATCH_METHOD,
               con.toReadableString(mAbsoluteClassName),
               "Non-compatible method found when looking for " + con.toSignatureString());
         }
       }
     } catch (Exception e) {
       SignatureTestLog.e("Got exception when checking constructor compliance", e);
       mResultObserver.notifyFailure(
           SignatureTestActivity.FAILURE_TYPE.CAUGHT_EXCEPTION,
           con.toReadableString(mAbsoluteClassName),
           "Exception!");
     }
   }
 }
Esempio n. 2
0
 private To newInstanceWithArgWrapper0(Object x)
     throws ClassNotFoundException, InstantiationException, IllegalAccessException,
         IllegalArgumentException, InvocationTargetException {
   Object[] params = new Object[formals.length];
   params[0] = new ArgWrapper(x, extending, formals);
   if (extending.isVarArgs()) {
     params = new Object[] {params};
   }
   return (To) extending.newInstance(params);
 }
Esempio n. 3
0
 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));
   }
 }
Esempio n. 4
0
  public static EffectBase getEffect(String effectTypeString, Value[] values) {

    EffectBase effect = null;

    try {
      Class effectClass = (Class) effectMap.get(effectTypeString.toLowerCase());

      if (effectClass != null) {
        Constructor c = effectClass.getConstructors()[0];
        if (c.isVarArgs())
          effect = (EffectBase) effectClass.getConstructors()[0].newInstance(new Object[] {values});
        else effect = (EffectBase) effectClass.getConstructors()[0].newInstance(new Object[] {});
      }
    } catch (Exception exc) {
      exc.printStackTrace();
    }

    return effect;
  }
  /**
   * Locate a constructor on the type. There are three kinds of match that might occur:
   *
   * <ol>
   *   <li>An exact match where the types of the arguments match the types of the constructor
   *   <li>An in-exact match where the types we are looking for are subtypes of those defined on the
   *       constructor
   *   <li>A match where we are able to convert the arguments into those expected by the
   *       constructor, according to the registered type converter.
   * </ol>
   */
  @Override
  public ConstructorExecutor resolve(
      EvaluationContext context, String typename, List<TypeDescriptor> argumentTypes)
      throws AccessException {

    try {
      TypeConverter typeConverter = context.getTypeConverter();
      Class<?> type = context.getTypeLocator().findType(typename);
      Constructor<?>[] ctors = type.getConstructors();

      Arrays.sort(
          ctors,
          new Comparator<Constructor<?>>() {
            @Override
            public int compare(Constructor<?> c1, Constructor<?> c2) {
              int c1pl = c1.getParameterTypes().length;
              int c2pl = c2.getParameterTypes().length;
              return (new Integer(c1pl)).compareTo(c2pl);
            }
          });

      Constructor<?> closeMatch = null;

      for (Constructor<?> ctor : ctors) {
        Class<?>[] paramTypes = ctor.getParameterTypes();
        List<TypeDescriptor> paramDescriptors = new ArrayList<TypeDescriptor>(paramTypes.length);
        for (int i = 0; i < paramTypes.length; i++) {
          paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
        }
        ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
        if (ctor.isVarArgs() && argumentTypes.size() >= paramTypes.length - 1) {
          // *sigh* complicated
          // Basically.. we have to have all parameters match up until the varargs one, then the
          // rest of what is
          // being provided should be
          // the same type whilst the final argument to the method must be an array of that (oh, how
          // easy...not) -
          // or the final parameter
          // we are supplied does match exactly (it is an array already).
          matchInfo =
              ReflectionHelper.compareArgumentsVarargs(
                  paramDescriptors, argumentTypes, typeConverter);
        } else if (paramTypes.length == argumentTypes.size()) {
          // worth a closer look
          matchInfo =
              ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
        }
        if (matchInfo != null) {
          if (matchInfo.isExactMatch()) {
            return new ReflectiveConstructorExecutor(ctor);
          } else if (matchInfo.isCloseMatch() || matchInfo.isMatchRequiringConversion()) {
            closeMatch = ctor;
          }
        }
      }

      return (closeMatch != null ? new ReflectiveConstructorExecutor(closeMatch) : null);
    } catch (EvaluationException ex) {
      throw new AccessException("Failed to resolve constructor", ex);
    }
  }
  @Override
  public Object invoke(
      ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params) {

    if (context == null) {
      throw new NullPointerException();
    }

    if (base instanceof ELClass && method instanceof String) {
      context.setPropertyResolved(base, method);

      Class<?> clazz = ((ELClass) base).getKlass();
      String methodName = (String) method;

      if ("<init>".equals(methodName)) {
        Constructor<?> match = Util.findConstructor(clazz, paramTypes, params);

        Object[] parameters =
            Util.buildParameters(match.getParameterTypes(), match.isVarArgs(), params);

        Object result = null;

        try {
          result = match.newInstance(parameters);
        } catch (IllegalArgumentException | IllegalAccessException | InstantiationException e) {
          throw new ELException(e);
        } catch (InvocationTargetException e) {
          Throwable cause = e.getCause();
          Util.handleThrowable(cause);
          throw new ELException(cause);
        }
        return result;

      } else {
        Method match = Util.findMethod(clazz, methodName, paramTypes, params);

        int modifiers = match.getModifiers();
        if (!Modifier.isStatic(modifiers)) {
          throw new MethodNotFoundException(
              Util.message(
                  context, "staticFieldELResolver.methodNotFound", methodName, clazz.getName()));
        }

        Object[] parameters =
            Util.buildParameters(match.getParameterTypes(), match.isVarArgs(), params);

        Object result = null;
        try {
          result = match.invoke(null, parameters);
        } catch (IllegalArgumentException | IllegalAccessException e) {
          throw new ELException(e);
        } catch (InvocationTargetException e) {
          Throwable cause = e.getCause();
          Util.handleThrowable(cause);
          throw new ELException(cause);
        }
        return result;
      }
    }
    return null;
  }