List of usage examples for java.lang.reflect Method isSynthetic
@Override public boolean isSynthetic()
From source file:org.evosuite.setup.TestClusterGenerator.java
public static boolean canUse(Method m, Class<?> ownerClass) { if (m.isBridge()) { logger.debug("Excluding bridge method: {}", m.toString()); return false; }// w w w. j a v a 2s. c o m if (m.isSynthetic()) { logger.debug("Excluding synthetic method: {}", m.toString()); return false; } if (!Properties.USE_DEPRECATED && m.isAnnotationPresent(Deprecated.class)) { logger.debug("Excluding deprecated method {}", m.getName()); return false; } if (m.isAnnotationPresent(Test.class)) { logger.debug("Excluding test method {}", m.getName()); return false; } if (m.isAnnotationPresent(EvoSuiteExclude.class)) { logger.debug("Excluding method with exclusion annotation {}", m.getName()); return false; } if (m.getDeclaringClass().equals(java.lang.Object.class)) { return false; } if (!m.getReturnType().equals(String.class) && !canUse(m.getReturnType())) { return false; } if (m.getDeclaringClass().equals(Enum.class)) { return false; /* if (m.getName().equals("valueOf") || m.getName().equals("values") || m.getName().equals("ordinal")) { logger.debug("Excluding valueOf for Enum " + m.toString()); return false; } // Skip compareTo on enums (like Randoop) if (m.getName().equals("compareTo") && m.getParameterTypes().length == 1 && m.getParameterTypes()[0].equals(Enum.class)) return false; */ } if (m.getDeclaringClass().equals(java.lang.Thread.class)) return false; // Hashcode only if we need to cover it if (m.getName().equals("hashCode") && !m.getDeclaringClass().equals(Properties.getTargetClass())) return false; // Randoop special case: just clumps together a bunch of hashCodes, so skip it if (m.getName().equals("deepHashCode") && m.getDeclaringClass().equals(Arrays.class)) return false; // Randoop special case: differs too much between JDK installations if (m.getName().equals("getAvailableLocales")) return false; if (m.getName().equals(ClassResetter.STATIC_RESET)) { logger.debug("Ignoring static reset class"); return false; } if (isForbiddenNonDeterministicCall(m)) { return false; } if (!Properties.CONSIDER_MAIN_METHODS && m.getName().equals("main") && Modifier.isStatic(m.getModifiers()) && Modifier.isPublic(m.getModifiers())) { logger.debug("Ignoring static main method "); return false; } /* if(m.getTypeParameters().length > 0) { logger.debug("Cannot handle generic methods at this point"); if(m.getDeclaringClass().equals(Properties.getTargetClass())) { LoggingUtils.getEvoLogger().info("* Skipping method "+m.getName()+": generic methods are not handled yet"); } return false; } */ // If default or if (Modifier.isPublic(m.getModifiers())) { makeAccessible(m); return true; } // If default access rights, then check if this class is in the same package as the target class if (!Modifier.isPrivate(m.getModifiers())) { // && !Modifier.isProtected(m.getModifiers())) { String packageName = ClassUtils.getPackageName(ownerClass); String declaredPackageName = ClassUtils.getPackageName(m.getDeclaringClass()); if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) { makeAccessible(m); return true; } } return false; }
From source file:org.evosuite.setup.TestUsageChecker.java
public static boolean canUse(Method m, Class<?> ownerClass) { if (m.isBridge()) { logger.debug("Excluding bridge method: " + m.toString()); return false; }//from w w w .j a v a 2s . c o m if (m.isSynthetic()) { logger.debug("Excluding synthetic method: " + m.toString()); return false; } if (!Properties.USE_DEPRECATED && m.isAnnotationPresent(Deprecated.class)) { final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && !m.getDeclaringClass().equals(targetClass)) { logger.debug("Excluding deprecated method " + m.getName()); return false; } } if (m.isAnnotationPresent(Test.class) || m.isAnnotationPresent(Before.class) || m.isAnnotationPresent(BeforeClass.class) || m.isAnnotationPresent(After.class) || m.isAnnotationPresent(AfterClass.class)) { logger.debug("Excluding test method " + m.getName()); return false; } if (m.isAnnotationPresent(EvoSuiteTest.class)) { logger.debug("Excluding EvoSuite test method " + m.getName()); return false; } if (m.isAnnotationPresent(EvoSuiteExclude.class)) { logger.debug("Excluding method with exclusion annotation " + m.getName()); return false; } if (m.getDeclaringClass().equals(java.lang.Object.class)) { return false; } if (!m.getReturnType().equals(String.class) && (!canUse(m.getReturnType()) || !canUse(m.getGenericReturnType()))) { return false; } for (java.lang.reflect.Type paramType : m.getGenericParameterTypes()) { if (!canUse(paramType)) return false; } if (m.getDeclaringClass().equals(Enum.class)) { return false; /* if (m.getName().equals("valueOf") || m.getName().equals("values") || m.getName().equals("ordinal")) { logger.debug("Excluding valueOf for Enum " + m.toString()); return false; } // Skip compareTo on enums (like Randoop) if (m.getName().equals("compareTo") && m.getParameterTypes().length == 1 && m.getParameterTypes()[0].equals(Enum.class)) return false; */ } if (m.getDeclaringClass().equals(java.lang.Thread.class)) return false; // Hashcode only if we need to cover it if (m.getName().equals("hashCode")) { final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (!m.getDeclaringClass().equals(targetClass)) return false; else { if (GraphPool.getInstance(ownerClass.getClassLoader()).getActualCFG(Properties.TARGET_CLASS, m.getName() + Type.getMethodDescriptor(m)) == null) { // Don't cover generated hashCode // TODO: This should work via annotations return false; } } } // Randoop special case: just clumps together a bunch of hashCodes, so skip it if (m.getName().equals("deepHashCode") && m.getDeclaringClass().equals(Arrays.class)) return false; // Randoop special case: differs too much between JDK installations if (m.getName().equals("getAvailableLocales")) return false; if (m.getName().equals(ClassResetter.STATIC_RESET)) { logger.debug("Ignoring static reset method"); return false; } if (isForbiddenNonDeterministicCall(m)) { return false; } if (!Properties.CONSIDER_MAIN_METHODS && m.getName().equals("main") && Modifier.isStatic(m.getModifiers()) && Modifier.isPublic(m.getModifiers())) { logger.debug("Ignoring static main method "); return false; } /* if(m.getTypeParameters().length > 0) { logger.debug("Cannot handle generic methods at this point"); if(m.getDeclaringClass().equals(Properties.getTargetClass())) { LoggingUtils.getEvoLogger().info("* Skipping method "+m.getName()+": generic methods are not handled yet"); } return false; } */ // If default or if (Modifier.isPublic(m.getModifiers())) { TestClusterUtils.makeAccessible(m); return true; } // If default access rights, then check if this class is in the same package as the target class if (!Modifier.isPrivate(m.getModifiers())) { // && !Modifier.isProtected(m.getModifiers())) { String packageName = ClassUtils.getPackageName(ownerClass); String declaredPackageName = ClassUtils.getPackageName(m.getDeclaringClass()); if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) { TestClusterUtils.makeAccessible(m); return true; } } return false; }
From source file:org.evosuite.testcase.statements.FunctionalMockStatement.java
public static boolean canBeFunctionalMocked(Type type) { Class<?> rawClass = new GenericClass(type).getRawClass(); final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && (rawClass.equals(targetClass))) { return false; }//from ww w .j a va 2 s .c o m if (EvoSuiteMock.class.isAssignableFrom(rawClass) || MockList.isAMockClass(rawClass.getName()) || rawClass.equals(Class.class) || rawClass.isArray() || rawClass.isPrimitive() || rawClass.isAnonymousClass() || rawClass.isEnum() || //note: Mockito can handle package-level classes, but we get all kinds of weird exceptions with instrumentation :( !Modifier.isPublic(rawClass.getModifiers())) { return false; } if (!InstrumentedClass.class.isAssignableFrom(rawClass) && Modifier.isFinal(rawClass.getModifiers())) { /* if a class has not been instrumented (eg because belonging to javax.*), then if it is final we cannot mock it :( recall that instrumentation does remove the final modifiers */ return false; } //FIXME: tmp fix to avoid mocking any class with package access methods try { for (Method m : rawClass.getDeclaredMethods()) { /* Unfortunately, it does not seem there is a "isPackageLevel" method, so we have to go by exclusion */ if (!Modifier.isPublic(m.getModifiers()) && !Modifier.isProtected(m.getModifiers()) && !Modifier.isPrivate(m.getModifiers()) && !m.isBridge() && !m.isSynthetic() && !m.getName().equals(ClassResetter.STATIC_RESET)) { return false; } } } catch (NoClassDefFoundError | Exception e) { //this could happen if we failed to load the class AtMostOnceLogger.warn(logger, "Failed to check if can mock class " + rawClass.getName() + ": " + e.getMessage()); return false; } //avoid cases of infinite recursions boolean onlySelfReturns = true; for (Method m : rawClass.getDeclaredMethods()) { if (!rawClass.equals(m.getReturnType())) { onlySelfReturns = false; break; } } if (onlySelfReturns && rawClass.getDeclaredMethods().length > 0) { //avoid weird cases like java.lang.Appendable return false; } //ad-hoc list of classes we should not really mock List<Class<?>> avoid = Arrays.asList( //add here if needed ); if (avoid.contains(rawClass)) { return false; } return true; }
From source file:org.gradle.model.internal.manage.schema.extract.ManagedImplTypeSchemaExtractionStrategySupport.java
private Iterable<Method> filterIgnoredMethods(Iterable<Method> methods) { return Iterables.filter(methods, new Predicate<Method>() { @Override// ww w .j a va 2 s. c om public boolean apply(Method method) { return !method.isSynthetic() && !IGNORED_METHODS.contains(METHOD_EQUIVALENCE.wrap(method)) && !ignoreMethod(method); } }); }
From source file:org.gradle.model.internal.manage.schema.extract.ManagedImplTypeSchemaExtractionStrategySupport.java
private void ensureNoProtectedOrPrivateMethods(ModelSchemaExtractionContext<?> extractionContext, Class<?> typeClass) { Class<?> superClass = typeClass.getSuperclass(); if (superClass != null && !superClass.equals(Object.class)) { ensureNoProtectedOrPrivateMethods(extractionContext, superClass); }//from ww w . jav a 2 s. c o m Iterable<Method> protectedAndPrivateMethods = Iterables .filter(Arrays.asList(typeClass.getDeclaredMethods()), new Predicate<Method>() { @Override public boolean apply(Method method) { int modifiers = method.getModifiers(); return !method.isSynthetic() && (Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)); } }); if (!Iterables.isEmpty(protectedAndPrivateMethods)) { throw invalidMethods(extractionContext, "protected and private methods are not allowed", protectedAndPrivateMethods); } }
From source file:org.gradle.model.internal.manage.schema.extract.StructStrategy.java
private Iterable<Method> filterIgnoredMethods(Iterable<Method> methods) { return Iterables.filter(methods, new Predicate<Method>() { @Override/*from ww w .j a va 2s . c om*/ public boolean apply(Method method) { return !method.isSynthetic() && !ignoredMethods.contains(equivalence.wrap(method)); } }); }
From source file:org.grails.datastore.mapping.reflect.ClassPropertyFetcher.java
private void processMethod(Method method) { if (method.isSynthetic()) { return;/* w ww .j av a2 s .c o m*/ } 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); List<PropertyFetcher> propertyFetchers = staticFetchers.get(name); if (propertyFetchers == null) { staticFetchers.put(name, propertyFetchers = new ArrayList<PropertyFetcher>()); } propertyFetchers.add(fetcher); String decapitalized = Introspector.decapitalize(name); if (!decapitalized.equals(name)) { propertyFetchers = staticFetchers.get(decapitalized); if (propertyFetchers == null) { staticFetchers.put(decapitalized, propertyFetchers = new ArrayList<PropertyFetcher>()); } propertyFetchers.add(fetcher); } } } } }
From source file:org.jtester.utility.ReflectionUtils.java
/** * Gets all methods of the given class and all its super-classes. * //ww w. j av a 2s . c o m * @param clazz * The class * @return The methods, not null */ public static Set<Method> getAllMethods(Class<?> clazz) { Set<Method> result = new HashSet<Method>(); if (clazz == null || clazz.equals(Object.class)) { return result; } // add all methods of this class Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method declaredMethod : declaredMethods) { if (declaredMethod.isSynthetic() || declaredMethod.isBridge()) { // skip methods that were added by the compiler continue; } result.add(declaredMethod); } // add all methods of the super-classes result.addAll(getAllMethods(clazz.getSuperclass())); return result; }
From source file:org.pircbotx.hooks.ListenerAdapterTest.java
@DataProvider @SuppressWarnings("unchecked") public static Object[][] onEventTestDataProvider() { //Map events to methods Map<Class<? extends Event>, Set<Method>> eventToMethod = new HashMap(); for (Method curMethod : ListenerAdapter.class.getDeclaredMethods()) { //Filter out methods by basic criteria if (curMethod.getName().equals("onEvent") || curMethod.getParameterTypes().length != 1 || curMethod.isSynthetic()) continue; Class<?> curClass = curMethod.getParameterTypes()[0]; //Filter out methods that don't have the right param or are already added if (curClass.isAssignableFrom(Event.class) || curClass.isInterface() || (eventToMethod.containsKey(curClass) && eventToMethod.get(curClass).contains(curMethod))) continue; Set<Method> methods = Sets.newHashSet(); methods.add(curMethod);/*w ww .ja v a 2s. com*/ eventToMethod.put((Class<? extends Event>) curClass, methods); } //Now that we have all the events, start mapping interfaces for (Method curMethod : ListenerAdapter.class.getDeclaredMethods()) { //Make sure this is an event method if (curMethod.getParameterTypes().length != 1 || curMethod.isSynthetic()) continue; Class<?> curClass = curMethod.getParameterTypes()[0]; if (!curClass.isInterface() || !GenericEvent.class.isAssignableFrom(curClass)) continue; //Add this interface method to all events that implement it for (Class curEvent : eventToMethod.keySet()) if (curClass.isAssignableFrom(curEvent) && !eventToMethod.get(curEvent).contains(curMethod)) eventToMethod.get(curEvent).add(curMethod); } //Build object array that TestNG understands Object[][] params = new Object[eventToMethod.size()][]; int paramsCounter = 0; for (Map.Entry<Class<? extends Event>, Set<Method>> curEntry : eventToMethod.entrySet()) params[paramsCounter++] = new Object[] { curEntry.getKey(), curEntry.getValue() }; return params; }
From source file:org.soybeanMilk.core.exe.Invoke.java
/** * ????/*w w w . jav a 2s . c o m*/ * @param clazz * @param methodName * @param args * @param exactMatch ?? * @return * @throws ExecuteException * @date 2012-5-11 */ protected MethodInfo findMethodInfo(Class<?> clazz, String methodName, Arg[] args, boolean exactMatch) { MethodInfo result = null; //????????? if (isAncestorType(Proxy.class, clazz)) { Class<?>[] interfaces = clazz.getInterfaces(); if (interfaces != null && interfaces.length > 0) { for (Class<?> si : interfaces) { result = findMethodInfo(si, methodName, args, exactMatch); if (result != null) break; } } } else { Method method = null; Class<?> methodClass = clazz; int al = (args == null ? 0 : args.length); Type[] argTypes = new Type[al]; for (int i = 0; i < al; i++) argTypes[i] = args[i].getType(); Method[] ms = clazz.getMethods(); for (Method m : ms) { //? if (m.isSynthetic()) continue; if (m.getName().equals(methodName) && Modifier.isPublic(m.getModifiers())) { Class<?>[] types = m.getParameterTypes(); int mal = (types == null ? 0 : types.length); if (mal == al) { boolean match = true; for (int i = 0; i < mal; i++) { //null? if (argTypes[i] == null) continue; //? if (exactMatch) { if (!argTypes[i].equals(types[i])) { match = false; break; } } //? else { Class<?> methodType = SbmUtils.narrowToClass(wrapType(types[i])); Type argType = wrapType(argTypes[i]); if (!isAncestorType(methodType, argType)) { match = false; break; } } } if (match) { method = m; break; } } } } if (method != null) result = new MethodInfo(method, methodClass); } return result; }