List of usage examples for java.lang.reflect Modifier toString
public static String toString(int mod)
From source file:objenome.util.bytecode.SgUtils.java
/** * Checks if the modifiers are valid for a class. If any of the modifiers is * not valid an <code>IllegalArgumentException</code> is thrown. * //from w ww .jav a 2 s . com * @param modifiers * Modifiers. * @param isInterface * Are the modifiers from an interface? * @param isInnerClass * Is it an inner class? */ public static void checkClassModifiers(int modifiers, boolean isInterface, boolean isInnerClass) { // Basic checks int type; if (isInterface) { type = isInnerClass ? INNER_INTERFACE : OUTER_INTERFACE; } else { type = isInnerClass ? INNER_CLASS : OUTER_CLASS; } checkModifiers(type, modifiers); // Abstract and final check if (Modifier.isAbstract(modifiers) && Modifier.isFinal(modifiers)) { throw new IllegalArgumentException( CLASS_ABSTRACT_AND_FINAL_ERROR + " [" + Modifier.toString(modifiers) + ']'); } }
From source file:objenome.util.bytecode.SgUtils.java
/** * Checks if the modifiers are valid for a field. If any of the modifiers is * not valid an <code>IllegalArgumentException</code> is thrown. * //ww w. ja va2 s.c o m * @param modifiers * Modifiers. */ public static void checkFieldModifiers(int modifiers) { // Basic checks checkModifiers(FIELD, modifiers); // Check final and volatile if (Modifier.isFinal(modifiers) && Modifier.isVolatile(modifiers)) { throw new IllegalArgumentException( FIELD_FINAL_VOLATILE_ERROR + " [" + Modifier.toString(modifiers) + ']'); } }
From source file:objenome.util.bytecode.SgUtils.java
/** * Checks if the modifiers are valid for a method. If any of the modifiers * is not valid an <code>IllegalArgumentException</code> is thrown. * //from w ww . j av a2s . c o m * @param modifiers * Modifiers. */ // CHECKSTYLE:OFF Cyclomatic complexity is OK public static void checkMethodModifiers(int modifiers) { // Base check checkModifiers(METHOD, modifiers); // Check overlapping modifiers if (Modifier.isPrivate(modifiers)) { if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers)) { throw new IllegalArgumentException( METHOD_ACCESS_MODIFIER_ERROR + " [" + Modifier.toString(modifiers) + ']'); } } if (Modifier.isProtected(modifiers)) { if (Modifier.isPrivate(modifiers) || Modifier.isPublic(modifiers)) { throw new IllegalArgumentException( METHOD_ACCESS_MODIFIER_ERROR + " [" + Modifier.toString(modifiers) + ']'); } } if (Modifier.isPublic(modifiers)) { if (Modifier.isPrivate(modifiers) || Modifier.isProtected(modifiers)) { throw new IllegalArgumentException( METHOD_ACCESS_MODIFIER_ERROR + " [" + Modifier.toString(modifiers) + ']'); } } // Check illegal abstract modifiers if (Modifier.isAbstract(modifiers)) { if (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isNative(modifiers) || Modifier.isStrict(modifiers) || Modifier.isSynchronized(modifiers)) { throw new IllegalArgumentException( METHOD_ILLEGAL_ABSTRACT_MODIFIERS_ERROR + " [" + Modifier.toString(modifiers) + ']'); } } // Check native and strictfp if (Modifier.isNative(modifiers) && Modifier.isStrict(modifiers)) { throw new IllegalArgumentException( METHOD_NATIVE_STRICTFP_ERROR + " [" + Modifier.toString(modifiers) + ']'); } }
From source file:org.eclipse.gemini.blueprint.config.internal.adapter.CustomListenerAdapterUtils.java
private static Map<Class<?>, List<Method>> doDetermineCustomMethods(final Class<?> target, final String methodName, final Class<?>[] possibleArgumentTypes, final boolean onlyPublic) { final Map<Class<?>, List<Method>> methods = new LinkedHashMap<Class<?>, List<Method>>(3); final boolean trace = log.isTraceEnabled(); org.springframework.util.ReflectionUtils.doWithMethods(target, new org.springframework.util.ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if (!method.isBridge() && methodName.equals(method.getName())) { if (onlyPublic && !Modifier.isPublic(method.getModifiers())) { if (trace) log.trace("Only public methods are considered; ignoring " + method); return; }/*from ww w. jav a 2 s .c o m*/ // take a look at the variables Class<?>[] args = method.getParameterTypes(); if (args != null) { // Properties can be ignored if (args.length == 1) { addMethod(args[0], method, methods); } // or passed as Map, Dictionary else if (args.length == 2) { Class<?> propType = args[1]; for (int i = 0; i < possibleArgumentTypes.length; i++) { Class<?> clazz = possibleArgumentTypes[i]; if (clazz.isAssignableFrom(propType)) { addMethod(args[0], method, methods); } } } } } } private void addMethod(Class<?> key, Method mt, Map<Class<?>, List<Method>> methods) { if (trace) log.trace("discovered custom method [" + mt.toString() + "] on " + target); List<Method> mts = methods.get(key); if (mts == null) { mts = new ArrayList<Method>(2); methods.put(key, mts); org.springframework.util.ReflectionUtils.makeAccessible(mt); mts.add(mt); return; } // add a method only if there is still space if (mts.size() == 1) { Method m = mts.get(0); if (m.getParameterTypes().length == mt.getParameterTypes().length) { if (trace) log.trace("Method w/ signature " + methodSignature(m) + " has been already discovered; ignoring it"); } else { org.springframework.util.ReflectionUtils.makeAccessible(mt); mts.add(mt); } } } private String methodSignature(Method m) { StringBuilder sb = new StringBuilder(); int mod = m.getModifiers(); if (mod != 0) { sb.append(Modifier.toString(mod) + " "); } sb.append(m.getReturnType() + " "); sb.append(m.getName() + "("); Class<?>[] params = m.getParameterTypes(); for (int j = 0; j < params.length; j++) { sb.append(params[j]); if (j < (params.length - 1)) sb.append(","); } sb.append(")"); return sb.toString(); } }); return methods; }
From source file:org.power.commons.lang.util.ClassUtils.java
/** * ??method??/*from w w w .ja v a2 s . c o m*/ */ public static String getSimpleMethodSignature(Method method, boolean withModifiers, boolean withReturnType, boolean withClassName, boolean withExceptionType) { if (method == null) { return null; } StringBuilder buf = new StringBuilder(); if (withModifiers) { buf.append(Modifier.toString(method.getModifiers())).append(' '); } if (withReturnType) { buf.append(getSimpleClassName(method.getReturnType())).append(' '); } if (withClassName) { buf.append(getSimpleClassName(method.getDeclaringClass())).append('.'); } buf.append(method.getName()).append('('); Class<?>[] paramTypes = method.getParameterTypes(); for (int i = 0; i < paramTypes.length; i++) { Class<?> paramType = paramTypes[i]; buf.append(getSimpleClassName(paramType)); if (i < paramTypes.length - 1) { buf.append(", "); } } buf.append(')'); if (withExceptionType) { Class<?>[] exceptionTypes = method.getExceptionTypes(); if (ArrayUtils.isNotEmpty(exceptionTypes)) { buf.append(" throws "); for (int i = 0; i < exceptionTypes.length; i++) { Class<?> exceptionType = exceptionTypes[i]; buf.append(getSimpleClassName(exceptionType)); if (i < exceptionTypes.length - 1) { buf.append(", "); } } } } return buf.toString(); }