List of usage examples for java.lang.reflect Method isSynthetic
@Override public boolean isSynthetic()
From source file:org.crazydog.util.spring.ClassUtils.java
/** * Determine whether the given method is declared by the user or at least pointing to * a user-declared method./*from www.j a v a 2s.com*/ * <p>Checks {@link Method#isSynthetic()} (for implementation methods) as well as the * {@code GroovyObject} interface (for interface methods; on an implementation class, * implementations of the {@code GroovyObject} methods will be marked as synthetic anyway). * Note that, despite being synthetic, bridge methods ({@link Method#isBridge()}) are considered * as user-level methods since they are eventually pointing to a user-declared generic method. * @param method the method to check * @return {@code true} if the method can be considered as user-declared; [@code false} otherwise */ public static boolean isUserLevelMethod(Method method) { org.springframework.util.Assert.notNull(method, "Method must not be null"); return (method.isBridge() || (!method.isSynthetic() && !isGroovyObjectMethod(method))); }
From source file:hu.bme.mit.sette.common.model.snippet.SnippetInputFactoryContainer.java
/** * Validates methods of the class./* w w w .j av a2 s . c o m*/ * * @param validator * a validator * @return a map containing the input factory methods by their name */ private Map<String, Method> validateMethods(final AbstractValidator<?> validator) { // check: only "public static" or synthetic methods Map<String, Method> factoryMethods = new HashMap<String, Method>(); for (Method method : javaClass.getDeclaredMethods()) { if (method.isSynthetic()) { // skip synthetic methods continue; } MethodValidator v = new MethodValidator(method); if (factoryMethods.get(method.getName()) != null) { v.addException("The method must have a unique name"); } v.withModifiers(Modifier.PUBLIC | Modifier.STATIC); v.withoutModifiers(Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.SYNCHRONIZED); factoryMethods.put(method.getName(), method); validator.addChildIfInvalid(v); } return factoryMethods; }
From source file:com.netflix.hystrix.contrib.javanica.utils.MethodProvider.java
/** * Finds generic method for the given bridge method. * * @param bridgeMethod the bridge method * @param aClass the type where the bridge method is declared * @return generic method/*from w w w . j a v a 2 s . c o m*/ * @throws IOException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public Method unbride(final Method bridgeMethod, Class<?> aClass) throws IOException, NoSuchMethodException, ClassNotFoundException { if (bridgeMethod.isBridge() && bridgeMethod.isSynthetic()) { if (cache.containsKey(bridgeMethod)) { return cache.get(bridgeMethod); } ClassReader classReader = new ClassReader(aClass.getName()); final MethodSignature methodSignature = new MethodSignature(); classReader.accept(new ClassVisitor(ASM5) { @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { boolean bridge = (access & ACC_BRIDGE) != 0 && (access & ACC_SYNTHETIC) != 0; if (bridge && bridgeMethod.getName().equals(name) && getParameterCount(desc) == bridgeMethod.getParameterTypes().length) { return new MethodFinder(methodSignature); } return super.visitMethod(access, name, desc, signature, exceptions); } }, 0); Method method = aClass.getDeclaredMethod(methodSignature.name, methodSignature.getParameterTypes()); cache.put(bridgeMethod, method); return method; } else { return bridgeMethod; } }
From source file:GenericClass.java
public List<MethodDef> getMethods() { List<MethodDef> founds = new ArrayList<MethodDef>(); Class<?> current = myclass; while (current != null) { Method[] methods = current.getDeclaredMethods(); for (Method method : methods) { if (!method.isBridge() && !method.isSynthetic()) { MethodDef def = new MethodDef(method); if (!founds.contains(def)) founds.add(def);//from ww w . j av a 2 s . c om } } current = current.getSuperclass(); } return founds; }
From source file:GenericClass.java
/** * Search for all methods having that name, no matter which parameter they * take./*from w w w .j av a 2 s . co m*/ * * @param name * The name of the methods * @return A list of {@link MethodDef}, ordered with methods from current * class first, then method from superclass and so on. */ public List<MethodDef> findAllMethods(String name) { List<MethodDef> founds = new ArrayList<MethodDef>(); Class<?> current = myclass; while (current != null) { Method[] methods = current.getDeclaredMethods(); for (Method method : methods) { if (!method.isBridge() && !method.isSynthetic() && method.getName().equals(name)) { MethodDef def = new MethodDef(method); if (!founds.contains(def)) founds.add(def); } } current = current.getSuperclass(); } return founds; }
From source file:GenericClass.java
/** * Search for all occurrencies of a specific method. * <p>/*w ww .j av a 2 s . co m*/ * The type parameters passed in may be Class or null. If they are null, that * means that we don't know which class they should be, if they are a class, * that means we are searching for that class, and the comparison is made on * dereferenced generics. * </p> * <p> * Specifying no parameter types explicitly means "a method without * parameters". * </p> * * @param name * The name of the method * @param parameterTypes * The types of the parameters * @return A list of {@link MethodDef}, ordered with methods from current * class first, then method from superclass and so on. */ public List<MethodDef> findMethods(String name, Class<?>... parameterTypes) { List<MethodDef> founds = new ArrayList<MethodDef>(); Class<?> current = myclass; while (current != null) { Method[] methods = current.getDeclaredMethods(); for (Method method : methods) { if (!method.isBridge() && !method.isSynthetic() && method.getName().equals(name)) { Type[] types = method.getGenericParameterTypes(); if (types.length == parameterTypes.length) { GenericClass[] genericClasses = getParameterTypes(method); Class<?>[] classes = toRawClasses(genericClasses); boolean good = true; for (int i = 0; i < types.length; i++) { if (parameterTypes[i] != null) { if (!classes[i].equals(parameterTypes[i])) { good = false; break; } } } if (good) { MethodDef def = new MethodDef(method, genericClasses); if (!founds.contains(def)) founds.add(def); } } } } current = current.getSuperclass(); } return founds; }
From source file:io.konik.utils.RandomInvoiceGenerator.java
public Object populteData(Class<?> root, String name) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Object rootObj;//www .j a v a 2s .c o m if (isLeafType(root)) {//final type return generatePrimitveValue(root, name); } rootObj = createNewInstance(root); // get method and populate each of them Method[] methods = root.getMethods(); for (Method method : methods) { int methodModifiers = method.getModifiers(); Class<?> methodParameter = null; if (Modifier.isAbstract(methodModifiers) || method.isSynthetic()) continue; if (method.getName().startsWith("add")) { methodParameter = method.getParameterTypes()[0]; if (methodParameter != null && !methodParameter.isArray() && (methodParameter.isInterface() || Modifier.isAbstract(methodParameter.getModifiers()))) { continue; } } //getter else if (method.getName().startsWith("get") && !Collection.class.isAssignableFrom(method.getReturnType()) && !method.getName().equals("getClass") && !Modifier.isAbstract(methodModifiers)) { methodParameter = method.getReturnType(); } else { continue;// next on setter } if (methodParameter == null || methodParameter.isInterface()) { continue; } Object popultedData = populteData(methodParameter, method.getName()); setValue(rootObj, method, popultedData); } return rootObj; }
From source file:org.codehaus.griffon.commons.ClassPropertyFetcher.java
private void init() { FieldCallback fieldCallback = new ReflectionUtils.FieldCallback() { public void doWith(Field field) { if (field.isSynthetic()) return; final int modifiers = field.getModifiers(); if (!Modifier.isPublic(modifiers)) return; final String name = field.getName(); if (name.indexOf('$') == -1) { boolean staticField = Modifier.isStatic(modifiers); if (staticField) { staticFetchers.put(name, new FieldReaderFetcher(field, staticField)); } else { instanceFetchers.put(name, new FieldReaderFetcher(field, staticField)); }//from ww w. ja v a 2 s. c o m } } }; MethodCallback methodCallback = new ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if (method.isSynthetic()) return; if (!Modifier.isPublic(method.getModifiers())) return; if (Modifier.isStatic(method.getModifiers()) && method.getReturnType() != Void.class) { if (method.getParameterTypes().length == 0) { String name = method.getName(); if (name.indexOf('$') == -1) { if (name.length() > 3 && name.startsWith("get") && Character.isUpperCase(name.charAt(3))) { name = name.substring(3); } else if (name.length() > 2 && name.startsWith("is") && Character.isUpperCase(name.charAt(2)) && (method.getReturnType() == Boolean.class || method.getReturnType() == boolean.class)) { name = name.substring(2); } PropertyFetcher fetcher = new GetterPropertyFetcher(method, true); staticFetchers.put(name, fetcher); staticFetchers.put(StringUtils.uncapitalize(name), fetcher); } } } } }; List<Class<?>> allClasses = resolveAllClasses(clazz); for (Class<?> c : allClasses) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { try { fieldCallback.doWith(field); } catch (IllegalAccessException ex) { throw new IllegalStateException( "Shouldn't be illegal to access field '" + field.getName() + "': " + ex); } } Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { try { methodCallback.doWith(method); } catch (IllegalAccessException ex) { throw new IllegalStateException( "Shouldn't be illegal to access method '" + method.getName() + "': " + ex); } } } propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz); for (PropertyDescriptor desc : propertyDescriptors) { Method readMethod = desc.getReadMethod(); if (readMethod != null) { boolean staticReadMethod = Modifier.isStatic(readMethod.getModifiers()); if (staticReadMethod) { staticFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod)); } else { instanceFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod)); } } } }
From source file:hu.bme.mit.sette.common.model.snippet.SnippetContainer.java
/** * Validates methods of the class.//from www .jav a2 s . co m * * @param validator * a validator * @return a map containing the snippet methods by their name */ private Map<String, Method> validateMethods(final AbstractValidator<?> validator) { // check: only "[public|private] static" or synthetic methods Map<String, Method> snippetMethods = new HashMap<String, Method>(); for (Method method : javaClass.getDeclaredMethods()) { if (method.isSynthetic()) { // skip synthetic methods continue; } MethodValidator v = new MethodValidator(method); if (snippetMethods.get(method.getName()) != null) { v.addException("The method must have a unique name"); } int methodModifiers = method.getModifiers(); if (!Modifier.isPublic(methodModifiers) && !Modifier.isPrivate(methodModifiers)) { v.addException("The method must be public or private"); } v.withModifiers(Modifier.STATIC); v.withoutModifiers(Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.SYNCHRONIZED); AnnotationMap methodAnns = SetteAnnotationUtils.getSetteAnnotations(method); if (Modifier.isPublic(methodModifiers)) { if (methodAnns.get(SetteNotSnippet.class) == null) { // should be snippet, validated by Snippet class and added // later snippetMethods.put(method.getName(), method); } else { // not snippet snippetMethods.put(method.getName(), null); if (methodAnns.size() != 1) { v.addException("The method must not have " + "any other SETTE annotations " + "if it is not a snippet."); } } } else { // method is private if (methodAnns.size() != 0) { v.addException("The method must not have " + "any SETTE annotations"); } } validator.addChildIfInvalid(v); } return snippetMethods; }
From source file:hu.bme.mit.sette.common.model.snippet.Snippet.java
/** * Parses the methods which should be considered in coverage. * * @param annotation/*from ww w. jav a2s . com*/ * the {@link SetteIncludeCoverage} annotation * @param v * a {@link MethodValidator} * @param classLoader * the class loader for loading snippet project classes */ private void parseIncludedMethods(final SetteIncludeCoverage annotation, final MethodValidator v, final ClassLoader classLoader) { if (annotation == null) { return; } Class<?>[] includedClasses = annotation.classes(); String[] includedMethodStrings = annotation.methods(); boolean shouldParse = true; // only parse if no validation error // check the arrays: not empty, no null element, same lengths if (ArrayUtils.isEmpty(includedClasses)) { v.addException("The included class list must not be empty"); shouldParse = false; } if (ArrayUtils.contains(includedClasses, null)) { v.addException("The included class list " + "must not contain null elements"); shouldParse = false; } if (ArrayUtils.isEmpty(includedMethodStrings)) { v.addException("The included method list must not be empty"); shouldParse = false; } if (ArrayUtils.contains(includedMethodStrings, null)) { v.addException("The included method list " + "must not contain null elements"); shouldParse = false; } if (!ArrayUtils.isSameLength(includedClasses, includedMethodStrings)) { v.addException("The included class list and method list " + "must have the same length"); shouldParse = false; } if (shouldParse) { // check and add methods for (int i = 0; i < includedClasses.length; i++) { Class<?> includedClass = includedClasses[i]; String includedMethodString = includedMethodStrings[i].trim(); if (includedMethodString.equals("*")) { // add all non-synthetic constructors for (Constructor<?> c : includedClass.getDeclaredConstructors()) { if (!c.isSynthetic()) { addIncludedConstructor(c, v); } } // add all non-synthetic methods for (Method m : includedClass.getDeclaredMethods()) { if (!m.isSynthetic()) { addIncludedMethod(m, v); } } } else { parseIncludedMethod(includedClass, includedMethodString, v, classLoader); } } } }