List of usage examples for java.lang.reflect Constructor getModifiers
@Override public int getModifiers()
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
public static boolean isPublic(Constructor<?> constructor) { return Modifier.isPublic(constructor.getModifiers()); }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
public static boolean isPrivate(Constructor<?> constructor) { return Modifier.isPrivate(constructor.getModifiers()); }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
public static boolean isProtected(Constructor<?> constructor) { return Modifier.isProtected(constructor.getModifiers()); }
From source file:org.gradle.api.internal.AbstractClassGenerator.java
private <T> Class<? extends T> generateUnderLock(Class<T> type) { Map<Class<?>, Class<?>> cache = GENERATED_CLASSES.get(getClass()); if (cache == null) { // WeakHashMap won't work here. It keeps a strong reference to the mapping value, which is the generated class in this case // However, the generated class has a strong reference to the source class (by extending it), so the keys will always be // strongly reachable while this Class is strongly reachable. Use weak references for both key and value of the mapping instead. cache = new ReferenceMap(AbstractReferenceMap.WEAK, AbstractReferenceMap.WEAK); GENERATED_CLASSES.put(getClass(), cache); }/* www. j av a 2s. co m*/ Class<?> generatedClass = cache.get(type); if (generatedClass != null) { return generatedClass.asSubclass(type); } if (Modifier.isPrivate(type.getModifiers())) { throw new GradleException( String.format("Cannot create a proxy class for private class '%s'.", type.getSimpleName())); } if (Modifier.isAbstract(type.getModifiers())) { throw new GradleException( String.format("Cannot create a proxy class for abstract class '%s'.", type.getSimpleName())); } Class<? extends T> subclass; try { ClassMetaData classMetaData = inspectType(type); ClassBuilder<T> builder = start(type, classMetaData); builder.startClass(); if (!DynamicObjectAware.class.isAssignableFrom(type)) { if (ExtensionAware.class.isAssignableFrom(type)) { throw new UnsupportedOperationException( "A type that implements ExtensionAware must currently also implement DynamicObjectAware."); } builder.mixInDynamicAware(); } if (!GroovyObject.class.isAssignableFrom(type)) { builder.mixInGroovyObject(); } builder.addDynamicMethods(); if (classMetaData.conventionAware && !IConventionAware.class.isAssignableFrom(type)) { builder.mixInConventionAware(); } Class noMappingClass = Object.class; for (Class<?> c = type; c != null && noMappingClass == Object.class; c = c.getSuperclass()) { if (c.getAnnotation(NoConventionMapping.class) != null) { noMappingClass = c; } } Set<PropertyMetaData> conventionProperties = new HashSet<PropertyMetaData>(); for (PropertyMetaData property : classMetaData.properties.values()) { if (SKIP_PROPERTIES.contains(property.name)) { continue; } if (property.injector) { builder.addInjectorProperty(property); for (Method getter : property.getters) { builder.applyServiceInjectionToGetter(property, getter); } for (Method setter : property.setters) { builder.applyServiceInjectionToSetter(property, setter); } continue; } boolean needsConventionMapping = false; if (classMetaData.isExtensible()) { for (Method getter : property.getters) { if (!Modifier.isFinal(getter.getModifiers()) && !getter.getDeclaringClass().isAssignableFrom(noMappingClass)) { needsConventionMapping = true; break; } } } if (needsConventionMapping) { conventionProperties.add(property); builder.addConventionProperty(property); for (Method getter : property.getters) { builder.applyConventionMappingToGetter(property, getter); } } if (needsConventionMapping) { for (Method setter : property.setters) { if (!Modifier.isFinal(setter.getModifiers())) { builder.applyConventionMappingToSetter(property, setter); } } } } Set<Method> actionMethods = classMetaData.missingOverloads; for (Method method : actionMethods) { builder.addActionMethod(method); } // Adds a set method for each mutable property for (PropertyMetaData property : classMetaData.properties.values()) { if (property.setters.isEmpty()) { continue; } if (Iterable.class.isAssignableFrom(property.getType())) { // Currently not supported continue; } if (property.setMethods.isEmpty()) { for (Method setter : property.setters) { builder.addSetMethod(property, setter); } } else if (conventionProperties.contains(property)) { for (Method setMethod : property.setMethods) { builder.applyConventionMappingToSetMethod(property, setMethod); } } } for (Constructor<?> constructor : type.getConstructors()) { if (Modifier.isPublic(constructor.getModifiers())) { builder.addConstructor(constructor); } } subclass = builder.generate(); } catch (Throwable e) { throw new GradleException( String.format("Could not generate a proxy class for class %s.", type.getName()), e); } cache.put(type, subclass); cache.put(subclass, subclass); return subclass; }
From source file:org.nuxeo.ecm.core.io.registry.reflect.MarshallerInspector.java
/** * Introspect this marshaller: gets instantiation mode, supported mimetype, gets the managed class, generic type and * load every needed injection to be ready to create an instance quickly. * * @since 7.2//from w ww . j av a 2s .c o m */ private void load() { // checks if there's a public constructor without parameters for (Constructor<?> constructor : clazz.getDeclaredConstructors()) { if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 0) { this.constructor = constructor; break; } } if (constructor == null) { throw new MarshallingException("No public constructor found for class " + clazz.getName() + ". Instanciation will not be possible."); } // load instantiation mode Setup setup = loadSetup(clazz); if (setup == null) { throw new MarshallingException("No required @Setup annotation found for class " + clazz.getName() + ". Instanciation will not be possible."); } if (!isReader() && !isWriter()) { throw new MarshallingException( "MarshallerInspector only supports Reader and Writer: you must implement one of this interface for this class: " + clazz.getName()); } if (isReader() && isWriter()) { throw new MarshallingException( "MarshallerInspector only supports either Reader or Writer: you must implement only one of this interface: " + clazz.getName()); } instantiation = setup.mode(); priority = setup.priority(); // load supported mimetype Supports supports = loadSupports(clazz); if (supports != null) { for (String mimetype : supports.value()) { try { MediaType mediaType = MediaType.valueOf(mimetype); this.supports.add(mediaType); } catch (IllegalArgumentException e) { log.warn("In marshaller class " + clazz.getName() + ", the declared mediatype " + mimetype + " cannot be parsed as a mimetype"); } } } if (this.supports.isEmpty()) { log.warn("The marshaller " + clazz.getName() + " does not support any mimetype. You can add some using annotation @Supports"); } // loads the marshalled type and generic type loadMarshalledType(clazz); // load properties that require injection loadInjections(clazz); // warn if several context found if (contextFields.size() > 1) { log.warn("The marshaller " + clazz.getName() + " has more than one context injected property. You probably should use a context from a parent class."); } if (instantiation == Instantiations.SINGLETON) { singleton = getNewInstance(null, true); // the context is empty since it's not required at this place (no // use - just preparing) } }
From source file:org.guicerecipes.spring.support.AutowiredMemberProvider.java
/** * Returns a new instance of the given class if its a public non abstract class which has a public zero argument constructor otherwise returns null *//* ww w . j ava 2s .c om*/ protected Object tryCreateInstance(Class<?> type) { Object answer = null; int modifiers = type.getModifiers(); if (!Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers) && !type.isInterface()) { // if its a concrete class with no args make one Constructor<?> constructor = null; try { constructor = type.getConstructor(); } catch (NoSuchMethodException e) { // ignore } if (constructor != null) { if (Modifier.isPublic(constructor.getModifiers())) { try { answer = constructor.newInstance(); } catch (InstantiationException e) { throw new ProvisionException("Failed to instantiate " + constructor, e); } catch (IllegalAccessException e) { throw new ProvisionException("Failed to instantiate " + constructor, e); } catch (InvocationTargetException ie) { Throwable e = ie.getTargetException(); throw new ProvisionException("Failed to instantiate " + constructor, e); } } } } return answer; }
From source file:org.evosuite.setup.TestClusterGenerator.java
protected static void makeAccessible(Constructor<?> constructor) { if (!Modifier.isPublic(constructor.getModifiers()) || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) { constructor.setAccessible(true); }/*from w w w. j a va 2 s .c o m*/ }
From source file:org.evosuite.testcarver.testcase.EvoTestCaseCodeGenerator.java
private boolean hasDefaultConstructor(final Class<?> clazz) { for (final Constructor<?> c : clazz.getConstructors()) { if (c.getParameterTypes().length == 0 && Modifier.isPublic(c.getModifiers())) { return true; }//from w w w .j av a 2 s .co m } return false; }
From source file:org.evosuite.setup.TestUsageChecker.java
public static boolean canUse(Constructor<?> c) { if (c.isSynthetic()) { return false; }// w w w .j av a 2 s. c om // synthetic constructors are OK if (Modifier.isAbstract(c.getDeclaringClass().getModifiers())) return false; // TODO we could enable some methods from Object, like getClass //if (c.getDeclaringClass().equals(java.lang.Object.class)) // return false;// handled here to avoid printing reasons if (c.getDeclaringClass().equals(java.lang.Thread.class)) return false;// handled here to avoid printing reasons if (c.getDeclaringClass().isAnonymousClass()) return false; if (c.getDeclaringClass().isLocalClass()) { logger.debug("Skipping constructor of local class " + c.getName()); return false; } if (c.getDeclaringClass().isMemberClass() && !TestUsageChecker.canUse(c.getDeclaringClass())) return false; if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) { final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && !c.getDeclaringClass().equals(targetClass)) { logger.debug("Excluding deprecated constructor " + c.getName()); return false; } } if (isForbiddenNonDeterministicCall(c)) { return false; } if (Modifier.isPublic(c.getModifiers())) { TestClusterUtils.makeAccessible(c); return true; } for (java.lang.reflect.Type paramType : c.getGenericParameterTypes()) { if (!canUse(paramType)) return false; } // If default access rights, then check if this class is in the same package as the target class if (!Modifier.isPrivate(c.getModifiers())) { // && !Modifier.isProtected(c.getModifiers())) { String packageName = ClassUtils.getPackageName(c.getDeclaringClass()); if (packageName.equals(Properties.CLASS_PREFIX)) { TestClusterUtils.makeAccessible(c); return true; } } return false; }
From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java
private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) { try {/*from w w w .j a v a 2 s.c o m*/ if (type.isArray()) { Class<?> componentType = type.getComponentType(); // TODO - only handles 2-dimensional arrays if (componentType.isArray()) { Object array = Array.newInstance(componentType, 1); Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0)); return array; } else { return Array.newInstance(componentType, 0); } } else if (Collection.class.isAssignableFrom(type)) { TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null); return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16); } else if (Map.class.isAssignableFrom(type)) { TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null); return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16); } else { Constructor<?> ctor = type.getDeclaredConstructor(); if (Modifier.isPrivate(ctor.getModifiers())) { throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor); } return BeanUtils.instantiateClass(ctor); } } catch (Throwable ex) { throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name, "Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path", ex); } }