Java Code Examples for java.lang.reflect.Modifier#isStatic()

The following examples show how to use java.lang.reflect.Modifier#isStatic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodParameterChangeClassFinder.java    From deobfuscator with Apache License 2.0 8 votes vote down vote up
private Set<ClassNode> findInterfaces(Collection<ClassNode> classes) {
    Set<ClassNode> result = new HashSet<>();
    for (ClassNode classNode : classes) {
        if (!Modifier.isInterface(classNode.access)) {
            continue;
        }
        MethodNode intArr = TransformerHelper.findMethodNode(classNode, null, "()[I");
        if (intArr == null) {
            continue;
        }
        if (Modifier.isStatic(intArr.access) || !Modifier.isAbstract(intArr.access)) {
            continue;
        }
        MethodNode boolRet = TransformerHelper.findMethodNode(classNode, null, "(Ljava/lang/Object;)I", true);
        if (boolRet == null) {
            continue;
        }
        if (Modifier.isStatic(boolRet.access) || !Modifier.isAbstract(boolRet.access)) {
            continue;
        }
        result.add(classNode);
    }
    return result;
}
 
Example 2
Source File: ReflectionUtil.java    From securify with Apache License 2.0 8 votes vote down vote up
/**
 * Get a constant's name by its value in a class.
 * @param cls
 * @param value
 * @return constant's name, null if nothing found
 */
public static String getConstantNameByValue(Class<?> cls, Object value) {
	for (Field f : cls.getDeclaredFields()) {
		int mod = f.getModifiers();
		if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
			try {
				if (Objects.equals(f.get(null), value)) {
					return f.getName();
				}
			}
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}
	return null;
}
 
Example 3
Source File: Introspector.java    From openjdk-jdk9 with GNU General Public License v2.0 8 votes vote down vote up
/**
 * Returns {@code true} if the given method is a "getter" method (where
 * "getter" method is a public method of the form getXXX or "boolean
 * isXXX")
 */
static boolean isReadMethod(Method method) {
    // ignore static methods
    int modifiers = method.getModifiers();
    if (Modifier.isStatic(modifiers))
        return false;

    String name = method.getName();
    Class<?>[] paramTypes = method.getParameterTypes();
    int paramCount = paramTypes.length;

    if (paramCount == 0 && name.length() > 2) {
        // boolean isXXX()
        if (name.startsWith(IS_METHOD_PREFIX))
            return (method.getReturnType() == boolean.class);
        // getXXX()
        if (name.length() > 3 && name.startsWith(GET_METHOD_PREFIX))
            return (method.getReturnType() != void.class);
    }
    return false;
}
 
Example 4
Source File: GuavaPatcher.java    From Bats with Apache License 2.0 8 votes vote down vote up
/**
 * Makes Guava stopwatch look like the old version for compatibility with hbase-server (for test purposes).
 */
private static void patchStopwatch() throws Exception {

  ClassPool cp = ClassPool.getDefault();
  CtClass cc = cp.get("com.google.common.base.Stopwatch");

  // Expose the constructor for Stopwatch for old libraries who use the pattern new Stopwatch().start().
  for (CtConstructor c : cc.getConstructors()) {
    if (!Modifier.isStatic(c.getModifiers())) {
      c.setModifiers(Modifier.PUBLIC);
    }
  }

  // Add back the Stopwatch.elapsedMillis() method for old consumers.
  CtMethod newmethod = CtNewMethod.make(
      "public long elapsedMillis() { return elapsed(java.util.concurrent.TimeUnit.MILLISECONDS); }", cc);
  cc.addMethod(newmethod);

  // Load the modified class instead of the original.
  cc.toClass();

  logger.info("Google's Stopwatch patched for old HBase Guava version.");
}
 
Example 5
Source File: MethodFinder.java    From TencentKona-8 with GNU General Public License v2.0 8 votes vote down vote up
/**
 * Finds method that is accessible from public class or interface through class hierarchy.
 *
 * @param method  object that represents found method
 * @return object that represents accessible method
 * @throws NoSuchMethodException if method is not accessible or is not found
 *                               in specified superclass or interface
 */
