List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.github.helenusdriver.driver.impl.ClassInfoImpl.java
/** * Finds an initial objects factory method for the POJO if configured. * * @author paouelle/*from w ww . j a v a 2 s. c o m*/ * * @return the initial objects factory method or <code>null</code> if none * configured * @throws IllegalArgumentException if the initial objects method is not * properly defined */ private Method findInitial() { final InitialObjects io = clazz.getAnnotation(InitialObjects.class); if (io != null) { final String mname = io.staticMethod(); try { final Method m = (suffixesByType.isEmpty() ? clazz.getMethod(mname) : clazz.getMethod(mname, Map.class)); // validate the method is static if (!Modifier.isStatic(m.getModifiers())) { throw new IllegalArgumentException("initial objects method '" + mname + "' is not static in class: " + clazz.getSimpleName()); } // validate the return type is compatible with this class and is an array final Class<?> type = m.getReturnType(); if (!type.isArray()) { throw new IllegalArgumentException("initial objects method '" + mname + "' doesn't return an array in class: " + clazz.getSimpleName()); } final Class<?> ctype = type.getComponentType(); if (!ctype.isAssignableFrom(clazz)) { throw new IllegalArgumentException("incompatible returned class '" + ctype.getName() + "' for initial objects method '" + mname + "' in class: " + clazz.getSimpleName()); } // validate that if suffixes are defined, the method expects a Map<String, String> // to provide the values for the suffixes when initializing objects final Class<?>[] cparms = m.getParameterTypes(); if (suffixesByType.isEmpty()) { // should always be 0 as we used no classes in getMethod() if (cparms.length != 0) { throw new IllegalArgumentException("expecting no parameters for initial objects method '" + mname + "' in class: " + clazz.getSimpleName()); } } else { // should always be 1 as we used only 1 class in getMethod() if (cparms.length != 1) { throw new IllegalArgumentException( "expecting one Map<String, String> parameter for initial objects method '" + mname + "' in class: " + clazz.getSimpleName()); } // should always be a map as we used a Map to find the method if (!Map.class.isAssignableFrom(cparms[0])) { throw new IllegalArgumentException("expecting parameter for initial objects method '" + mname + "' to be of type Map<String, String> in class: " + clazz.getSimpleName()); } final Type[] tparms = m.getGenericParameterTypes(); // should always be 1 as we used only 1 class in getMethod() if (tparms.length != 1) { // should always be 1 as it was already tested above throw new IllegalArgumentException( "expecting one Map<String, String> parameter for initial objects method '" + mname + "' in class: " + clazz.getSimpleName()); } if (tparms[0] instanceof ParameterizedType) { final ParameterizedType ptype = (ParameterizedType) tparms[0]; // maps will always have 2 arguments for (final Type atype : ptype.getActualTypeArguments()) { final Class<?> aclazz = ReflectionUtils.getRawClass(atype); if (String.class != aclazz) { throw new IllegalArgumentException( "expecting a Map<String, String> parameter for initial objects method '" + mname + "' in class: " + clazz.getSimpleName()); } } } else { throw new IllegalArgumentException( "expecting a Map<String, String> parameter for initial objects method '" + mname + "' in class: " + clazz.getSimpleName()); } } return m; } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "missing initial objects method '" + mname + "' in class: " + clazz.getSimpleName(), e); } } return null; }
From source file:com.github.jinahya.sql.database.metadata.bind.MetadataContext.java
private Set<String> getMethodNames() { if (methodNames == null) { methodNames = new HashSet<String>(); for (final Method method : DatabaseMetaData.class.getMethods()) { if (method.getDeclaringClass() != DatabaseMetaData.class) { continue; }//from w w w . java 2 s. c om final int modifier = method.getModifiers(); if (Modifier.isStatic(modifier)) { continue; } if (!Modifier.isPublic(modifier)) { continue; } methodNames.add(method.getName()); } } return methodNames; }
From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java
/** * Copies the fields, where the field names are matches. * * @param <T> the generic type/*from ww w.ja v a 2s .c om*/ * @param targetClass the target class * @param source the source * @param target the target * @param fieldNameMatcher the field name matcher */ public static <T> void copyInstanceProperties(Class<?> targetClass, T source, T target, Matcher<Field> fieldNameMatcher) { if (targetClass == null) { return; } for (Field f : targetClass.getDeclaredFields()) { int mod = f.getModifiers(); if (Modifier.isStatic(mod) == true) { continue; } if (fieldNameMatcher.match(f) == false) { continue; } Object value = readField(source, f.getName()); writeField(target, f, value); } copyInstanceProperties(targetClass.getSuperclass(), source, target, fieldNameMatcher); }
From source file:adalid.core.XS1.java
static boolean checkFieldAnnotation(boolean log, Field field, Class<? extends Annotation> annotation, Class<?>[] validTypes) { String name = field.getName(); Class<?> type = field.getDeclaringClass(); String string;/*from ww w. j av a 2s. c om*/ int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) { string = "field " + name + " has static and/or final modifier"; if (log) { logFieldAnnotationErrorMessage(name, type, annotation, string); } return false; } int length = validTypes == null ? 0 : validTypes.length; if (length < 1) { return true; } Class<?> ft = getTrueType(field.getType()); String[] strings = new String[length]; int i = 0; for (Class<?> vt : validTypes) { if (vt.isAssignableFrom(ft)) { return true; } strings[i++] = vt.getSimpleName(); } string = "type of " + name + " is not " + StringUtils.join(strings, " or "); if (log) { logFieldAnnotationErrorMessage(name, type, annotation, string); } return false; }
From source file:com.glaf.core.util.ReflectUtils.java
public static boolean isPublicInstanceField(Field field) { return Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers()) && !field.isSynthetic(); }
From source file:org.apache.axis.description.JavaServiceDesc.java
/** * Fill in a service description by introspecting the implementation * class./*from w w w . j a v a 2s .c om*/ */ public void loadServiceDescByIntrospection(Class implClass) { if (introspectionComplete || implClass == null) { return; } // set the implementation class for the service description this.implClass = implClass; if (Skeleton.class.isAssignableFrom(implClass)) { isSkeletonClass = true; loadSkeletonOperations(); } /** If the class knows what it should be exporting, * respect its wishes. */ AxisServiceConfig axisConfig = null; try { Method method = implClass.getDeclaredMethod("getAxisServiceConfig", new Class[] {}); if (method != null && Modifier.isStatic(method.getModifiers())) { axisConfig = (AxisServiceConfig) method.invoke(null, null); } } catch (Exception e) { // No problem, just continue without... } if (axisConfig != null) { String allowedMethodsStr = axisConfig.getAllowedMethods(); if (allowedMethodsStr != null && !"*".equals(allowedMethodsStr)) { ArrayList methodList = new ArrayList(); StringTokenizer tokenizer = new StringTokenizer(allowedMethodsStr, " ,"); while (tokenizer.hasMoreTokens()) { methodList.add(tokenizer.nextToken()); } setAllowedMethods(methodList); } } loadServiceDescByIntrospectionRecursive(implClass); // All operations should now be synchronized. Check it. for (Iterator iterator = operations.iterator(); iterator.hasNext();) { OperationDesc operation = (OperationDesc) iterator.next(); if (operation.getMethod() == null) { throw new InternalException(Messages.getMessage("badWSDDOperation", operation.getName(), "" + operation.getNumParams())); } } if ((style == Style.MESSAGE) && operations.size() == 1) { messageServiceDefaultOp = (OperationDesc) operations.get(0); } introspectionComplete = true; }
From source file:com.hc.wx.server.common.bytecode.ReflectUtils.java
public static Map<String, Field> getBeanPropertyFields(Class cl) { Map<String, Field> properties = new HashMap<String, Field>(); for (; cl != null; cl = cl.getSuperclass()) { Field[] fields = cl.getDeclaredFields(); for (Field field : fields) { if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) { continue; }//from w w w .ja va2 s . c o m field.setAccessible(true); properties.put(field.getName(), field); } } return properties; }
From source file:com.dianping.resource.io.util.ClassUtils.java
/** * Return a public static method of a class. * @param methodName the static method name * @param clazz the class which defines the method * @param args the parameter types to the method * @return the static method, or {@code null} if no static method was found * @throws IllegalArgumentException if the method name is blank or the clazz is null *//*from w w w . j a v a 2s . c o m*/ public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(methodName, "Method name must not be null"); try { Method method = clazz.getMethod(methodName, args); return Modifier.isStatic(method.getModifiers()) ? method : null; } catch (NoSuchMethodException ex) { return null; } }
From source file:com.freetmp.common.util.ClassUtils.java
public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(methodName, "Method name must not be null"); try {//from ww w . ja v a 2s . c o m Method method = clazz.getMethod(methodName, args); return Modifier.isStatic(method.getModifiers()) ? method : null; } catch (NoSuchMethodException ex) { return null; } }
From source file:org.apache.openjpa.meta.ClassMetaData.java
/** * Return whether the given name represents a managed or static field of * this class, including superclass fields. *///from w ww. j a va2s . com public boolean isAccessibleField(String field) { if (getDeclaredField(field) != null) return true; if (_staticFields == null) { Field[] fields = (Field[]) AccessController.doPrivileged(J2DoPrivHelper.getDeclaredFieldsAction(_type)); Set<String> names = new HashSet<String>(); for (int i = 0; i < fields.length; i++) if (Modifier.isStatic(fields[i].getModifiers())) names.add(fields[i].getName()); _staticFields = names; } if (_staticFields.contains(field)) return true; if (_super != null) return getPCSuperclassMetaData().isAccessibleField(field); return false; }