List of usage examples for java.lang.reflect Constructor getModifiers
@Override public int getModifiers()
From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java
/** * Get all the classes referenced by the given class, which might be used by an extension. We consider visible * methods, constructors, fields and inner classes, as well as superclasses and interfaces extended by the class. * * @param initialClass The class to analyse. * @param consideredClasses Classes that have already been considered, and which should not be considered again. If * the given class has already been considered then an empty set will be returned. This set will be * updated with the given class. * @return The set of classes that might be accessible by an extension of this class. *//*from w w w.j a va 2 s . co m*/ private static Set<Class<?>> getReferencedClassesFromClass(Class<?> initialClass, Set<Class<?>> consideredClasses) { Set<Class<?>> referencedClasses = new HashSet<>(); if (consideredClasses.add(initialClass)) { for (Method method : initialClass.getDeclaredMethods()) { if (isVisibleToExtender(method.getModifiers())) { referencedClasses.addAll(getClassesFromMethod(method)); } } for (Constructor<?> constructor : initialClass.getDeclaredConstructors()) { if (isVisibleToExtender(constructor.getModifiers())) { referencedClasses.addAll(getClassesFromConstructor(constructor)); } } for (Field field : initialClass.getDeclaredFields()) { if (isVisibleToExtender(field.getModifiers())) { referencedClasses.addAll(getClassesFromField(field)); } } for (Class<?> clazz : initialClass.getDeclaredClasses()) { if (isVisibleToExtender(clazz.getModifiers())) { referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses)); } } if (initialClass.getSuperclass() != null) { referencedClasses .addAll(getReferencedClassesFromClass(initialClass.getSuperclass(), consideredClasses)); } for (Class<?> clazz : initialClass.getInterfaces()) { referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses)); } } return referencedClasses; }
From source file:org.seedstack.seed.core.utils.BaseClassSpecifications.java
/** * @return a specification which check if a least one constructor is public *//*from w w w.j av a 2 s . c o m*/ public static Specification<Class<?>> classConstructorIsPublic() { return new AbstractSpecification<Class<?>>() { @Override public boolean isSatisfiedBy(Class<?> candidate) { for (Constructor<?> constructor : candidate.getDeclaredConstructors()) { if (Modifier.isPublic(constructor.getModifiers())) { return true; } } return false; } }; }
From source file:com.espertech.esper.util.MethodResolver.java
public static Constructor resolveCtor(Class declaringClass, Class[] paramTypes) throws EngineNoSuchCtorException { // Get all the methods for this class Constructor[] ctors = declaringClass.getConstructors(); Constructor bestMatch = null; int bestConversionCount = -1; // Examine each method, checking if the signature is compatible Constructor conversionFailedCtor = null; for (Constructor ctor : ctors) { // Check the modifiers: we only want public if (!Modifier.isPublic(ctor.getModifiers())) { continue; }//from w ww . j av a 2s. c om // Check the parameter list int conversionCount = compareParameterTypesNoContext(ctor.getParameterTypes(), paramTypes, null, null, ctor.getGenericParameterTypes()); // Parameters don't match if (conversionCount == -1) { conversionFailedCtor = ctor; continue; } // Parameters match exactly if (conversionCount == 0) { bestMatch = ctor; break; } // No previous match if (bestMatch == null) { bestMatch = ctor; bestConversionCount = conversionCount; } else { // Current match is better if (conversionCount < bestConversionCount) { bestMatch = ctor; bestConversionCount = conversionCount; } } } if (bestMatch != null) { return bestMatch; } else { StringBuilder parameters = new StringBuilder(); String message = "Constructor not found for " + declaringClass.getSimpleName() + " taking "; if (paramTypes != null && paramTypes.length != 0) { String appendString = ""; for (Object param : paramTypes) { parameters.append(appendString); if (param == null) { parameters.append("(null)"); } else { parameters.append(param.toString()); } appendString = ", "; } message += "('" + parameters + "')'"; } else { message += "no parameters"; } throw new EngineNoSuchCtorException(message, conversionFailedCtor); } }
From source file:Mopex.java
/** * Returns a String that represents the header for a constructor. * //from w w w.ja v a 2s. c om * @return String * @param c * java.lang.Constructor */ //start extract constructorHeaderToString public static String headerToString(Constructor c) { String mods = Modifier.toString(c.getModifiers()); if (mods.length() == 0) return headerSuffixToString(c); else return mods + " " + headerSuffixToString(c); }
From source file:com.impetus.kundera.utils.KunderaCoreUtils.java
/** * @param clazz/*from w w w . j a v a 2 s .c o m*/ * @return */ public static Object createNewInstance(Class clazz) { Object target = null; try { Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor constructor : constructors) { if ((Modifier.isProtected(constructor.getModifiers()) || Modifier.isPublic(constructor.getModifiers())) && constructor.getParameterTypes().length == 0) { constructor.setAccessible(true); target = constructor.newInstance(); constructor.setAccessible(false); break; } } return target; } catch (InstantiationException iex) { logger.error("Error while creating an instance of {} .", clazz); throw new PersistenceException(iex); } catch (IllegalAccessException iaex) { logger.error("Illegal Access while reading data from {}, Caused by: .", clazz, iaex); throw new PersistenceException(iaex); } catch (Exception e) { logger.error("Error while creating an instance of {}, Caused by: .", clazz, e); throw new PersistenceException(e); } }
From source file:org.openspaces.test.client.executor.ExecutorUtils.java
/** * Ensure that the class is valid, that is, that it has appropriate access: The class is public * and has public constructor with no-args. */// w ww. ja v a 2 s .com @SuppressWarnings({ "rawtypes", "unchecked" }) public static void ensureValidClass(Class claz) { boolean ctorOK = false; try { // check if defined public constructor if (!Modifier.isPublic(claz.getModifiers())) { throw new IllegalArgumentException("Class [" + claz.getName() + "] not public"); } Constructor ctor = claz.getConstructor(new Class[0]); ctorOK = Modifier.isPublic(ctor.getModifiers()); } catch (NoSuchMethodException e) { ctorOK = false; } catch (SecurityException e) { ctorOK = false; } if (!ctorOK) { throw new IllegalArgumentException("Class [" + claz.getName() + "] needs public no-arg constructor"); } }
From source file:com.liferay.cli.support.util.ReflectionUtils.java
/** * Make the given constructor accessible, explicitly setting it accessible * if necessary. The <code>setAccessible(true)</code> method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active).//from w w w.j av a 2 s .co m * * @param ctor the constructor to make accessible * @see java.lang.reflect.Constructor#setAccessible */ public static void makeAccessible(final Constructor<?> ctor) { if (!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) { ctor.setAccessible(true); } }
From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java
/** * Make the given constructor accessible, explicitly setting it accessible * if necessary. The {@code setAccessible(true)} method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active).//www. ja v a 2 s . c om * @param ctor the constructor to make accessible * @see java.lang.reflect.Constructor#setAccessible */ public static void makeAccessible(Constructor<?> ctor) { if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { ctor.setAccessible(true); } }
From source file:com.google.dexmaker.ProxyBuilder.java
private static <T, G extends T> void generateConstructorsAndFields(DexMaker dexMaker, TypeId<G> generatedType, TypeId<T> superType, Class<T> superClass) { TypeId<InvocationHandler> handlerType = TypeId.get(InvocationHandler.class); TypeId<Method[]> methodArrayType = TypeId.get(Method[].class); FieldId<G, InvocationHandler> handlerField = generatedType.getField(handlerType, FIELD_NAME_HANDLER); dexMaker.declare(handlerField, PRIVATE, null); FieldId<G, Method[]> allMethods = generatedType.getField(methodArrayType, FIELD_NAME_METHODS); dexMaker.declare(allMethods, PRIVATE | STATIC, null); for (Constructor<T> constructor : getConstructorsToOverwrite(superClass)) { if (constructor.getModifiers() == Modifier.FINAL) { continue; }// w ww . j a v a 2 s.c om TypeId<?>[] types = classArrayToTypeArray(constructor.getParameterTypes()); MethodId<?, ?> method = generatedType.getConstructor(types); Code constructorCode = dexMaker.declare(method, PUBLIC); Local<G> thisRef = constructorCode.getThis(generatedType); Local<?>[] params = new Local[types.length]; for (int i = 0; i < params.length; ++i) { params[i] = constructorCode.getParameter(i, types[i]); } MethodId<T, ?> superConstructor = superType.getConstructor(types); constructorCode.invokeDirect(superConstructor, null, thisRef, params); constructorCode.returnVoid(); } }
From source file:org.openstreetmap.josm.plugins.conflation.config.parser.InstanceConstructor.java
/** * Return the class' public constructor with the largest number of arguments. *///from w w w . j a va 2 s .com private Constructor<?> getLargestPublicConstructor(Class<?> type) { Constructor<?> bestCnstr = null; for (Constructor<?> cnstr : type.getConstructors()) { if (Modifier.isPublic(cnstr.getModifiers()) && ((bestCnstr == null) || (cnstr.getParameterTypes().length > bestCnstr.getParameterTypes().length))) bestCnstr = cnstr; } return bestCnstr; }