public static Method findAccessibleMethod(Method method) throws NoSuchMethodException {
    Class<?> type = method.getDeclaringClass();
    if (Modifier.isPublic(type.getModifiers()) && isPackageAccessible(type)) {
        return method;
    }
    if (Modifier.isStatic(method.getModifiers())) {
        throw new NoSuchMethodException("Method '" + method.getName() + "' is not accessible");
    }
    for (Type generic : type.getGenericInterfaces()) {
        try {
            return findAccessibleMethod(method, generic);
        }
        catch (NoSuchMethodException exception) {
            // try to find in superclass or another interface
        }
    }
    return findAccessibleMethod(method, type.getGenericSuperclass());
}
 
Example 6
Source File: AbstractTest.java    From TencentKona-8 with GNU General Public License v2.0 8 votes vote down vote up
protected static void assertImmutable(Class<?> cls) {
    assertTrue(Modifier.isPublic(cls.getModifiers()));
    assertTrue(Modifier.isFinal(cls.getModifiers()));
    Field[] fields = cls.getDeclaredFields();
    for (Field field : fields) {
        if (field.getName().contains("$") == false) {
            if (Modifier.isStatic(field.getModifiers())) {
                assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName());
            } else {
                assertTrue(Modifier.isPrivate(field.getModifiers()), "Field:" + field.getName());
                assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName());
            }
        }
    }
    Constructor<?>[] cons = cls.getDeclaredConstructors();
    for (Constructor<?> con : cons) {
        assertTrue(Modifier.isPrivate(con.getModifiers()));
    }
}
 
Example 7
Source File: AbstractTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 8 votes vote down vote up
protected static void assertImmutable(Class<?> cls) {
    assertTrue(Modifier.isPublic(cls.getModifiers()));
    assertTrue(Modifier.isFinal(cls.getModifiers()));
    Field[] fields = cls.getDeclaredFields();
    for (Field field : fields) {
        if (field.getName().contains("$") == false) {
            if (Modifier.isStatic(field.getModifiers())) {
                assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName());
            } else {
                assertTrue(Modifier.isPrivate(field.getModifiers()), "Field:" + field.getName());
                assertTrue(Modifier.isFinal(field.getModifiers()), "Field:" + field.getName());
            }
        }
    }
    Constructor<?>[] cons = cls.getDeclaredConstructors();
    for (Constructor<?> con : cons) {
        assertTrue(Modifier.isPrivate(con.getModifiers()));
    }
}
 
Example 8
Source File: ResolvedManifests.java    From cdep with Apache License 2.0 8 votes vote down vote up
@NotNull
static public List<NamedManifest> all() {
  List<NamedManifest> result = new ArrayList<>();
  for (Method method : ResolvedManifests.class.getMethods()) {
    if (!Modifier.isStatic(method.getModifiers())) {
      continue;
    }
    if (method.getReturnType() != TestManifest.class) {
      continue;
    }
    if (method.getParameterTypes().length != 0) {
      continue;
    }
    TestManifest resolved = (TestManifest) ReflectionUtils.invoke(method, null);
    String body = resolved.body;
    result.add(new NamedManifest(method.getName(), body, resolved.manifest));
  }
  return result;
}
 
Example 9
Source File: TypedJson.java    From gemfirexd-oss with Apache License 2.0 7 votes vote down vote up
void writeChildrens(Writer w, Object object) {
  Class klass = object.getClass();

  // If klass is a System class then set includeSuperClass to false.

  boolean includeSuperClass = klass.getClassLoader() != null;

  Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
  for (int i = 0; i < methods.length; i += 1) {
    try {
      Method method = methods[i];
      if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) {
        String name = method.getName();
        String key = "";
        if (name.startsWith("get")) {
          if ("getClass".equals(name) || "getDeclaringClass".equals(name)) {
            key = "";
          } else {
            key = name.substring(3);
          }
        } else if (name.startsWith("is")) {
          key = name.substring(2);
        }
        if (key.length() > 0 && Character.isUpperCase(key.charAt(0)) && method.getParameterTypes().length == 0) {
          if (key.length() == 1) {
            key = key.toLowerCase();
          } else if (!Character.isUpperCase(key.charAt(1))) {
            key = key.substring(0, 1).toLowerCase() + key.substring(1);
          }
          method.setAccessible(true);
          Object result = method.invoke(object, (Object[]) null);
          writeKeyValue(w, key, result, method.getReturnType());
        }
      }
    } catch (Exception ignore) {
    }
  }
}
 
