Java Class.getMethods Examples

Java Class.getMethods - 30 examples found. These are the top rated real world Java examples of Class.getMethods extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  private final void bindResourceToLocalContainer(
      final Class<?> resource, final Class<?> container) {
    final Set<Method> nonAbstractMethods =
        Sets.newHashSet(resource.getMethods())
            .stream()
            .filter(method -> !Modifier.isAbstract(method.getModifiers()))
            .collect(Collectors.toSet());
    Preconditions.checkState(
        !nonAbstractMethods.isEmpty(),
        "Found non-abstract methods in " + resource + ": " + nonAbstractMethods);

    final Set<Method> abstractMethods =
        Sets.newHashSet(resource.getMethods())
            .stream()
            .filter(method -> Modifier.isAbstract(method.getModifiers()))
            .collect(Collectors.toSet());

    for (final Method resourceMethod : abstractMethods) {
      final Method containerMethod = findMatchingMethod(container, resourceMethod);
      if (containerMethod != null) {
        this.resourceToContainer.put(resourceMethod, containerMethod);
      }
    }

    bindResourceToContainer(resource, injector.getInstance(container));
  }
  public OutputPipeInvocationHandler(Pipe outputRing, int msgIdx, Class<?> clazz) {
    super(clazz.getMethods());

    FieldReferenceOffsetManager from = Pipe.from(outputRing);
    final Method[] methods = clazz.getMethods();

    writers = new OutputPipeWriterMethod[MAX_METHODS];
    int j = methods.length;
    while (--j >= 0) {
      final Method method = methods[j];
      ProngTemplateField fieldAnnonation = method.getAnnotation(ProngTemplateField.class);
      if (null != fieldAnnonation) {

        int fieldLoc =
            FieldReferenceOffsetManager.lookupFieldLocator(fieldAnnonation.fieldId(), msgIdx, from);

        int key = buildKey(this, method.getName());
        if (null != writers[key]) {
          throw new UnsupportedOperationException();
        }
        writers[key] =
            OutputPipeWriterMethod.buildWriteForYourType(
                outputRing,
                fieldAnnonation.decimalPlaces(),
                fieldLoc,
                (fieldLoc >> FieldReferenceOffsetManager.RW_FIELD_OFF_BITS)
                    & TokenBuilder.MASK_TYPE,
                from);
      }
    }
  }
  public WrapperHelper createWrapperHelper(
      Class<?> wrapperType,
      QName wrapperName,
      List<String> partNames,
      List<String> elTypeNames,
      List<Class<?>> partClasses) {

    List<Method> getMethods = new ArrayList<Method>(partNames.size());
    List<Method> setMethods = new ArrayList<Method>(partNames.size());
    List<Field> fields = new ArrayList<Field>(partNames.size());

    Method allMethods[] = wrapperType.getMethods();

    for (int x = 0; x < partNames.size(); x++) {
      String partName = partNames.get(x);
      if (partName == null) {
        getMethods.add(null);
        setMethods.add(null);
        fields.add(null);
        continue;
      }

      String getAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
      String setAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);
      Method getMethod = null;
      Method setMethod = null;
      Class<?> valueClass = XmlBeansWrapperHelper.getXMLBeansValueType(wrapperType);
      allMethods = valueClass.getMethods();

      try {
        getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
      } catch (NoSuchMethodException ex) {
        // ignore for now
      }

      for (Method method : allMethods) {
        if (method.getParameterTypes() != null
            && method.getParameterTypes().length == 1
            && (setAccessor.equals(method.getName()))) {
          setMethod = method;
          break;
        }
      }

      getMethods.add(getMethod);
      setMethods.add(setMethod);
      // There is no filed in the XMLBeans type class
      fields.add(null);
    }

    return new XmlBeansWrapperHelper(
        wrapperType,
        setMethods.toArray(new Method[setMethods.size()]),
        getMethods.toArray(new Method[getMethods.size()]),
        fields.toArray(new Field[fields.size()]));
  }
  /**
   * Creates a support map that maintain for each method name (name as key) a list of Methods found
   * in the Class.
   */
  @SuppressWarnings("deprecation")
  private Map<String, List<Method>> buildAnnotatedMethodsCache(Class<?> clazz) {
    ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    IProject currProj = JDTUtils.getCurrentProjectForOpenEditor();
    String classCanonicalName = clazz.getCanonicalName();
    if (currProj != null
        && !classCanonicalName.startsWith("net.sf.jasperreports.functions.standard")) {
      // Try to reload a fresh new instance of the Class instance.
      // Useful for code being developed directly inside JSS.
      // We skip the standard ones contributed via jar.
      OutputFolderClassLoader reloaderCL =
          new OutputFolderClassLoader(JavaCore.create(currProj), ctxClassLoader);
      Class<?> reloadedClazz = reloaderCL.reloadClass(clazz.getCanonicalName());
      if (reloadedClazz != null) {
        clazz = reloadedClazz;
      }
    }

    Map<String, List<Method>> methodsByNameMap = new HashMap<String, List<Method>>();

    // First round locate all methods with the JRFunction annotation
    // in order to have a list of what will be the functions
    for (Method m : clazz.getMethods()) {
      JRExprFunction jrFunctionAnn = m.getAnnotation(JRExprFunction.class);
      Function jrNewFunctionAnn = m.getAnnotation(Function.class);
      if (jrNewFunctionAnn != null || jrFunctionAnn != null) {
        String methodName = m.getName();
        List<Method> methods = methodsByNameMap.get(methodName);
        if (methods == null) {
          methods = new ArrayList<Method>();
        }
        methods.add(m);
        methodsByNameMap.put(methodName, methods);
      }
    }

    // After that enrich the map with the remaining methods that have the same function
    // name but a different list of parameters
    for (Method m : clazz.getMethods()) {
      JRExprFunction jrFunctionAnn = m.getAnnotation(JRExprFunction.class);
      Function jrNewFunctionAnn = m.getAnnotation(Function.class);
      if (jrNewFunctionAnn == null || jrFunctionAnn == null) {
        String methodName = m.getName();
        List<Method> methods = methodsByNameMap.get(methodName);
        if (methods != null) {
          methods.add(m);
        }
      }
    }

    return methodsByNameMap;
  }
 private MetaClass skipTokens(LinkedList<String> tokens, MetaClass metaClass) {
   while (tokens.size() > 1) {
     Class theClass = metaClass.getTheClass();
     String token = tokens.pollFirst();
     Class selectedClass = null;
     if (propertyPattern.matcher(token).matches()) {
       String getterName = "get" + StringGroovyMethods.capitalize((CharSequence) token);
       for (MetaMethod metaMethod : metaClass.getMetaMethods()) {
         if (metaMethod.getName().equals(getterName)) {
           selectedClass = metaMethod.getReturnType();
           break;
         }
       }
       if (selectedClass == null)
         for (Method method : theClass.getMethods()) {
           if (method.getParameterTypes().length != 0) continue;
           if (method.getName().equals(getterName)) {
             selectedClass = method.getReturnType();
             break;
           }
         }
       if (selectedClass == null) {
         MetaProperty metaProperty = metaClass.getMetaProperty(token);
         if (metaProperty == null) return null;
         selectedClass = metaProperty.getType();
       }
     } else if (methodPattern.matcher(token).matches()) {
       String curMethodName = token.substring(0, token.indexOf('('));
       for (MetaMethod metaMethod : metaClass.getMetaMethods()) {
         String name = metaMethod.getName();
         if (name.equals(curMethodName)) {
           selectedClass = metaMethod.getReturnType();
           break;
         }
       }
       if (selectedClass == null)
         for (Method method : theClass.getMethods()) {
           String name = method.getName();
           if (name.equals(curMethodName)) {
             selectedClass = method.getReturnType();
             break;
           }
         }
     }
     if (selectedClass == null) return null;
     metaClass = InvokerHelper.getMetaClass(selectedClass);
   }
   return metaClass;
 }
Example #6
0
 private static <C> Method getAdder(Class<C> klass, Field field) {
   Method[] methods = klass.getMethods();
   for (Method method : methods) {
     if (isAdder(method, field.getName())) return method;
   }
   return null;
 }