List of usage examples for java.lang.reflect Field getModifiers
public int getModifiers()
From source file:py.una.pol.karaku.test.test.ValidationMessagesTest.java
@Test public void testConstants() { ReflectionUtils.doWithFields(ValidationMessages.class, new FieldCallback() { @Override/*from w w w . j a v a 2 s . com*/ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { String value = (String) field.get(null); assertNotNull(value); assertTrue("Key: " + value + " not found", keys.contains(value)); } }, new FieldFilter() { @Override public boolean matches(Field field) { int modifiers = field.getModifiers(); return Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) && Modifier.isPublic(modifiers); } }); }
From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java
@Test public void notAllowdModifiers() throws IllegalAccessException { Field[] declaredFields = Modifier.class.getDeclaredFields(); int maxModifierValue = 0; for (Field field : declaredFields) { int modifiers = field.getModifiers(); if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers) && field.getType().equals(Integer.TYPE)) { try { Integer value = (Integer) field.get(Modifier.class); maxModifierValue = Math.max(maxModifierValue, value.intValue()); } catch (IllegalArgumentException e) { throw new AssertionError(e); }/*from www .j av a 2s. c om*/ } } maxModifierValue = maxModifierValue << 2; try { memberCriteria.withModifiers(maxModifierValue); throw new AssertionError("modifiers are not allowed"); } catch (IllegalArgumentException e) { String message = e.getMessage(); assertTrue(message.startsWith("modifiers must be")); } }
From source file:org.apache.hadoop.net.TestDNS.java
/** * Change DNS#cachedHostName to something which cannot be a real * host name. Uses reflection since it is a 'private final' field. *//*from w w w.ja v a 2 s. c om*/ private String changeDnsCachedHostname(final String newHostname) throws Exception { final String oldCachedHostname = DNS.getDefaultHost(DEFAULT); Field field = DNS.class.getDeclaredField("cachedHostname"); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.set(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newHostname); return oldCachedHostname; }
From source file:com.pwn9.pwnchat.config.ConfigObject.java
protected boolean doSkip(Field field) { return Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()) || Modifier.isPrivate(field.getModifiers()); }
From source file:org.javelin.sws.ext.bind.internal.metadata.PropertyCallback.java
/** * <p>Reads class' metadata and returns a {@link XmlEventsPattern pattern of XML events} which may be used to marshal * an object of the analyzed class.<?p> * //from www. ja va 2 s . co m * @return */ public TypedPattern<T> analyze() { TypedPattern<T> result = this.patternRegistry.findPatternByClass(this.clazz); if (result != null) return result; log.trace("Analyzing {} class with {} type name", this.clazz.getName(), this.typeName); // analyze fields ReflectionUtils.doWithFields(this.clazz, this, new FieldFilter() { @Override public boolean matches(Field field) { return !Modifier.isStatic(field.getModifiers()); } }); // analyze get/set methods - even private ones! ReflectionUtils.doWithMethods(this.clazz, this, new MethodFilter() { @Override public boolean matches(Method method) { boolean match = true; // is it getter? match &= method.getName().startsWith("get"); match &= method.getParameterTypes().length == 0; match &= method.getReturnType() != Void.class; // is there a setter? if (match) { Method setter = ReflectionUtils.findMethod(clazz, method.getName().replaceFirst("^get", "set"), method.getReturnType()); // TODO: maybe allow non-void returning setters as Spring-Framework already does? Now: yes match = setter != null || Collection.class.isAssignableFrom(method.getReturnType()); } return match; } }); if (this.valueMetadata != null && this.childElementMetadata.size() == 0 && this.childAttributeMetadata.size() == 0) { // we have a special case, where Java class becomes simpleType: // - formatting the analyzed class is really formatting the value // - the type information of the analyzed class is not changed! log.trace("Changing {} class' pattern to SimpleContentPattern", this.clazz.getName()); SimpleContentPattern<T> valuePattern = SimpleContentPattern.newValuePattern(this.typeName, this.clazz); SimpleContentPattern<?> simpleTypePattern = (SimpleContentPattern<?>) this.valueMetadata.getPattern(); valuePattern.setFormatter( PeelingFormatter.newPeelingFormatter(this.valueMetadata, simpleTypePattern.getFormatter())); result = valuePattern; } else { if (this.valueMetadata != null && this.childElementMetadata.size() > 0) { throw new RuntimeException("TODO: can't mix @XmlValue and @XmlElements"); } // we have complex type (possibly with simpleContent, when there's @XmlValue + one or more @XmlAttributes) // @XmlAttributes first. then @XmlElements this.childAttributeMetadata.addAll(this.childElementMetadata); if (this.valueMetadata != null) this.childAttributeMetadata.add(this.valueMetadata); result = ComplexTypePattern.newContentModelPattern(this.typeName, this.clazz, this.childAttributeMetadata); } if (log.isTraceEnabled()) log.trace("-> Class {} was mapped to {} with {} XSD type", this.clazz.getName(), result, this.typeName); return result; }
From source file:de.codesourcery.eve.skills.util.XMLMapper.java
private BeanDescription createBeanDescription(Class<?> clasz) { BeanDescription result = new BeanDescription(); for (java.lang.reflect.Field f : clasz.getDeclaredFields()) { final int modifiers = f.getModifiers(); if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) { continue; }/*w w w . j av a 2 s . c om*/ if (!f.isAccessible()) { f.setAccessible(true); } result.addField(f); } return result; }
From source file:org.codehaus.griffon.commons.ClassPropertyFetcher.java
private void init() { FieldCallback fieldCallback = new ReflectionUtils.FieldCallback() { public void doWith(Field field) { if (field.isSynthetic()) return; final int modifiers = field.getModifiers(); if (!Modifier.isPublic(modifiers)) return; final String name = field.getName(); if (name.indexOf('$') == -1) { boolean staticField = Modifier.isStatic(modifiers); if (staticField) { staticFetchers.put(name, new FieldReaderFetcher(field, staticField)); } else { instanceFetchers.put(name, new FieldReaderFetcher(field, staticField)); }/*from www .ja v a 2 s .co m*/ } } }; MethodCallback methodCallback = new ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if (method.isSynthetic()) return; if (!Modifier.isPublic(method.getModifiers())) return; if (Modifier.isStatic(method.getModifiers()) && method.getReturnType() != Void.class) { if (method.getParameterTypes().length == 0) { String name = method.getName(); if (name.indexOf('$') == -1) { if (name.length() > 3 && name.startsWith("get") && Character.isUpperCase(name.charAt(3))) { name = name.substring(3); } else if (name.length() > 2 && name.startsWith("is") && Character.isUpperCase(name.charAt(2)) && (method.getReturnType() == Boolean.class || method.getReturnType() == boolean.class)) { name = name.substring(2); } PropertyFetcher fetcher = new GetterPropertyFetcher(method, true); staticFetchers.put(name, fetcher); staticFetchers.put(StringUtils.uncapitalize(name), fetcher); } } } } }; List<Class<?>> allClasses = resolveAllClasses(clazz); for (Class<?> c : allClasses) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { try { fieldCallback.doWith(field); } catch (IllegalAccessException ex) { throw new IllegalStateException( "Shouldn't be illegal to access field '" + field.getName() + "': " + ex); } } Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { try { methodCallback.doWith(method); } catch (IllegalAccessException ex) { throw new IllegalStateException( "Shouldn't be illegal to access method '" + method.getName() + "': " + ex); } } } propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz); for (PropertyDescriptor desc : propertyDescriptors) { Method readMethod = desc.getReadMethod(); if (readMethod != null) { boolean staticReadMethod = Modifier.isStatic(readMethod.getModifiers()); if (staticReadMethod) { staticFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod)); } else { instanceFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod)); } } } }
From source file:com.gwtcx.server.servlet.FileUploadServlet.java
@SuppressWarnings("rawtypes") private Object createNestedEntity(Class entity, Field[] fields, String[] nextLine) { Log.debug("createNestedEntity()"); try {/*from w w w . jav a 2s. c o m*/ Object object = entity.newInstance(); for (Field field : fields) { Class type = field.getType(); // ignore Static fields if (Modifier.isStatic(field.getModifiers())) { continue; } if (type.getSimpleName().equals("String")) { Integer index = fieldNames.get(field.getName()); if (index != null) { field.set(object, nextLine[index].trim()); Log.debug("Field name: " + field.getName() + " index[" + index + "] = " + nextLine[index]); } } } return object; } catch (Exception e) { Log.error("Error encountered while creating nested entity", e); } return null; }
From source file:com.mstiles92.plugins.stileslib.config.ConfigObject.java
protected boolean doSkip(Field field) { return Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()) || Modifier.isProtected(field.getModifiers()) || Modifier.isPrivate(field.getModifiers()); }
From source file:io.mandrel.OtherTest.java
public void inspect(int level, Type clazz, String name) { if (level > 6) return;//w w w. j a v a 2s.c om if (clazz instanceof Class && !((Class<?>) clazz).isEnum() && ((Class<?>) clazz).getPackage() != null && ((Class<?>) clazz).getPackage().getName().startsWith("io.mandrel")) { int newLevel = level + 1; Field[] fields = ((Class<?>) clazz).getDeclaredFields(); for (Field field : fields) { Class<?> fieldType = field.getType(); String text; if (!field.isAnnotationPresent(JsonIgnore.class) && !Modifier.isStatic(field.getModifiers())) { if (List.class.equals(fieldType) || Map.class.equals(fieldType)) { Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Type paramType = pType.getActualTypeArguments()[pType.getActualTypeArguments().length - 1]; text = field.getName() + "(container of " + paramType + ")"; System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); inspect(newLevel, paramType, ""); } } else { text = field.getName() + (field.getType().isPrimitive() || field.getType().equals(String.class) || field.getType().equals(LocalDateTime.class) ? " (" + field.getType().getName() + ")" : ""); System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); inspect(newLevel, fieldType, field.getName()); } // JsonSubTypes subtype = // fieldType.getAnnotation(JsonSubTypes.class); // if (subtype != null) { // int subLevel = level + 2; // text = "subtypes:"; // System.err.println(StringUtils.leftPad(text, // text.length() + subLevel * 5, "\t- ")); // for (JsonSubTypes.Type type : subtype.value()) { // text = "subtype:" + type.name(); // System.err.println(StringUtils.leftPad(text, // text.length() + (subLevel + 1) * 5, "\t- ")); // inspect((subLevel + 1), type.value(), type.name()); // } // } } } JsonSubTypes subtype = ((Class<?>) clazz).getAnnotation(JsonSubTypes.class); if (subtype != null) { int subLevel = level + 1; String text = "subtypes:"; System.err.println(StringUtils.leftPad(text, text.length() + subLevel * 5, "\t- ")); for (JsonSubTypes.Type type : subtype.value()) { text = "subtype:" + type.name(); System.err.println(StringUtils.leftPad(text, text.length() + (subLevel + 1) * 5, "\t- ")); inspect((subLevel + 1), type.value(), type.name()); } } } }