Example 10
Source File: InterceptedStaticMethodsProcessor.java    From quarkus with Apache License 2.0 7 votes vote down vote up
private InterceptedStaticMethodBuildItem findMatchingMethod(int access, String name, String descriptor) {
    if (Modifier.isStatic(access)) {
        for (InterceptedStaticMethodBuildItem method : methods) {
            if (method.getMethod().name().equals(name)
                    && MethodDescriptor.of(method.getMethod()).getDescriptor().equals(descriptor)) {
                return method;
            }
        }
    }
    return null;
}
 
Example 11
Source File: KeyConstants.java    From sis with Apache License 2.0 7 votes vote down vote up
/**
 * Returns the internal array of key names. <strong>Do not modify the returned array.</strong>
 * This method should usually not be invoked, in order to avoid loading the inner Keys class.
 * The keys names are used only in rare situation, like {@link IndexedResourceBundle#list(Appendable)}
 * or in log records.
 */
@SuppressWarnings("ReturnOfCollectionOrArrayField")
final synchronized String[] getKeyNames() {
    if (keys == null) {
        String[] names;
        int length = 0;
        try {
            final Field[] fields = keysClass.getFields();
            names = new String[fields.length];
            for (final Field field : fields) {
                if (Modifier.isStatic(field.getModifiers()) && field.getType() == Short.TYPE) {
                    final int index = Short.toUnsignedInt((Short) field.get(null)) - IndexedResourceBundle.FIRST;
                    if (index >= length) {
                        length = index + 1;
                        if (length > names.length) {
                            // Usually don't happen, except for incomplete bundles.
                            names = Arrays.copyOf(names, length*2);
                        }
                    }
                    names[index] = field.getName();
                }
            }
        } catch (IllegalAccessException e) {
            names = CharSequences.EMPTY_ARRAY;
        }
        keys = ArraysExt.resize(names, length);
    }
    return keys;
}
 
Example 12
Source File: ParameterGenerator.java    From arctic-sea with Apache License 2.0 7 votes vote down vote up
private AbstractEsParameter getFieldValue(Field field) {
    if (Modifier.isFinal(field.getModifiers()) &&
        Modifier.isStatic(field.getModifiers()) &&
        Modifier.isPublic(field.getModifiers()) &&
        field.getType().isAssignableFrom(AbstractEsParameter.class)) {
        try {
            return (AbstractEsParameter) field.get(null);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return null;
}
 
Example 13
Source File: ClassDialogue.java    From JByteMod-Beta with GNU General Public License v2.0 7 votes vote down vote up
private void initializeFields() {
  fields = new ArrayList<>();
  for (Field f : clazz.getDeclaredFields()) {
    if (!Modifier.isStatic(f.getModifiers()) && !ignore(f.getName())) {
      f.setAccessible(true);
      fields.add(f);
    }
  }
}
 
Example 14
Source File: Resources.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isWritableField(Field field) {
    int modifiers = field.getModifiers();
    return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
            && !Modifier.isFinal(modifiers);
}
 
Example 15
Source File: Resources.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isWritableField(Field field) {
    int modifiers = field.getModifiers();
    return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
            && !Modifier.isFinal(modifiers);
}
 
Example 16
Source File: ReflectionPredicates.java    From raistlic-lib-commons-core with Apache License 2.0 6 votes vote down vote up
@Override
public boolean test(Member member) {
  return Modifier.isStatic(member.getModifiers());
}
 
Example 17
Source File: ReflectionNavigator.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public boolean isStaticMethod(Method method) {
    return Modifier.isStatic(method.getModifiers());
}
 
Example 18
Source File: Settings.java    From tribaltrouble with GNU General Public License v2.0 6 votes vote down vote up
private final static boolean hasValidModifiers(int mods) {
	return !Modifier.isStatic(mods) && !Modifier.isFinal(mods);
}
 
Example 19
Source File: ClassInfo.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public boolean isMethodStatic(int i) {
    return Modifier.isStatic(methodAccessFlags[i]);
}
 
Example 20
Source File: ReflectionUtil.java    From common-utils with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <code>Field</code>是否不用改写
 * 
 * @param field 目标<code>Field</code>
 * @return 如果是则返回<code>true</code>,否则返回<code>false</code>
 */
public static boolean notWriter(Field field) {
    return field == null || Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers());
}