List of usage examples for java.lang.reflect Modifier isPublic
public static boolean isPublic(int mod)
From source file:org.ajax4jsf.builder.config.ComponentBaseBean.java
/** * Subclasses should extend this method to provide specifc checks * /*from w w w .ja v a2 s .c o m*/ * Check existing and default properties * For properties filled from configuration, attempt to set additional parameters. * If base class have any bean properties, append it to configured * @throws ConfigurationException */ public void checkProperties() throws ParsingException { try { getLog().debug("Parse properties for Component " + getName() + " with superclass " + getSuperclass()); if (getSuperclass() != null) { Class<?> superClass = getLoader().loadClass(getSuperclass()); new ClassWalkingLogic(superClass).walk(new ClassVisitor() { public void visit(Class<?> clazz) { checkPropertiesForClass(clazz); } }); } } catch (ClassNotFoundException e) { getLog().error("superclass not found for component " + getName(), e); } if (null != getTag()) { try { Class superClass = getLoader().loadClass(getTag().getSuperclass()); PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(superClass); // for all properties, add it to component. If property have not abstract getter/setter , // add it with exist = true . If property in list of hidden names, set hidden = true. for (int i = 0; i < properties.length; i++) { PropertyDescriptor descriptor = properties[i]; Method writeMethod = descriptor.getWriteMethod(); if (containProperty(descriptor.getName())) { if (null != writeMethod && !Modifier.isAbstract(writeMethod.getModifiers()) && Modifier.isPublic(writeMethod.getModifiers())) { ((PropertyBean) this.properties.get(descriptor.getName())).setExistintag(true); } } else if (null != writeMethod && Modifier.isPublic(writeMethod.getModifiers())) { if (Arrays.asList(enabledTagProperties).contains(descriptor.getName())) { Class type = descriptor.getPropertyType(); getLog().debug("Register tag property " + descriptor.getName() + " with type name " + type.getCanonicalName()); PropertyBean property = new PropertyBean(); property.setName(descriptor.getName()); property.setDescription(descriptor.getShortDescription()); property.setDisplayname(descriptor.getDisplayName()); property.setClassname(descriptor.getPropertyType().getCanonicalName()); property.setExist(true); if (!Modifier.isAbstract(writeMethod.getModifiers())) { property.setExistintag(true); } addProperty(property); } } } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block getLog().error("superclass not found for tag " + getTag().getName(), e); } } }
From source file:com.holonplatform.core.internal.property.DefaultPropertySetRefIntrospector.java
@Override public PropertySet<?> getPropertySet(PropertySetRef annotation) throws PropertySetIntrospectionException { ObjectUtils.argumentNotNull(annotation, "PropertySetRef annotation must be not null"); // reference class final Class<?> cls = annotation.value(); if (cls == null) { throw new PropertySetIntrospectionException("[PropertySetRef] missing value"); }/*w w w. j av a 2s.c o m*/ // field name String fieldName = AnnotationUtils.getStringValue(annotation.field()); boolean wasNullFieldName = (fieldName == null); if (fieldName == null) { // check cache synchronized (cache) { final CacheKey key = new CacheKey(cls, null); if (cache.containsKey(key)) { return cache.get(key); } } if (PropertySet.class == cls) { throw new PropertySetIntrospectionException( "Invalid PropertySetRef class value: [" + cls.getName() + "]"); } // If the class itself is a PropertySet, try to instantiate it if (PropertySet.class.isAssignableFrom(cls)) { try { return (PropertySet<?>) cls.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new PropertySetIntrospectionException( "[PropertySetRef] Failed to instantiate PropertySet class [" + cls.getName() + "]", e); } } // Look for a public static PropertySet type field List<String> candidateFieldNames = new LinkedList<>(); Field[] flds = cls.getDeclaredFields(); if (flds != null) { for (Field fld : flds) { if (Modifier.isStatic(fld.getModifiers()) && Modifier.isPublic(fld.getModifiers()) && PropertySet.class.isAssignableFrom(fld.getType())) { candidateFieldNames.add(fld.getName()); } } } if (candidateFieldNames.isEmpty()) { throw new PropertySetIntrospectionException( "[PropertySetRef] Cannot find any valid public static PropertySet type field in class [" + cls.getName() + "]"); } if (candidateFieldNames.size() > 1) { throw new PropertySetIntrospectionException( "[PropertySetRef] More than one valid PropertySet type field found in class [" + cls.getName() + "]: please specify the field name to use in PropertySetRef annotation. Detected PropertySet fields: [" + candidateFieldNames + "]"); } fieldName = candidateFieldNames.get(0); } else { // check cache synchronized (cache) { final CacheKey key = new CacheKey(cls, fieldName); if (cache.containsKey(key)) { return cache.get(key); } } } if (LOGGER.isEnabled(Level.DEBUG)) { final String fn = fieldName; LOGGER.debug(() -> "Get PropertySet using PropertySetRef annotation for class [" + cls + "] and field name [" + fn + "]"); } // Read the PropertySet field try { Object value = FieldUtils.readStaticField(cls, fieldName); if (value == null) { throw new PropertySetIntrospectionException("[PropertySetRef] The field [" + fieldName + "] in class [" + cls.getName() + "] has null value"); } if (!PropertySet.class.isAssignableFrom(value.getClass())) { throw new PropertySetIntrospectionException( "[PropertySetRef] The field [" + fieldName + "] in class [" + cls.getName() + "] is not of PropertySet type but [" + value.getClass().getName() + "]"); } final PropertySet<?> propertySet = (PropertySet<?>) value; // put in cache and return if (wasNullFieldName) { cache.putIfAbsent(new CacheKey(cls, null), propertySet); } PropertySet<?> existing = cache.putIfAbsent(new CacheKey(cls, fieldName), propertySet); return (existing != null ? existing : propertySet); } catch (IllegalAccessException e) { throw new PropertySetIntrospectionException( "[PropertySetRef] Failed to read field [" + fieldName + "] from class [" + cls.getName() + "]", e); } }
From source file:org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass.java
private void methodStrategy(Collection<String> methodNames) { Class superClass = getClazz(); while (superClass != null && superClass != Object.class && superClass != GroovyObject.class) { for (Method method : superClass.getMethods()) { if (Modifier.isPublic(method.getModifiers()) && method.getAnnotation(Action.class) != null) { String methodName = method.getName(); if (!methodName.endsWith(FLOW_SUFFIX)) { methodNames.add(methodName); configureMappingForMethodAction(methodName); }//from w ww.j a va2s.c om } } superClass = superClass.getSuperclass(); } if (!isActionMethod(defaultActionName) && methodNames.size() == 1 && !isReadableProperty("scaffold")) { defaultActionName = methodNames.iterator().next(); } }
From source file:org.evosuite.assertion.InspectorManager.java
private boolean isInspectorMethod(Method method) { if (!Modifier.isPublic(method.getModifiers())) return false; if (!method.getReturnType().isPrimitive() && !method.getReturnType().equals(String.class) && !method.getReturnType().isEnum() && !ClassUtils.isPrimitiveWrapper(method.getReturnType())) { return false; }/* w w w .j ava 2 s . c o m*/ if (method.getReturnType().equals(void.class)) return false; if (method.getParameterTypes().length != 0) return false; if (method.getName().equals("hashCode")) return false; if (method.getDeclaringClass().equals(Object.class)) return false; if (method.getDeclaringClass().equals(Enum.class)) return false; if (method.isSynthetic()) return false; if (method.isBridge()) return false; if (method.getName().equals("pop")) return false; if (isBlackListed(method)) return false; if (isImpureJDKMethod(method)) return false; if (isAWTToString(method)) return false; if (Properties.PURE_INSPECTORS) { if (!CheapPurityAnalyzer.getInstance().isPure(method)) { return false; } } return true; }
From source file:fr.imag.model2roo.addon.graph.NodeEntityMetadata.java
private MethodMetadataBuilder getIdentifierAccessor() { if (parent != null) { final MethodMetadataBuilder parentIdAccessor = parent.getIdentifierAccessor(); if (parentIdAccessor != null && parentIdAccessor.getReturnType().equals(idType)) { return parentIdAccessor; }/*from w ww .j av a2s .co m*/ } JavaSymbolName requiredAccessorName = BeanInfoUtils.getAccessorMethodName(idField); // See if the user provided the field if (!getId().equals(idField.getDeclaredByMetadataId())) { // Locate an existing accessor final MethodMetadata method = memberDetails.getMethod(requiredAccessorName, new ArrayList<JavaType>()); if (method != null) { if (Modifier.isPublic(method.getModifier())) { // Method exists and is public so return it return new MethodMetadataBuilder(method); } // Method is not public so make the required accessor name // unique requiredAccessorName = new JavaSymbolName(requiredAccessorName.getSymbolName() + "_"); } } // We declared the field in this ITD, so produce a public accessor for // it final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); bodyBuilder.appendFormalLine("return this." + idField.getFieldName().getSymbolName() + ";"); return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, requiredAccessorName, idField.getFieldType(), bodyBuilder); }
From source file:com.strandls.alchemy.rest.client.RestInterfaceAnalyzer.java
/** * Analyze the class and build the meta information. * * @param klass/*from ww w. j a v a 2s .co m*/ * the class. * @return analyzed metadata. * @throws NotRestInterfaceException * if the class analyzed is not a rest interface. */ protected RestInterfaceMetadata doAnalyze(final Class<?> klass) throws NotRestInterfaceException { final List<String> produced = new ArrayList<String>(); final List<String> consumed = new ArrayList<String>(); String path = null; @SuppressWarnings("unchecked") final Set<Annotation> declaredAnnotationList = ReflectionUtils.getAllAnnotations(klass); for (final Annotation annotation : declaredAnnotationList) { if (annotation instanceof Path) { path = ((Path) annotation).value(); } else if (annotation instanceof Produces) { final Produces produces = (Produces) annotation; final String[] values = produces.value(); produced.addAll(Arrays.asList(values)); } else if (annotation instanceof Consumes) { final Consumes consumes = (Consumes) annotation; final String[] values = consumes.value(); consumed.addAll(Arrays.asList(values)); } } final Map<Method, RestMethodMetadata> methodMetadataMap = new LinkedHashMap<Method, RestMethodMetadata>(); @SuppressWarnings("unchecked") final Set<Method> methods = ReflectionUtils.getAllMethods(klass, new Predicate<Method>() { @Override public boolean apply(final Method input) { // return only public methods return Modifier.isPublic(input.getModifiers()); } }); for (final Method method : methods) { final RestMethodMetadata methodMetadata = analyzeMethod(method); if (methodMetadata != null) { methodMetadataMap.put(method, methodMetadata); } } if (methodMetadataMap.isEmpty()) { // not a valid rest interface. throw new NotRestInterfaceException(klass); } return new RestInterfaceMetadata(path, produced, consumed, methodMetadataMap); }
From source file:org.evosuite.setup.TestUsageChecker.java
public static boolean canUse(Class<?> c) { //if (Throwable.class.isAssignableFrom(c)) // return false; if (Modifier.isPrivate(c.getModifiers())) return false; if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) { final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && !c.equals(targetClass)) { logger.debug("Skipping deprecated class " + c.getName()); return false; }/*from www .jav a 2s. c o m*/ } if (c.isAnonymousClass()) { return false; } if (c.getName().startsWith("junit")) return false; if (TestClusterUtils.isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) { return false; } if (c.getEnclosingClass() != null) { if (!canUse(c.getEnclosingClass())) return false; } if (c.getDeclaringClass() != null) { if (!canUse(c.getDeclaringClass())) return false; } // If the SUT is not in the default package, then // we cannot import classes that are in the default // package if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) { return false; } if (c.getName().contains("EnhancerByMockito")) { return false; } // TODO: This should be unnecessary if Java reflection works... // This is inefficient if (TestClusterUtils.isAnonymousClass(c.getName())) { String message = c + " looks like an anonymous class, ignoring it (although reflection says " + c.isAnonymousClass() + ") " + c.getSimpleName(); LoggingUtils.logWarnAtMostOnce(logger, message); return false; } if (Modifier.isPublic(c.getModifiers())) { return true; } // 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); if (packageName.equals(Properties.CLASS_PREFIX)) { return true; } } logger.debug("Not public"); return false; }
From source file:io.github.benas.randombeans.util.ReflectionUtils.java
/** * Check if the type is public.// ww w. j a v a 2 s . c om * * @param type the type to check * @param <T> the actual type to check * @return true if the type is public, false otherwise */ public static <T> boolean isPublic(final Class<T> type) { return Modifier.isPublic(type.getModifiers()); }
From source file:com.glaf.core.util.ReflectUtils.java
public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException { Constructor<?> targetConstructor; try {//from w w w . j a va2 s . com targetConstructor = clazz.getConstructor(new Class<?>[] { paramType }); } catch (NoSuchMethodException e) { targetConstructor = null; Constructor<?>[] constructors = clazz.getConstructors(); for (Constructor<?> constructor : constructors) { if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0].isAssignableFrom(paramType)) { targetConstructor = constructor; break; } } if (targetConstructor == null) { throw e; } } return targetConstructor; }
From source file:org.apache.bval.util.reflection.Reflection.java
/** * Set the accessibility of {@code o} to {@code accessible}. If running without a {@link SecurityManager} * and {@code accessible == false}, this call is ignored (because any code could reflectively make any * object accessible at any time).//from w ww.j a v a 2 s . c o m * @param o * @param accessible * @return whether a change was made. */ public static boolean setAccessible(final AccessibleObject o, boolean accessible) { if (o == null || o.isAccessible() == accessible) { return false; } if (!accessible && System.getSecurityManager() == null) { return false; } final Member m = (Member) o; // For public members whose declaring classes are public, we need do nothing: if (Modifier.isPublic(m.getModifiers()) && Modifier.isPublic(m.getDeclaringClass().getModifiers())) { return false; } o.setAccessible(accessible); return true; }