List of usage examples for java.lang Class getDeclaredConstructors
@CallerSensitive public Constructor<?>[] getDeclaredConstructors() throws SecurityException
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 www. j a va2s. c om 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.cellprofiler.preferences.CellProfilerPreferences.java
/** * Get the preferences factory supplied by the JRE or * provided as a service./*w ww .ja va 2 s . c om*/ * * @return the default preferences factory. */ static private PreferencesFactory getJREPreferencesFactory() { synchronized (lock) { if (delegatePreferencesFactory == null) { do { /* * First, see if there is a PreferencesFactory * provided as a service. */ final ServiceLoader<PreferencesFactory> pfServiceLoader = ServiceLoader .loadInstalled(PreferencesFactory.class); final Iterator<PreferencesFactory> pfIter = pfServiceLoader.iterator(); if (pfIter.hasNext()) { delegatePreferencesFactory = pfIter.next(); break; } /* * Next, try the WindowsPreferencesFactory if OS is Windows. */ String pfName = (SystemUtils.IS_OS_WINDOWS) ? "java.util.prefs.WindowsPreferencesFactory" : "java.util.prefs.FilePreferencesFactory"; try { Class<?> pfClass = Class.forName("java.util.prefs.WindowsPreferencesFactory", false, null); Class<?> pfFuckYou = Class.forName("java.util.prefs.WindowsPreferences", true, null); Constructor<?>[] pfConstructors = pfClass.getDeclaredConstructors(); for (Constructor<?> c : pfConstructors) { if (c.getParameterTypes().length == 0) { /* * Bad boy - it's package-private AND I CALL IT ANYWAY BAH HA HA HA HA HA HA */ c.setAccessible(true); delegatePreferencesFactory = (PreferencesFactory) c.newInstance(new Object[0]); break; } } break; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * And as a last resort, there's always our headless * preferences factory. */ delegatePreferencesFactory = new HeadlessPreferencesFactory(); } while (false); } } return delegatePreferencesFactory; }
From source file:com.zc.util.refelect.Reflector.java
/** * * ?annotationClass/*from w w w. j av a2 s.com*/ * * @param targetClass * Class * @param annotationClass * Class * * @return List */ public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) { List<T> result = new ArrayList<T>(); Annotation annotation = targetClass.getAnnotation(annotationClass); if (annotation != null) { result.add((T) annotation); } Constructor[] constructors = targetClass.getDeclaredConstructors(); // ? CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator()); Field[] fields = targetClass.getDeclaredFields(); // ? CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator()); Method[] methods = targetClass.getDeclaredMethods(); // ? CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator()); for (Class<?> superClass = targetClass.getSuperclass(); superClass == null || superClass == Object.class; superClass = superClass.getSuperclass()) { List<T> temp = (List<T>) getAnnotations(superClass, annotationClass); if (CollectionUtils.isNotEmpty(temp)) { CollectionUtils.addAll(result, temp.iterator()); } } return result; }
From source file:com.google.dexmaker.ProxyBuilder.java
@SuppressWarnings("unchecked") private static <T> Constructor<T>[] getConstructorsToOverwrite(Class<T> clazz) { return (Constructor<T>[]) clazz.getDeclaredConstructors(); }
From source file:com.helpinput.utils.Utils.java
public static <T> Constructor<?> findConstructor(Class<T> targetClass, Class<?>[] agrsClasses) { Constructor<?>[] constructors = targetClass.getDeclaredConstructors(); Constructor<?> theConstructor = null; for (Constructor<?> constructor : constructors) { Class<?>[] classes = constructor.getParameterTypes(); if (agrsClasses.length == 0 && classes.length == 0) { theConstructor = constructor; break; }// w w w . ja v a2 s. com if (agrsClasses.length == classes.length) { for (int i = 0; i < agrsClasses.length; i++) { if (agrsClasses[i] == null) { if (i == agrsClasses.length - 1) { theConstructor = constructor; break; } continue; } if (classes[i].isPrimitive()) { if (!primitiveWrap(classes[i]).isAssignableFrom(agrsClasses[i])) break; } else if (!classes[i].isAssignableFrom(agrsClasses[i])) break; if (i == agrsClasses.length - 1) { theConstructor = constructor; break; } } } if (theConstructor != null) break; } if (null != theConstructor) { if (!theConstructor.isAccessible()) theConstructor.setAccessible(true); return theConstructor; } else { if (targetClass.getSuperclass() != null) { return findConstructor(targetClass.getSuperclass(), agrsClasses); } return null; } }
From source file:com.helpinput.core.Utils.java
@SuppressWarnings("unchecked") public static <T> Constructor<?> findConstructor(Object target, Object... args) { Class<?> targetClass = getClass(target); Class<?>[] agrClasses = getArgClasses(args); Constructor<?>[] constructors = targetClass.getDeclaredConstructors(); return (Constructor<T>) findMember(constructors, targetClass, null, agrClasses); }
From source file:com.sm.query.utils.QueryUtils.java
public static Object createInstance(Class<?> cls) throws Exception { Constructor<?> constructor = null; Object[] constructorArgs = null; Constructor<?>[] constructors = cls.getDeclaredConstructors(); //take the first constructor if (constructors.length > 0) { constructor = constructors[0];//w w w.j av a 2 s. co m constructor.setAccessible(true); Class<?>[] params = constructor.getParameterTypes(); constructorArgs = new Object[params.length]; for (int i = 0; i < params.length; i++) { constructorArgs[i] = getParamArg(params[i]); } return constructor.newInstance(constructorArgs); } else return cls.newInstance(); }
From source file:org.eclipse.wb.internal.core.model.description.helpers.ComponentDescriptionHelper.java
/** * Adds {@link ConstructorDescription} for given {@link ComponentDescription}. *//*from w w w. j ava 2 s. c o m*/ private static void addConstructors(IJavaProject javaProject, ComponentDescription componentDescription) throws Exception { Class<?> componentClass = componentDescription.getComponentClass(); for (Constructor<?> constructor : componentClass.getDeclaredConstructors()) { constructor.setAccessible(true); ConstructorDescription constructorDescription = new ConstructorDescription(componentClass); // add parameter descriptions of constructor for (Class<?> parameterType : constructor.getParameterTypes()) { addParameter(constructorDescription, parameterType); } // OK, add constructor description constructorDescription.postProcess(); componentDescription.addConstructor(constructorDescription); } }
From source file:com.nerve.commons.repository.utils.reflection.ReflectionUtils.java
/** * * ?annotationClass//from www . j av a 2 s . co m * * @param targetClass * Class * @param annotationClass * Class * * @return List */ public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) { Assert.notNull(targetClass, "targetClass?"); Assert.notNull(annotationClass, "annotationClass?"); List<T> result = new ArrayList<T>(); Annotation annotation = targetClass.getAnnotation(annotationClass); if (annotation != null) { result.add((T) annotation); } Constructor[] constructors = targetClass.getDeclaredConstructors(); // ? CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator()); Field[] fields = targetClass.getDeclaredFields(); // ? CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator()); Method[] methods = targetClass.getDeclaredMethods(); // ? CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator()); for (Class<?> superClass = targetClass.getSuperclass(); superClass == null || superClass == Object.class; superClass = superClass.getSuperclass()) { List<T> temp = (List<T>) getAnnotations(superClass, annotationClass); if (CollectionUtils.isNotEmpty(temp)) { CollectionUtils.addAll(result, temp.iterator()); } } return result; }
From source file:Main.java
public static Constructor<?> findConstructorBestMatch(Class<?> clazz, Class<?>... parameterTypes) { StringBuilder sb = new StringBuilder(clazz.getName()); sb.append(getParametersString(parameterTypes)); sb.append("#bestmatch"); String fullConstructorName = sb.toString(); if (constructorCache.containsKey(fullConstructorName)) { Constructor<?> constructor = constructorCache.get(fullConstructorName); if (constructor == null) throw new NoSuchMethodError(fullConstructorName); return constructor; }/*from w w w .ja v a 2s .c om*/ try { Constructor<?> constructor = findConstructorExact(clazz, parameterTypes); constructorCache.put(fullConstructorName, constructor); return constructor; } catch (NoSuchMethodError ignored) { } Constructor<?> bestMatch = null; Constructor<?>[] constructors = clazz.getDeclaredConstructors(); for (Constructor<?> constructor : constructors) { // compare name and parameters // if (ClassUtils.isAssignable(parameterTypes, constructor.getParameterTypes(), true)) { // // get accessible version of method // if (bestMatch == null || MemberUtils.compareParameterTypes( // constructor.getParameterTypes(), // bestMatch.getParameterTypes(), // parameterTypes) < 0) { // bestMatch = constructor; // } // } } if (bestMatch != null) { bestMatch.setAccessible(true); constructorCache.put(fullConstructorName, bestMatch); return bestMatch; } else { NoSuchMethodError e = new NoSuchMethodError(fullConstructorName); constructorCache.put(fullConstructorName, null); throw e; } }