List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.github.helenusdriver.driver.tools.Tool.java
/** * Finds an initial objects factory method and its dependent classes from the * specified object creator class.//from w w w. ja v a2 s. com * * @author paouelle * * @param clazz the non-<code>null</code> object creator class * @return the initial objects factory method and its set of dependenc classes * or <code>null</code> if none configured * @throws IllegalArgumentException if the initial objects method is not * properly defined */ private static Pair<Method, Class<?>[]> findInitial(Class<?> clazz) { final InitialObjects io = clazz.getAnnotation(InitialObjects.class); if (io != null) { final String mname = io.staticMethod(); try { Method m; try { // first look for one with a map for suffixes m = clazz.getMethod(mname, Map.class); // 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(); // 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()); } } catch (NoSuchMethodException e) { // fallback to one with no map m = clazz.getMethod(mname); } // 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 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()); } return Pair.of(m, io.dependsOn()); } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "missing initial objects method '" + mname + "' in class: " + clazz.getSimpleName(), e); } } return null; }
From source file:org.crazydog.util.spring.ClassUtils.java
/** * Return a public static method of a class. * @param clazz the class which defines the method * @param methodName the static method name * @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 2 s .c o m public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) { org.springframework.util.Assert.notNull(clazz, "Class must not be null"); org.springframework.util.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:org.apache.hadoop.yarn.api.TestPBImplRecords.java
private <R> Map<String, GetSetPair> getGetSetPairs(Class<R> recordClass) throws Exception { Map<String, GetSetPair> ret = new HashMap<String, GetSetPair>(); Method[] methods = recordClass.getDeclaredMethods(); // get all get methods for (int i = 0; i < methods.length; i++) { Method m = methods[i];//from w ww . ja v a 2 s .co m int mod = m.getModifiers(); if (m.getDeclaringClass().equals(recordClass) && Modifier.isPublic(mod) && (!Modifier.isStatic(mod))) { String name = m.getName(); if (name.equals("getProto")) { continue; } if ((name.length() > 3) && name.startsWith("get") && (m.getParameterTypes().length == 0)) { String propertyName = name.substring(3); Type valueType = m.getGenericReturnType(); GetSetPair p = ret.get(propertyName); if (p == null) { p = new GetSetPair(); p.propertyName = propertyName; p.type = valueType; p.getMethod = m; ret.put(propertyName, p); } else { Assert.fail("Multiple get method with same name: " + recordClass + p.propertyName); } } } } // match get methods with set methods for (int i = 0; i < methods.length; i++) { Method m = methods[i]; int mod = m.getModifiers(); if (m.getDeclaringClass().equals(recordClass) && Modifier.isPublic(mod) && (!Modifier.isStatic(mod))) { String name = m.getName(); if (name.startsWith("set") && (m.getParameterTypes().length == 1)) { String propertyName = name.substring(3); Type valueType = m.getGenericParameterTypes()[0]; GetSetPair p = ret.get(propertyName); if (p != null && p.type.equals(valueType)) { p.setMethod = m; } } } } // exclude incomplete get/set pair, and generate test value Iterator<Entry<String, GetSetPair>> itr = ret.entrySet().iterator(); while (itr.hasNext()) { Entry<String, GetSetPair> cur = itr.next(); GetSetPair gsp = cur.getValue(); if ((gsp.getMethod == null) || (gsp.setMethod == null)) { LOG.info(String.format("Exclude protential property: %s\n", gsp.propertyName)); itr.remove(); } else { LOG.info(String.format("New property: %s type: %s", gsp.toString(), gsp.type)); gsp.testValue = genTypeValue(gsp.type); LOG.info(String.format(" testValue: %s\n", gsp.testValue)); } } return ret; }
From source file:com.gatf.generator.core.GatfTestGeneratorMojo.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private Object getObject(Class claz, List<Type> heirarchies) throws Exception { if (claz.isEnum()) return claz.getEnumConstants()[0]; if (isMap(claz)) { return getMapValue(claz, claz.getTypeParameters(), heirarchies); } else if (isCollection(claz)) { return getListSetValue(claz, claz.getTypeParameters(), heirarchies); } else if (claz.isInterface() || Modifier.isAbstract(claz.getModifiers())) { return null; }//from w ww . jav a 2 s . c o m if (heirarchies.contains(claz) || heirarchies.size() >= 2) return null; heirarchies.add(claz); Constructor cons = null; try { cons = claz.getConstructor(new Class[] {}); } catch (Exception e) { getLog().error("No public no-args constructor found for class " + claz.getName()); return null; } Object object = cons.newInstance(new Object[] {}); List<Field> allFields = getAllFields(claz); for (Field field : allFields) { if (Modifier.isStatic(field.getModifiers())) continue; if (!field.isAccessible()) { field.setAccessible(true); } List<Type> fheirlst = new ArrayList<Type>(heirarchies); if (isDebugEnabled()) getLog().info("Parsing Class " + getHeirarchyStr(fheirlst) + " field " + field.getName() + " type " + field.getType().equals(boolean.class)); if (isPrimitive(field.getType())) { field.set(object, getPrimitiveValue(field.getType())); } else if (isMap(field.getType())) { ParameterizedType type = (ParameterizedType) field.getGenericType(); field.set(object, getMapValue(field.getType(), type.getActualTypeArguments(), fheirlst)); } else if (isCollection(field.getType())) { ParameterizedType type = (ParameterizedType) field.getGenericType(); field.set(object, getListSetValue(field.getType(), type.getActualTypeArguments(), fheirlst)); } else if (!claz.equals(field.getType())) { Object fieldval = getObject(field.getType(), fheirlst); field.set(object, fieldval); } else if (claz.equals(field.getType())) { if (isDebugEnabled()) getLog().info("Ignoring recursive fields..."); } } return object; }
From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java
/** * Parse callback methods into the given array, and return that array, * creating one if null. Each index into the array is a collection of * callback adapters for that numeric event type. * * @param sups whether to scan superclasses * @param listener whether this is a listener or not *///from w w w . j av a 2 s. c o m public static Collection<LifecycleCallbacks>[] parseCallbackMethods(Class<?> cls, Collection<LifecycleCallbacks>[] callbacks, boolean sups, boolean listener, MetaDataRepository repos) { if (cls == null) throw new IllegalArgumentException("cls cannot be null"); // first sort / filter based on inheritance Set<Method> methods = new TreeSet<Method>(MethodComparator.getInstance()); int mods; Class<?> sup = cls; MethodKey key; Set<MethodKey> seen = new HashSet<MethodKey>(); do { for (Method m : (Method[]) AccessController .doPrivileged(J2DoPrivHelper.getDeclaredMethodsAction(sup))) { mods = m.getModifiers(); if (Modifier.isStatic(mods) || Modifier.isFinal(mods) || Object.class.equals(m.getDeclaringClass())) continue; key = new MethodKey(m); if (!seen.contains(key)) { methods.add(m); seen.add(key); } } sup = sup.getSuperclass(); } while (sups && !Object.class.equals(sup)); OpenJPAConfiguration conf = repos.getConfiguration(); for (Method m : methods) { for (Annotation anno : (Annotation[]) AccessController .doPrivileged(J2DoPrivHelper.getDeclaredAnnotationsAction(m))) { MetaDataTag tag = _tags.get(anno.annotationType()); if (tag == null) continue; int[] events = MetaDataParsers.getEventTypes(tag, conf); if (events == null) continue; if (callbacks == null) callbacks = (Collection<LifecycleCallbacks>[]) new Collection[LifecycleEvent.ALL_EVENTS.length]; for (int i = 0; i < events.length; i++) { int e = events[i]; if (callbacks[e] == null) callbacks[e] = new ArrayList<LifecycleCallbacks>(3); MetaDataParsers.validateMethodsForSameCallback(cls, callbacks[e], m, tag, conf, repos.getLog()); if (listener) { callbacks[e].add(new BeanLifecycleCallbacks(cls, m, false)); } else { callbacks[e].add(new MethodLifecycleCallbacks(m, false)); } } } } return callbacks; }
From source file:com.github.wshackle.java4cpp.J4CppMain.java
private static boolean isMethodToMakeEasy(Method m) { if (Modifier.isStatic(m.getModifiers())) { return false; }/*from www.j ava2s . com*/ Class<?> types[] = m.getParameterTypes(); for (int i = 0; i < types.length; i++) { Class<?> type = types[i]; if (type.isArray() && !type.getComponentType().isPrimitive()) { return false; } } for (int i = 0; i < types.length; i++) { Class<?> type = types[i]; if (type.isArray() || isString(type)) { return true; } } return false; // return !Modifier.isStatic(m.getModifiers()) // && Arrays.stream(m.getParameterTypes()) // .anyMatch(t -> t.isArray() || isString(t)) // && Arrays.stream(m.getParameterTypes()) // .noneMatch(t -> t.isArray() && !t.getComponentType().isPrimitive()); }
From source file:org.apache.axis.wsdl.fromJava.Types.java
/** * Returns true if indicated type matches the JAX-RPC enumeration class. * Note: supports JSR 101 version 0.6 Public Draft * * @param cls/*from w w w . j a v a2s .c o m*/ * @return */ public static boolean isEnumClass(Class cls) { try { java.lang.reflect.Method m = cls.getMethod("getValue", null); java.lang.reflect.Method m2 = cls.getMethod("toString", null); if ((m != null) && (m2 != null)) { java.lang.reflect.Method m3 = cls.getDeclaredMethod("fromString", new Class[] { java.lang.String.class }); java.lang.reflect.Method m4 = cls.getDeclaredMethod("fromValue", new Class[] { m.getReturnType() }); if ((m3 != null) && Modifier.isStatic(m3.getModifiers()) && Modifier.isPublic(m3.getModifiers()) && (m4 != null) && Modifier.isStatic(m4.getModifiers()) && Modifier.isPublic(m4.getModifiers())) { // Return false if there is a setValue member method try { if (cls.getMethod("setValue", new Class[] { m.getReturnType() }) == null) { return true; } return false; } catch (java.lang.NoSuchMethodException e) { return true; } } } } catch (java.lang.NoSuchMethodException e) { } return false; }
From source file:edu.ku.brc.specify.conversion.CollectionInfo.java
@Override public String toString() { // Taken from a web example try {/*from w w w .ja v a2s. c om*/ String result = ""; Class<?> cls = this.getClass(); Field fieldlist[] = cls.getDeclaredFields(); for (int i = 0; i < fieldlist.length; i++) { Field fld = fieldlist[i]; if (!Modifier.isStatic(fld.getModifiers())) { result += fld.getName() + " = " + fld.get(this) + "\n"; } } result += "\n"; return result; } catch (Throwable e) { e.printStackTrace(); } return super.toString(); }
From source file:mil.army.usace.data.dataquery.rdbms.RdbmsDataQuery.java
private HashMap<Field, String> getDbStructFieldMapping(Class dbStructClazz) { HashMap<Field, String> fieldMapping = new HashMap<>(); Field[] fields = dbStructClazz.getDeclaredFields(); for (Field field : fields) { if (!Modifier.isStatic(field.getModifiers())) { //exclude static fields if (field.getAnnotation(Transient.class) == null) { fieldMapping.put(field, getDbName(field)); }// ww w .j a v a 2s . c o m } } return fieldMapping; }
From source file:org.apache.axis.wsdl.fromJava.Types.java
/** * Write Enumeration Complex Type//from ww w .j a v a 2 s. c o m * (Only supports enumeration classes of string types) * * @param qName QName of type. * @param cls class of type * @return * @throws NoSuchMethodException * @throws IllegalAccessException * @throws AxisFault */ public Element writeEnumType(QName qName, Class cls) throws NoSuchMethodException, IllegalAccessException, AxisFault { if (!isEnumClass(cls)) { return null; } // Get the base type of the enum class java.lang.reflect.Method m = cls.getMethod("getValue", null); Class base = m.getReturnType(); // Create simpleType, restriction elements Element simpleType = docHolder.createElement("simpleType"); simpleType.setAttribute("name", qName.getLocalPart()); Element restriction = docHolder.createElement("restriction"); simpleType.appendChild(restriction); String baseType = writeType(base, null); restriction.setAttribute("base", baseType); // Create an enumeration using the field values Field[] fields = cls.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; int mod = field.getModifiers(); // Inspect each public static final field of the same type // as the base if (Modifier.isPublic(mod) && Modifier.isStatic(mod) && Modifier.isFinal(mod) && (field.getType() == base)) { // Create an enumeration using the value specified Element enumeration = docHolder.createElement("enumeration"); enumeration.setAttribute("value", field.get(null).toString()); restriction.appendChild(enumeration); } } return simpleType; }