List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.alibaba.dubbo.config.spring.AnnotationBean.java
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (!isMatchPackage(bean)) { return bean; }/* ww w. j a v a 2 s . c o m*/ Method[] methods = bean.getClass().getMethods(); for (Method method : methods) { String name = method.getName(); if (name.length() > 3 && name.startsWith("set") && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { try { Reference reference = method.getAnnotation(Reference.class); if (reference != null) { Object value = refer(reference, method.getParameterTypes()[0]); if (value != null) { method.invoke(bean, new Object[] {}); } } } catch (Throwable e) { logger.error("Failed to init remote service reference at method " + name + " in class " + bean.getClass().getName() + ", cause: " + e.getMessage(), e); } } } Field[] fields = bean.getClass().getDeclaredFields(); for (Field field : fields) { try { if (!field.isAccessible()) { field.setAccessible(true); } Reference reference = field.getAnnotation(Reference.class); if (reference != null) { Object value = refer(reference, field.getType()); if (value != null) { field.set(bean, value); } } } catch (Throwable e) { logger.error("Failed to init remote service reference at filed " + field.getName() + " in class " + bean.getClass().getName() + ", cause: " + e.getMessage(), e); } } return bean; }
From source file:com.haulmont.cuba.core.sys.MetadataLoader.java
/** * Guesses id for a datatype registered in legacy datatypes.xml file. * For backward compatibility only./*from w w w .jav a2 s .com*/ */ protected String guessDatatypeId(Datatype datatype) { if (datatype instanceof BigDecimalDatatype) return "decimal"; if (datatype instanceof BooleanDatatype) return "boolean"; if (datatype instanceof ByteArrayDatatype) return "byteArray"; if (datatype instanceof DateDatatype) return "date"; if (datatype instanceof DateTimeDatatype) return "dateTime"; if (datatype instanceof DoubleDatatype) return "double"; if (datatype instanceof IntegerDatatype) return "int"; if (datatype instanceof LongDatatype) return "long"; if (datatype instanceof StringDatatype) return "string"; if (datatype instanceof TimeDatatype) return "time"; if (datatype instanceof UUIDDatatype) return "uuid"; try { Field nameField = datatype.getClass().getField("NAME"); if (Modifier.isStatic(nameField.getModifiers()) && nameField.isAccessible()) { return (String) nameField.get(null); } } catch (Exception e) { log.trace("Cannot get NAME static field value: " + e); } throw new IllegalStateException("Cannot guess id for datatype " + datatype); }
From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java
private static void addFields(Object target, Class<?> startClass, Class<?> stopClass, LinkedHashMap<String, Object> map) { if (startClass != stopClass) { addFields(target, startClass.getSuperclass(), stopClass, map); }// w w w. j av a2 s . co m Field[] fields = startClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || Modifier.isPrivate(field.getModifiers())) { continue; } try { field.setAccessible(true); Object o = field.get(target); if (o != null && o.getClass().isArray()) { try { o = Arrays.asList((Object[]) o); } catch (Throwable e) { } } map.put(field.getName(), o); } catch (Throwable e) { e.printStackTrace(); } } }
From source file:RssAtomGenerationTest.java
public static <T> T convertObject(final T destination, final Object source, FeedType type, Object... context) { Map<Class, Object> contentMap = new HashMap<Class, Object>(); for (Object o : context) { contentMap.put(o.getClass(), o); }//from w w w .j a v a 2 s. c om final Method[] methods = source.getClass().getMethods(); for (Method method : methods) { final boolean refPresent = method.isAnnotationPresent(SyndicationRefs.class); if (refPresent || (method.isAnnotationPresent(SyndicationElement.class) && method.getAnnotation(SyndicationElement.class).type().equals(type))) { SyndicationElement annotation = null; if (refPresent) { final SyndicationElement[] value = method.getAnnotation(SyndicationRefs.class).value(); for (SyndicationElement element : value) { if (element.type().equals(type)) { annotation = element; break; } } if (annotation == null) { continue; } } else { annotation = method.getAnnotation(SyndicationElement.class); } //final SyndicationElement annotation = final String name = annotation.name(); try { final Object initValue = method.invoke(source); Object value = null; if (!(annotation.converter().isAssignableFrom(NoopConverter.class))) { final Converter converter = annotation.converter().newInstance(); value = converter.convert(initValue); } if (!(annotation.transformer().isAssignableFrom(NoopTransformer.class))) { contentMap.put(initValue.getClass(), initValue); final Class<?> transformer = annotation.transformer(); final Method[] transformerMethods = transformer.getMethods(); for (Method transformerMethod : transformerMethods) { if (transformerMethod.isAnnotationPresent(ContextTransformable.class)) { final Class<?>[] parameterTypes = transformerMethod.getParameterTypes(); List<Object> parameters = new ArrayList<Object>(); for (Class clazz : parameterTypes) { if (contentMap.containsKey(clazz)) { parameters.add(contentMap.get(clazz)); } else { boolean found = false; for (Object obj : contentMap.values()) { if (clazz.isInstance(obj)) { parameters.add(obj); found = true; break; } } if (!found) { parameters.add(null); } } } if (Modifier.isStatic(transformerMethod.getModifiers())) { value = transformerMethod.invoke(null, parameters.toArray()); } else { value = transformerMethod.invoke(transformer.newInstance(), parameters.toArray()); } break; } } } BeanUtils.setProperty(destination, name, value); } catch (Exception e) { log.error("test", e); e.printStackTrace(); } } } return destination; }
From source file:net.neevek.android.lib.paginize.util.AnnotationUtils.java
private static Object getTargetListener(Class clazz, Object object, Map<Class, Object> targetListenerCache, Class targetListenerClass, String tag) throws InstantiationException, IllegalAccessException, InvocationTargetException { if (targetListenerClass == null || targetListenerClass == void.class) { return null; }/*from ww w . j a v a 2 s . c o m*/ Object targetListener = targetListenerCache.get(targetListenerClass); if (targetListener == null) { try { boolean isStatic = Modifier.isStatic(targetListenerClass.getModifiers()); if (isStatic) { Constructor ctor = targetListenerClass.getDeclaredConstructor(); ctor.setAccessible(true); targetListener = ctor.newInstance(); } else { Constructor ctor = targetListenerClass.getDeclaredConstructor(clazz); ctor.setAccessible(true); targetListener = ctor.newInstance(object); } targetListenerCache.put(targetListenerClass, targetListener); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("The 'listener' field: '" + targetListenerClass.getSimpleName() + "' in " + tag + " must contain a default constructor without arguments."); } } return targetListener; }
From source file:ch.ifocusit.plantuml.classdiagram.ClassDiagramBuilder.java
public ClassMethod[] readMethods(Class aClass) { return Stream.of(aClass.getDeclaredMethods()) // only public and non static methods .filter(method -> !Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers())) .map(this::createClassMethod) // excludes specific fields .filter(filterMethods()).sorted().toArray(ClassMethod[]::new); }
From source file:com.zenesis.qx.remote.ProxyTypeImpl.java
/** * Constructor, used for defining interfaces which are to be proxied * @param className/* w w w . j a va 2 s . com*/ * @param methods */ public ProxyTypeImpl(ProxyType superType, Class clazz, Set<ProxyType> interfaces) { super(); if (interfaces == null) interfaces = Collections.EMPTY_SET; this.superType = superType; this.interfaces = interfaces; this.clazz = clazz; MethodsCompiler methodsCompiler = new MethodsCompiler(); // Get a complete list of methods from the interfaces that the new class has to // implement; we include methods marked as DoNotProxy so that we can check for // conflicting instructions if (!clazz.isInterface()) { // Get a full list of the interfaces which our class has to implement HashSet<ProxyType> allInterfaces = new HashSet<ProxyType>(); getAllInterfaces(allInterfaces, interfaces); for (ProxyType ifcType : allInterfaces) { try { methodsCompiler.addMethods(Class.forName(ifcType.getClassName()), true); } catch (ClassNotFoundException e) { throw new IllegalStateException("Cannot find class " + ifcType.getClassName()); } } } boolean defaultProxy = false; if (clazz.isInterface()) defaultProxy = true; else { for (Class tmp = clazz; tmp != null; tmp = tmp.getSuperclass()) { if (factoryMethod == null) { for (Method method : tmp.getDeclaredMethods()) { if (method.isAnnotationPresent(FactoryMethod.class)) { if (!Modifier.isStatic(method.getModifiers())) throw new IllegalStateException("Cannot use method " + method + " as FactoryMethod because it is not static"); factoryMethod = method; method.setAccessible(true); break; } } } if (tmp.isAnnotationPresent(AlwaysProxy.class)) { defaultProxy = true; break; } else if (tmp.isAnnotationPresent(ExplicitProxyOnly.class)) { break; } } } // If the class does not have any proxied interfaces or the class is marked with // the AlwaysProxy annotation, then we take methods from the class definition methodsCompiler.addMethods(clazz, defaultProxy); methodsCompiler.checkValid(); methodsCompiler.removeSuperTypeMethods(); // Load properties HashMap<String, ProxyEvent> events = new HashMap<String, ProxyEvent>(); HashMap<String, ProxyProperty> properties = new HashMap<String, ProxyProperty>(); Properties annoProperties = (Properties) clazz.getAnnotation(Properties.class); if (annoProperties != null) { for (Property anno : annoProperties.value()) { ProxyProperty property = new ProxyPropertyImpl(clazz, anno.value(), anno, annoProperties); properties.put(property.getName(), property); ProxyEvent event = property.getEvent(); if (event != null) events.put(event.getName(), event); } } for (Field field : clazz.getDeclaredFields()) { Property anno = field.getAnnotation(Property.class); if (anno != null) { ProxyProperty property = new ProxyPropertyImpl(clazz, anno.value().length() > 0 ? anno.value() : field.getName(), anno, annoProperties); properties.put(property.getName(), property); ProxyEvent event = property.getEvent(); if (event != null) events.put(event.getName(), event); } } for (Method method : clazz.getDeclaredMethods()) { String name = method.getName(); if (name.length() < 4 || !name.startsWith("get") || !Character.isUpperCase(name.charAt(3))) continue; Property anno = method.getAnnotation(Property.class); if (anno == null) continue; name = Character.toLowerCase(name.charAt(3)) + name.substring(4); if (properties.containsKey(name)) continue; ProxyProperty property = new ProxyPropertyImpl(clazz, anno.value().length() > 0 ? anno.value() : name, anno, annoProperties); properties.put(property.getName(), property); ProxyEvent event = property.getEvent(); if (event != null) events.put(event.getName(), event); } // Classes need to have all inherited properties added if (!clazz.isInterface()) { for (ProxyType ifc : interfaces) addProperties((ProxyTypeImpl) ifc, properties); } // Remove property accessors for (ProxyProperty prop : properties.values()) methodsCompiler.removePropertyAccessors((ProxyPropertyImpl) prop); // Load events if (clazz.isAnnotationPresent(Events.class)) { Events annoEvents = (Events) clazz.getAnnotation(Events.class); for (Event annoEvent : annoEvents.value()) { if (!events.containsKey(annoEvent.value())) events.put(annoEvent.value(), new ProxyEvent(annoEvent)); } } // Classes need to have all inherited events added if (!clazz.isInterface()) { for (ProxyType type : interfaces) addEvents((ProxyTypeImpl) type, events); } // Save this.properties = properties.isEmpty() ? null : properties; this.events = events.isEmpty() ? null : events; this.methods = methodsCompiler.toArray(); }
From source file:ca.sqlpower.object.PersistedSPObjectTest.java
/** * All persistable {@link SPObject} implementations must define a static * final field which is a list defining the absolute ordering of that * class's child type classes. This method ensures that list is retrievable * by reflection from the object, that the field is public, static, and * final, and that it is nonempty for classes that allow children and empty * for classes that do not allow children. */// w w w .j a v a2 s . c o m @SuppressWarnings("unchecked") public void testAllowedChildTypesField() throws Exception { Class<? extends SPObject> classUnderTest = getSPObjectUnderTest().getClass(); Field childOrderField; try { childOrderField = classUnderTest.getDeclaredField("allowedChildTypes"); } catch (NoSuchFieldException ex) { fail("Persistent " + classUnderTest + " must have a static final field called allowedChildTypes"); throw new AssertionError(); // NOTREACHED } assertEquals("The allowedChildTypes field must be final", true, Modifier.isFinal(childOrderField.getModifiers())); assertEquals("The allowedChildTypes field must be static", true, Modifier.isStatic(childOrderField.getModifiers())); // Note: in the future, we will change this to require that the field is private assertEquals("The allowedChildTypes field must be public", true, Modifier.isPublic(childOrderField.getModifiers())); List<Class<? extends SPObject>> allowedChildTypes = (List<Class<? extends SPObject>>) childOrderField .get(null); if (getSPObjectUnderTest().allowsChildren()) { assertFalse(allowedChildTypes.isEmpty()); } else { assertTrue(allowedChildTypes.isEmpty()); } }
From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java
public static void checkLegalCloudIdDef(Object obj) { for (Field f : obj.getClass().getDeclaredFields()) { if (f.getAnnotation(CloudObjectId.class) != null) { if (!f.getType().equals(UUID.class) && !f.getType().equals(String.class) && !f.getType().equals(Object.class)) throw new IllegalDefinitionException("Illegal field type " + f.getType().getName() + " annotated with " + "@CloudObjectId. You may only annotate java.lang.Object, java.lang.String and java.util.UUID"); if (Modifier.isStatic(f.getModifiers())) throw new IllegalDefinitionException("Illegal field " + f.getName() + " annotated with @CloudObjectId. " + "Field has to be non-static."); }/*from ww w .j a v a2s . co m*/ } }
From source file:com.espertech.esper.util.MethodResolver.java
private static boolean isPublicAndStatic(Method method, boolean allowInstance) { int modifiers = method.getModifiers(); if (allowInstance) { return Modifier.isPublic(modifiers); } else {// ww w . jav a 2 s . c om return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers); } }