List of usage examples for java.lang.reflect Modifier isPublic
public static boolean isPublic(int mod)
From source file:org.grails.datastore.mapping.reflect.ClassPropertyFetcher.java
private void processMethod(Method method) { if (method.isSynthetic()) { return;/* www . ja va2s . c om*/ } 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.alex73.skarynka.scan.Book2.java
private void get(Object obj, String prefix, List<String> lines) throws Exception { for (Field f : obj.getClass().getFields()) { if (Modifier.isPublic(f.getModifiers()) && !Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) { if (f.getType() == int.class) { int v = f.getInt(obj); if (v != -1) { lines.add(prefix + f.getName() + "=" + v); }//www. j av a 2 s .c o m } else if (f.getType() == boolean.class) { lines.add(prefix + f.getName() + "=" + f.getBoolean(obj)); } else if (f.getType() == String.class) { String s = (String) f.get(obj); if (s != null) { lines.add(prefix + f.getName() + "=" + s); } } else if (Set.class.isAssignableFrom(f.getType())) { Set<?> set = (Set<?>) f.get(obj); StringBuilder t = new StringBuilder(); for (Object o : set) { t.append(o.toString()).append(';'); } if (t.length() > 0) { t.setLength(t.length() - 1); } lines.add(prefix + f.getName() + "=" + t); } else { throw new RuntimeException("Unknown field class for get '" + f.getName() + "'"); } } } }
From source file:org.batoo.jpa.parser.impl.metadata.type.ManagedTypeMetadatImpl.java
/** * Infers and returns the access type based on all persistence annotations being on fields or methods and parent parent access type. * /*from www . ja v a 2s . c om*/ * @param parentAccessType * the parent access type * @return the inferred access type * * @since 2.0.0 */ private AccessType inferAccessType(AccessType parentAccessType) { boolean methodsHasAnnotations = false; boolean fieldsHasAnnotations = false; final List<String> alternated = Lists.newArrayList(); final Field[] fields = this.clazz.getDeclaredFields(); final Method[] methods = this.clazz.getDeclaredMethods(); // find the alternated ones with @Access for (final Method m : methods) { // skip static and private methods. final int mods = m.getModifiers(); if (Modifier.isStatic(mods) || !Modifier.isPublic(mods) || m.isBridge() || m.isSynthetic()) { continue; } if ((m.getParameterTypes().length != 0) || (m.getReturnType() == null)) { continue; } final Access access = m.getAnnotation(Access.class); if (access != null) { final String name = m.getName(); if ((m.getReturnType() == boolean.class) && name.startsWith("is")) { alternated.add(StringUtils.capitalize(name.substring(2))); } else if (name.startsWith("get")) { alternated.add(StringUtils.capitalize(name.substring(3))); } } } for (final Field f : fields) { final Access access = f.getAnnotation(Access.class); if (access != null) { alternated.add(StringUtils.capitalize(f.getName())); } } // check methods for (final Method m : methods) { for (final Annotation a : m.getAnnotations()) { // ignore @Access(PROPERTY) if (a instanceof Access) { if (((Access) a).value() != AccessType.PROPERTY) { continue; } } // ignore @Transient if (a instanceof Transient) { continue; } if ((m.getReturnType() == null) || (m.getParameterTypes().length > 0)) { continue; } String name = a.annotationType().getName(); // ignore the listener annotations if (name.startsWith("javax.persistence.Post") || name.startsWith("javax.persistence.Pre")) { continue; } if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) { name = m.getName(); if ((boolean.class == m.getReturnType()) || name.startsWith("is")) { name = name.substring(2); } else if (name.startsWith("get")) { name = name.substring(3); } if (alternated.contains(StringUtils.capitalize(name))) { continue; } methodsHasAnnotations = true; break; } } } // check fields for (final Field f : fields) { for (final Annotation a : f.getAnnotations()) { // ignore @Access(FIELD) if (a instanceof Access) { if (((Access) a).value() != AccessType.FIELD) { continue; } } // ignore @Transient if (a instanceof Transient) { continue; } final String name = a.annotationType().getName(); if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) { if (alternated.contains(StringUtils.capitalize(f.getName()))) { continue; } fieldsHasAnnotations = true; break; } } } if (fieldsHasAnnotations && methodsHasAnnotations) { throw new PersistenceException( "At least one field and one method has persistence annotations: " + this.clazz.getName()); } if (methodsHasAnnotations) { return AccessType.PROPERTY; } if (fieldsHasAnnotations) { return AccessType.FIELD; } if (parentAccessType != null) { return parentAccessType; } return AccessType.FIELD; }
From source file:com.gu.management.spring.ManagementUrlDiscoveryService.java
private boolean isRequestableMethod(Method method) { Class<?>[] parameterTypes = method.getParameterTypes(); boolean isPublic = Modifier.isPublic(method.getModifiers()); boolean isProtected = Modifier.isProtected(method.getModifiers()); boolean returnsModelAndView = method.getReturnType().equals(ModelAndView.class); boolean takesRequestAndResponse = parameterTypes.length >= 2 && HttpServletRequest.class.isAssignableFrom(parameterTypes[0]) && HttpServletResponse.class.isAssignableFrom(parameterTypes[1]); return (isPublic || isProtected) && (returnsModelAndView || takesRequestAndResponse); }
From source file:net.yasion.common.core.bean.wrapper.ExtendedBeanInfo.java
public static boolean isCandidateWriteMethod(Method method) { String methodName = method.getName(); Class<?>[] parameterTypes = method.getParameterTypes(); int nParams = parameterTypes.length; return (methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) && (!void.class.isAssignableFrom(method.getReturnType()) || Modifier.isStatic(method.getModifiers())) && (nParams == 1 || (nParams == 2 && parameterTypes[0].equals(int.class)))); }
From source file:org.apache.cocoon.forms.datatype.EnumSelectionList.java
public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { try {//w ww . j a v a 2s. c o m contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES); // Create void element if (nullable) { AttributesImpl voidAttrs = new AttributesImpl(); voidAttrs.addCDATAAttribute("value", ""); contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs); if (this.nullText != null) { contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES); contentHandler.startElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL, XMLUtils.EMPTY_ATTRIBUTES); contentHandler.characters(nullText.toCharArray(), 0, nullText.length()); contentHandler.endElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL); contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL); } contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL); } // Test if we have an apache enum class boolean apacheEnumDone = false; if (Enum.class.isAssignableFrom(clazz)) { Iterator iter = EnumUtils.iterator(clazz); if (iter != null) { apacheEnumDone = true; while (iter.hasNext()) { Enum element = (Enum) iter.next(); String stringValue = clazz.getName() + "." + element.getName(); generateItem(contentHandler, stringValue); } } } // If it's not an apache enum or we didn't manage to read the enum list, then proceed with common method. if (!apacheEnumDone) { Field fields[] = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; ++i) { int mods = fields[i].getModifiers(); if (Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods) && fields[i].get(null).getClass().equals(clazz)) { String stringValue = clazz.getName() + "." + fields[i].getName(); generateItem(contentHandler, stringValue); } } } // End the selection-list contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL); } catch (Exception e) { throw new SAXException("Got exception trying to get enum's values", e); } }
From source file:java2typescript.jaxrs.ServiceDescriptorGenerator.java
/** * Main method to generate a REST Service desciptor out of JAX-RS service * class//from www . j ava 2s .c o m */ private Collection<RestService> generateRestServices(Collection<? extends Class<?>> classes) { List<RestService> services = new ArrayList<RestService>(); for (Class<?> clazz : classes) { RestService service = new RestService(); service.setName(clazz.getSimpleName()); Path pathAnnotation = clazz.getAnnotation(Path.class); if (pathAnnotation == null) { throw new RuntimeException("No @Path on class " + clazz.getName()); } service.setPath(pathAnnotation.value()); for (Method method : clazz.getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers())) { RestMethod restMethod = generateMethod(method); service.getMethods().put(restMethod.getName(), restMethod); } } if (extras != null) { service.setExtra(extras.getExtraForService(clazz)); } services.add(service); } return services; }
From source file:ca.sqlpower.testutil.TestUtils.java
/** * Returns the set of property names which have both a getter and a setter * method and are annotated to be persisted through the {@link SPPersister} * classes.//from ww w . j av a 2s. c om * * @param objectUnderTest * The object that contains the persistable properties we want to * find. * @param includeTransient * If true the properties marked as transient will also be * included. If false only the properties that are persisted and * not transient are returned. * @param includeConstructorMutators * If true the properties that have getters but can only be set * through a constructor due to being final will be included. If * false the persisted properties provided must have a setter. */ public static Set<String> findPersistableBeanProperties(SPObject objectUnderTest, boolean includeTransient, boolean includeConstructorMutators) throws Exception { Set<String> getters = new HashSet<String>(); Set<String> setters = new HashSet<String>(); for (Method m : objectUnderTest.getClass().getMethods()) { if (m.getName().equals("getClass")) continue; //skip non-public methods as they are not visible for persisting anyways. if (!Modifier.isPublic(m.getModifiers())) continue; //skip static methods if (Modifier.isStatic(m.getModifiers())) continue; if (m.getName().startsWith("get") || m.getName().startsWith("is")) { Class<?> parentClass = objectUnderTest.getClass(); boolean accessor = false; boolean ignored = false; boolean isTransient = false; parentClass.getMethod(m.getName(), m.getParameterTypes());//test while (parentClass != null) { Method parentMethod; try { parentMethod = parentClass.getMethod(m.getName(), m.getParameterTypes()); } catch (NoSuchMethodException e) { parentClass = parentClass.getSuperclass(); continue; } if (parentMethod.getAnnotation(Accessor.class) != null) { accessor = true; if (parentMethod.getAnnotation(Transient.class) != null) { isTransient = true; } break; } else if (parentMethod.getAnnotation(NonProperty.class) != null || parentMethod.getAnnotation(NonBound.class) != null) { ignored = true; break; } parentClass = parentClass.getSuperclass(); } if (accessor) { if (includeTransient || !isTransient) { if (m.getName().startsWith("get")) { getters.add(m.getName().substring(3)); } else if (m.getName().startsWith("is")) { getters.add(m.getName().substring(2)); } } } else if (ignored) { //ignored so skip } else { fail("The method " + m.getName() + " of " + objectUnderTest.toString() + " is a getter that is not annotated " + "to be an accessor or transient. The exiting annotations are " + Arrays.toString(m.getAnnotations())); } } else if (m.getName().startsWith("set")) { if (m.getAnnotation(Mutator.class) != null) { if ((includeTransient || m.getAnnotation(Transient.class) == null) && (includeConstructorMutators || !m.getAnnotation(Mutator.class).constructorMutator())) { setters.add(m.getName().substring(3)); } } else if (m.getAnnotation(NonProperty.class) != null || m.getAnnotation(NonBound.class) != null) { //ignored so skip and pass } else { fail("The method " + m.getName() + " is a setter that is not annotated " + "to be a mutator or transient."); } } } Set<String> beanNames = new HashSet<String>(); for (String beanName : getters) { if (setters.contains(beanName)) { String firstLetter = new String(new char[] { beanName.charAt(0) }); beanNames.add(beanName.replaceFirst(firstLetter, firstLetter.toLowerCase())); } } return beanNames; }
From source file:microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema.java
/** * Adds schema property to dictionary.//from w ww. ja va 2s . co m * * @param type Schema type. * @param propDefDictionary The property definition dictionary. */ protected static void addSchemaPropertiesToDictionary(Class<?> type, Map<String, PropertyDefinitionBase> propDefDictionary) { Field[] fields = type.getDeclaredFields(); for (Field field : fields) { int modifier = field.getModifiers(); if (Modifier.isPublic(modifier) && Modifier.isStatic(modifier)) { Object o; try { o = field.get(null); if (o instanceof PropertyDefinition) { PropertyDefinition propertyDefinition = (PropertyDefinition) o; // Some property definitions descend from // ServiceObjectPropertyDefinition but don't have // a Uri, like ExtendedProperties. Ignore them. if (null != propertyDefinition.getUri() && !propertyDefinition.getUri().isEmpty()) { PropertyDefinitionBase existingPropertyDefinition; if (propDefDictionary.containsKey(propertyDefinition.getUri())) { existingPropertyDefinition = propDefDictionary.get(propertyDefinition.getUri()); EwsUtilities.ewsAssert(existingPropertyDefinition == propertyDefinition, "Schema.allSchemaProperties." + "delegate", String.format( "There are at least " + "two distinct property " + "definitions with the" + " following URI: %s", propertyDefinition.getUri())); } else { propDefDictionary.put(propertyDefinition.getUri(), propertyDefinition); // The following is a "generic hack" to register // property that are not public and // thus not returned by the above GetFields // call. It is currently solely used to register // the MeetingTimeZone property. List<PropertyDefinition> associatedInternalProperties = propertyDefinition .getAssociatedInternalProperties(); for (PropertyDefinition associatedInternalProperty : associatedInternalProperties) { propDefDictionary.put(associatedInternalProperty.getUri(), associatedInternalProperty); } } } } } catch (IllegalArgumentException e) { LOG.error(e); // Skip the field } catch (IllegalAccessException e) { LOG.error(e); // Skip the field } } } }
From source file:edu.psu.citeseerx.utility.ConfigurationManager.java
/** * Determines whether to accept the specified key. Currently, this * method makes sure that the key is not a public object. * @param key//from ww w.j av a 2 s . c o m * @return true if the method returns. * @throws InvalidAccessKeyException if the key should not be accepted. */ private boolean isValidKey(ConfigurationKey key) throws InvalidAccessKeyException { Class keyClass = key.getClass(); int m = keyClass.getModifiers(); if (Modifier.isPublic(m)) { throw new InvalidAccessKeyException("Key has innapropriate scope"); } return true; }