List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.baidu.bjf.remoting.protobuf.IDLProxyObject.java
private IDLProxyObject doSetFieldValue(String fullField, String field, Object value, Object object, boolean useCache, Map<String, ReflectInfo> cachedFields) { Field f;/*from w ww.j a va 2 s .c om*/ // check cache if (useCache) { ReflectInfo info = cachedFields.get(fullField); if (info != null) { setField(value, info.target, info.field); return this; } } int index = field.indexOf('.'); if (index != -1) { String parent = field.substring(0, index); String sub = field.substring(index + 1); try { f = FieldUtils.findField(object.getClass(), parent); if (f == null) { throw new RuntimeException( "No field '" + parent + "' found at class " + object.getClass().getName()); } Class<?> type = f.getType(); f.setAccessible(true); Object o = f.get(object); if (o == null) { boolean memberClass = type.isMemberClass(); if (memberClass && Modifier.isStatic(type.getModifiers())) { Constructor<?> constructor = type.getConstructor(new Class[0]); constructor.setAccessible(true); o = constructor.newInstance(new Object[0]); } else if (memberClass) { Constructor<?> constructor = type.getConstructor(new Class[] { object.getClass() }); constructor.setAccessible(true); o = constructor.newInstance(new Object[] { object }); } else { o = type.newInstance(); } f.set(object, o); } return put(fullField, sub, value, o); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } f = FieldUtils.findField(object.getClass(), field); if (f == null) { throw new RuntimeException("No field '" + field + "' found at class " + object.getClass().getName()); } if (useCache && !cachedFields.containsKey(fullField)) { cachedFields.put(fullField, new ReflectInfo(f, object)); } setField(value, object, f); return this; }
From source file:com.anteam.demo.codec.common.CoderUtil.java
public static Object decode(Object source, EncryptAlgorithm encryptAlgorithm, byte[] key) throws DecoderException { if (source == null || encryptAlgorithm == null) { return null; }/* ww w . j a v a2 s . c om*/ Object result = source; if (source instanceof byte[]) { return CoderUtil.decode((byte[]) source, encryptAlgorithm, key); } else if (source instanceof String) { return CoderUtil.decode((String) source, encryptAlgorithm, key); } Field[] fields = source.getClass().getDeclaredFields(); for (Field field : fields) { Encrypted encrypted = field.getAnnotation(Encrypted.class); if (encrypted != null) { ReflectionUtils.makeAccessible(field); if (!Modifier.isStatic(field.getModifiers())) { try { field.set(source, CoderUtil.decode(field.get(source))); } catch (IllegalAccessException e) { LOG.error("?:" + source.getClass().getName() + ":" + field.getName(), e); } } } } return result; }
From source file:org.apache.bookkeeper.common.conf.ConfigDef.java
/** * Build the config definitation of a config class. * * @param configClass config class/*from w w w.j a v a2s . c o m*/ * @return config definition. */ @SuppressWarnings("unchecked") public static ConfigDef of(Class configClass) { ConfigDef.Builder builder = ConfigDef.builder(); Field[] fields = configClass.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) && field.getType().equals(ConfigKey.class)) { field.setAccessible(true); try { builder.withConfigKey((ConfigKey) field.get(null)); } catch (IllegalAccessException e) { log.error("Illegal to access {}#{}", configClass.getSimpleName(), field.getName(), e); } } } return builder.build(); }
From source file:com.nonninz.robomodel.RoboModel.java
List<Field> getSavedFields() { //TODO: cache results final List<Field> savedFields = new ArrayList<Field>(); final Field[] declaredFields = getClass().getDeclaredFields(); boolean saved; for (final Field field : declaredFields) { saved = false;/*from w ww .j a va 2 s.c o m*/ saved = saved || field.isAnnotationPresent(Save.class); // If @Save is present, save it saved = saved || Modifier.isPublic(field.getModifiers()); // If it is public, save it saved = saved && !Modifier.isStatic(field.getModifiers()); // If it is static, don't save it saved = saved && !field.isAnnotationPresent(Exclude.class); // If @Exclude, don't save it if (saved) { savedFields.add(field); } } return savedFields; }
From source file:com.example.basedemo.view.ViewInjectorImpl.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, Class<?> handlerType, ViewFinder finder) { if (handlerType == null || IGNORED.contains(handlerType)) { return;//from ww w . java 2 s . c om } // ? injectObject(handler, handlerType.getSuperclass(), finder); // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { Class<?> fieldType = field.getType(); if ( /* ??? */ Modifier.isStatic(field.getModifiers()) || /* ?final */ Modifier.isFinal(field.getModifiers()) || /* ? */ fieldType.isPrimitive() || /* ? */ fieldType.isArray()) { continue; } ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.value(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); } else { throw new RuntimeException("Invalid @ViewInject for " + handlerType.getSimpleName() + "." + field.getName()); } } catch (Throwable ex) { Log.e(ex.getMessage(), ex.toString()); } } } } // end inject view // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPrivate(method.getModifiers())) { continue; } //??event Event event = method.getAnnotation(Event.class); if (event != null) { try { // id? int[] values = event.value(); int[] parentIds = event.parentId(); int parentIdsLen = parentIds == null ? 0 : parentIds.length; //id?ViewInfo??? for (int i = 0; i < values.length; i++) { int value = values[i]; if (value > 0) { ViewInfo info = new ViewInfo(); info.value = value; info.parentId = parentIdsLen > i ? parentIds[i] : 0; method.setAccessible(true); EventListenerManager.addEventMethod(finder, info, event, handler, method); } } } catch (Throwable ex) { Log.e(ex.getMessage(), ex.toString()); } } } } // end inject event }
From source file:com.alta189.beaker.CommonEventManager.java
/** * Registers all the {@link EventHandler}s contained in the * Listener// ww w. jav a2 s. c om * * @param listener registration, not null * @param owner owner of the listener, not null * @throws com.alta189.beaker.exceptions.EventRegistrationException */ @Override public void registerListener(Listener listener, Named owner) throws EventRegistrationException { try { Validate.notNull(listener, "Listener cannot be null"); Validate.notNull(owner, "Owner cannot be null"); Validate.notNull(owner.getName(), "Owner's name cannot be null"); Validate.notEmpty(owner.getName(), "Owner's name cannot be empty"); for (Method method : listener.getClass().getDeclaredMethods()) { if (!method.isAnnotationPresent(EventHandler.class)) { continue; } EventHandler handler = method.getAnnotation(EventHandler.class); Validate.notNull(handler.ignoreCancelled(), "ignoredCancelled cannot be null"); Validate.notNull(handler.priority(), "Priority cannot be null"); if (!Modifier.isPublic(method.getModifiers())) { throw new EventRegistrationException("Method has to be public"); } if (Modifier.isStatic(method.getModifiers())) { throw new EventRegistrationException("Method cannot be static"); } if (method.getParameterTypes().length != 1) { throw new EventRegistrationException("Method cannot have more than one parameter"); } if (!Event.class.isAssignableFrom(method.getParameterTypes()[0])) { throw new EventRegistrationException("Method's parameter type has to extend class"); } EventExecutor executor = new AnnotatedEventExecutor(listener, method); HandlerRegistration registration = new HandlerRegistration(executor, handler.priority(), handler.ignoreCancelled(), owner); List<HandlerRegistration> list = getRegistrationList( (Class<? extends Event>) method.getParameterTypes()[0], true); list.add(registration); sortList(list); } } catch (EventRegistrationException e) { throw e; } catch (Exception e) { throw new EventRegistrationException(e); } }
From source file:org.apache.syncope.client.console.wizards.resources.ResourceMappingPanel.java
private static void initFieldNames(final Class<?> entityClass, final Set<String> keys) { List<Class<?>> classes = ClassUtils.getAllSuperclasses(entityClass); classes.add(entityClass);//w w w . j a va2 s.com for (Class<?> clazz : classes) { for (Field field : clazz.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers()) && !Collection.class.isAssignableFrom(field.getType()) && !Map.class.isAssignableFrom(field.getType())) { keys.add(field.getName()); } } } }
From source file:com.esofthead.mycollab.vaadin.ui.BeanList.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private RowDisplayHandler<T> constructRowDisplayHandler() { RowDisplayHandler<T> rowHandler; try {//from ww w . j ava 2 s.co m if (rowDisplayHandler.getEnclosingClass() != null && !Modifier.isStatic(rowDisplayHandler.getModifiers())) { Constructor constructor = rowDisplayHandler .getDeclaredConstructor(rowDisplayHandler.getEnclosingClass()); rowHandler = (RowDisplayHandler<T>) constructor.newInstance(parentComponent); } else { rowHandler = rowDisplayHandler.newInstance(); } rowHandler.setOwner(this); return rowHandler; } catch (Exception e) { throw new MyCollabException(e); } }
From source file:com.asuka.android.asukaandroid.view.ViewInjectorImpl.java
@SuppressWarnings("ConstantConditions") private static void injectObject(Object handler, Class<?> handlerType, ViewFinder finder) { if (handlerType == null || IGNORED.contains(handlerType)) { return;//from ww w. j a va2 s. c o m } // ? injectObject(handler, handlerType.getSuperclass(), finder); // inject view Field[] fields = handlerType.getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { Class<?> fieldType = field.getType(); if ( /* ??? */ Modifier.isStatic(field.getModifiers()) || /* ?final */ Modifier.isFinal(field.getModifiers()) || /* ? */ fieldType.isPrimitive() || /* ? */ fieldType.isArray()) { continue; } ViewInject viewInject = field.getAnnotation(ViewInject.class); if (viewInject != null) { try { View view = finder.findViewById(viewInject.value(), viewInject.parentId()); if (view != null) { field.setAccessible(true); field.set(handler, view); } else { throw new RuntimeException("Invalid @ViewInject for " + handlerType.getSimpleName() + "." + field.getName()); } } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } } } } // end inject view // inject event Method[] methods = handlerType.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPrivate(method.getModifiers())) { continue; } //??event Event event = method.getAnnotation(Event.class); if (event != null) { try { // id? int[] values = event.value(); int[] parentIds = event.parentId(); int parentIdsLen = parentIds == null ? 0 : parentIds.length; //id?ViewInfo??? for (int i = 0; i < values.length; i++) { int value = values[i]; if (value > 0) { ViewInfo info = new ViewInfo(); info.value = value; info.parentId = parentIdsLen > i ? parentIds[i] : 0; method.setAccessible(true); EventListenerManager.addEventMethod(finder, info, event, handler, method); } } } catch (Throwable ex) { LogUtil.e(ex.getMessage(), ex); } } } } // end inject event }
From source file:edu.usu.sdl.openstorefront.util.ReflectionUtil.java
/** * This gets all declared field of the whole object hierarchy * * @param typeClass//from ww w . j a v a2 s .c o m * @return */ public static List<Field> getAllFields(Class typeClass) { Objects.requireNonNull(typeClass, "Class is required"); List<Field> fields = new ArrayList<>(); if (typeClass.getSuperclass() != null) { fields.addAll(getAllFields(typeClass.getSuperclass())); } for (Field field : typeClass.getDeclaredFields()) { if (Modifier.isStatic(field.getModifiers()) == false && Modifier.isFinal(field.getModifiers()) == false) { fields.add(field); } } return fields; }