List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformer.java
private boolean isStaticMethodInvokation(Object targetObject) { boolean isNullTarget = targetObject == null; boolean isStaticMethodToInvoke = Modifier.isStatic(methodToInvokeForTransformation.getModifiers()); if (isNullTarget && isStaticMethodToInvoke) { return true; }/*from w ww . ja v a 2 s . c o m*/ boolean isTargetObjectAClass = targetObject instanceof Class<?>; if (isTargetObjectAClass) { Class<?> targetClass = (Class<?>) targetObject; Class<?> declaringClass = methodToInvokeForTransformation.getDeclaringClass(); return targetClass.isAssignableFrom(declaringClass); } return false; }
From source file:io.github.benas.randombeans.util.ReflectionUtils.java
/** * Check if a field is static.// w ww . ja va 2 s .com * * @param field the field to check * @return true if the field is static, false otherwise */ public static boolean isStatic(final Field field) { return Modifier.isStatic(field.getModifiers()); }
From source file:com.medallia.spider.api.StRenderer.java
/** @return the action method of the given class; throws AssertionError if no such method exists */ private static Method findActionMethod(Class<?> clazz) { Method am = ACTION_METHOD_MAP.get(clazz); if (am != null) return am; for (Method m : CollUtils.concat(Arrays.asList(clazz.getMethods()), Arrays.asList(clazz.getDeclaredMethods()))) { if (m.getName().equals("action")) { int modifiers = m.getModifiers(); if (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers)) continue; m.setAccessible(true);// w ww.j a v a 2s . co m ACTION_METHOD_MAP.put(clazz, m); return m; } } throw new AssertionError("No action method found in " + clazz); }
From source file:com.pinterest.terrapin.tools.TerrapinAdmin.java
public static void main(String[] args) throws Exception { PropertiesConfiguration configuration = TerrapinUtil .readPropertiesExitOnFailure(System.getProperties().getProperty("terrapin.config")); TerrapinAdmin admin = new TerrapinAdmin(configuration); String action = args[0];/*ww w . j a v a 2 s.co m*/ Method[] methods = admin.getClass().getMethods(); boolean done = false; for (Method method : methods) { if (method.getName().equals(action) && !Modifier.isStatic(method.getModifiers())) { method.invoke(admin, (Object) args); done = true; } } if (!done) { LOG.error("Could not find a function call for " + args[0]); System.exit(1); } }
From source file:com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.java
/** * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link Reference @Reference} fields * * @param beanClass The {@link Class} of Bean * @return non-null {@link List}/*from ww w . ja v a2 s. c om*/ */ private List<InjectionMetadata.InjectedElement> findFieldReferenceMetadata(final Class<?> beanClass) { final List<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>(); ReflectionUtils.doWithFields(beanClass, new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { Reference reference = getAnnotation(field, Reference.class); if (reference != null) { if (Modifier.isStatic(field.getModifiers())) { if (logger.isWarnEnabled()) { logger.warn("@Reference annotation is not supported on static fields: " + field); } return; } elements.add(new ReferenceFieldElement(field, reference)); } } }); return elements; }
From source file:com.l2jfree.util.Introspection.java
private static boolean writeFields(Class<?> c, Object accessor, StringBuilder dest, String eol, boolean init) { if (c == null) throw new IllegalArgumentException("No class specified."); else if (!c.isInstance(accessor)) throw new IllegalArgumentException(accessor + " is not a " + c.getCanonicalName()); for (Field f : c.getDeclaredFields()) { int mod = f.getModifiers(); if (Modifier.isStatic(mod)) continue; if (init) init = false;//from w w w . j a va 2 s. c o m else if (eol == null) dest.append(", "); String fieldName = null; final Column column = f.getAnnotation(Column.class); if (column != null) fieldName = column.name(); if (StringUtils.isEmpty(fieldName)) fieldName = f.getName(); dest.append(fieldName); dest.append(" = "); try { f.setAccessible(true); Object val = f.get(accessor); if (accessor == val) dest.append("this"); else deepToString(val, dest, null); } catch (Exception e) { dest.append("???"); } finally { try { f.setAccessible(false); } catch (Exception e) { // ignore } } if (eol != null) dest.append(eol); } return !init; }
From source file:code.elix_x.excore.utils.nbt.mbt.encoders.NBTClassEncoder.java
public void populate(MBT mbt, NBTTagCompound nbt, Object o) { Class clazz = o.getClass();//from w w w . ja v a 2 s . c om try { while (clazz != null && clazz != Object.class) { if (!clazz.isAnnotationPresent(MBTIgnore.class)) { for (Field field : clazz.getDeclaredFields()) { if (!field.isAnnotationPresent(MBTIgnore.class)) { field.setAccessible(true); if (nbt.hasKey(field.getName())) { if (encodeStatic || !Modifier.isStatic(field.getModifiers())) { if (Modifier.isFinal(field.getModifiers())) { if (encodeFinal) { Field modifiers = Field.class.getDeclaredField("modifiers"); modifiers.setAccessible(true); modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); if (field.getGenericType() instanceof ParameterizedType) { Type[] types = ((ParameterizedType) field.getGenericType()) .getActualTypeArguments(); Class[] clas = new Class[] {}; for (Type type : types) { if (type instanceof Class) { clas = ArrayUtils.add(clas, (Class) type); } } field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType(), clas)); } else { field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType())); } } } else { if (field.getGenericType() instanceof ParameterizedType) { Type[] types = ((ParameterizedType) field.getGenericType()) .getActualTypeArguments(); Class[] clas = new Class[] {}; for (Type type : types) { if (type instanceof Class) { clas = ArrayUtils.add(clas, (Class) type); } } field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType(), clas)); } else { field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType())); } } } } } } } clazz = encodeSuper ? clazz.getSuperclass() : Object.class; } } catch (IllegalArgumentException e) { Throwables.propagate(e); } catch (IllegalAccessException e) { Throwables.propagate(e); } catch (NoSuchFieldException e) { Throwables.propagate(e); } catch (SecurityException e) { Throwables.propagate(e); } }
From source file:dk.netarkivet.common.utils.ApplicationUtils.java
/** * Starts up an application. The applications class must: * (i) Have a static getInstance() method which returns a * an instance of itself.//from ww w. ja v a2s. com * (ii) Implement CleanupIF. * If the class cannot be started and a shutdown hook added, the JVM * exits with a return code depending on the problem: * 1 means wrong arguments * 2 means no factory method exists for class * 3 means couldn't instantiate class * 4 means couldn't add shutdown hook * 5 means couldn't add liveness logger * 6 means couldn't add remote management * @param c The class to be started. * @param args The arguments to the application (should be empty). */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void startApp(Class c, String[] args) { String appName = c.getName(); Settings.set(CommonSettings.APPLICATION_NAME, appName); logAndPrint("Starting " + appName + "\n" + Constants.getVersionString()); log.info("Using settings files '" + StringUtils.conjoin(File.pathSeparator, Settings.getSettingsFiles()) + "'"); checkArgs(args); dirMustExist(FileUtils.getTempDir()); Method factoryMethod = null; CleanupIF instance = null; // Start the remote management connector try { MBeanConnectorCreator.exposeJMXMBeanServer(); log.trace("Added remote management for " + appName); } catch (Throwable e) { logExceptionAndPrint("Could not add remote management for class " + appName, e); System.exit(EXCEPTION_WHEN_ADDING_MANAGEMENT); } // Get the factory method try { factoryMethod = c.getMethod("getInstance", (Class[]) null); int modifier = factoryMethod.getModifiers(); if (!Modifier.isStatic(modifier)) { throw new Exception("getInstance is not static"); } logAndPrint(appName + " Running"); } catch (Throwable e) { logExceptionAndPrint("Class " + appName + " does not have required" + "factory method", e); System.exit(NO_FACTORY_METHOD); } // Invoke the factory method try { log.trace("Invoking factory method."); instance = (CleanupIF) factoryMethod.invoke(null, (Object[]) null); log.trace("Factory method invoked."); } catch (Throwable e) { logExceptionAndPrint("Could not start class " + appName, e); System.exit(EXCEPTION_WHILE_INSTANTIATING); } // Add the shutdown hook try { log.trace("Adding shutdown hook for " + appName); Runtime.getRuntime().addShutdownHook((new CleanupHook(instance))); log.trace("Added shutdown hook for " + appName); } catch (Throwable e) { logExceptionAndPrint("Could not add shutdown hook for class " + appName, e); System.exit(EXCEPTION_WHEN_ADDING_SHUTDOWN_HOOK); } }
From source file:de.taimos.dvalin.cloud.aws.AWSClientBeanPostProcessor.java
private InjectionMetadata buildResourceMetadata(Class<?> clazz) { LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>(); Class<?> targetClass = clazz; do {/* ww w.ja v a 2s . c o m*/ LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>(); for (Field field : targetClass.getDeclaredFields()) { if (field.isAnnotationPresent(AWSClient.class)) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException("@AWSClient annotation is not supported on static fields"); } currElements.add(new AWSClientElement(field, null, field.getAnnotation(AWSClient.class))); } } for (Method method : targetClass.getDeclaredMethods()) { Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { continue; } if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { if (bridgedMethod.isAnnotationPresent(AWSClient.class)) { if (Modifier.isStatic(method.getModifiers())) { throw new IllegalStateException( "@AWSClient annotation is not supported on static methods"); } if (method.getParameterTypes().length != 1) { throw new IllegalStateException( "@AWSClient annotation requires a single-arg method: " + method); } PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); currElements.add(new AWSClientElement(method, pd, method.getAnnotation(AWSClient.class))); } } } elements.addAll(0, currElements); targetClass = targetClass.getSuperclass(); } while ((targetClass != null) && (targetClass != Object.class)); return new InjectionMetadata(clazz, elements); }
From source file:com.tongbanjie.tarzan.rpc.protocol.RpcCommand.java
public CustomHeader decodeCustomHeader(Class<? extends CustomHeader> classHeader) throws RpcCommandException { CustomHeader objectHeader;/*w w w. j a v a2 s . com*/ try { objectHeader = classHeader.newInstance(); } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } if (MapUtils.isNotEmpty(this.customFields)) { Field[] fields = getClazzFields(classHeader); for (Field field : fields) { String fieldName = field.getName(); Class clazz = field.getType(); if (Modifier.isStatic(field.getModifiers()) || fieldName.startsWith("this")) { continue; } String value = this.customFields.get(fieldName); if (value == null) { continue; } field.setAccessible(true); Object valueParsed; if (clazz.isEnum()) { valueParsed = Enum.valueOf(clazz, value); } else { String type = getCanonicalName(clazz); try { valueParsed = ClassUtils.parseSimpleValue(type, value); } catch (ParseException e) { throw new RpcCommandException("Encode the header failed, the custom field <" + fieldName + "> type <" + getCanonicalName(clazz) + "> parse error:" + e.getMessage()); } } if (valueParsed == null) { throw new RpcCommandException("Encode the header failed, the custom field <" + fieldName + "> type <" + getCanonicalName(clazz) + "> is not supported."); } try { field.set(objectHeader, valueParsed); } catch (IllegalAccessException e) { throw new RpcCommandException( "Encode the header failed, set the value of field < " + fieldName + "> error.", e); } } objectHeader.checkFields(); } return objectHeader; }