List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:pl.maciejwalkowiak.plist.FieldSerializer.java
private String serializeField(Object o, Field field) { String result = ""; int modifiers = field.getModifiers(); if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) { ReflectionUtils.makeAccessible(field); if (!field.isAnnotationPresent(PlistIgnore.class)) { result = processField(field, o).toString(); } else {// w ww. j av a2 s . c om logger.debug("field {} is ignored", field.getName()); } } return result; }
From source file:code.elix_x.excore.utils.nbt.mbt.encoders.NBTClassEncoder.java
@Override public NBTTagCompound toNBT(MBT mbt, Object t) { NBTTagCompound nbt = new NBTTagCompound(); Class clazz = t.getClass();//from w w w . ja va 2 s .c o m if (encodeClass) nbt.setString(CLASS, clazz.getName()); 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 ((encodeFinal || !Modifier.isFinal(field.getModifiers())) && (encodeStatic || !Modifier.isStatic(field.getModifiers()))) { try { nbt.setTag(field.getName(), mbt.toNBT(field.get(t))); } catch (IllegalArgumentException e) { Throwables.propagate(e); } catch (IllegalAccessException e) { Throwables.propagate(e); } } } } } clazz = encodeSuper ? clazz.getSuperclass() : Object.class; } return nbt; }
From source file:com.sonatype.security.ldap.api.DeepEqualsBuilder.java
private static void reflectionAppend(Object lhs, Object rhs, Class clazz, EqualsBuilder builder, boolean useTransients, String[] excludeFields) { while (clazz.getSuperclass() != null) { Field[] fields = clazz.getDeclaredFields(); List excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length && builder.isEquals(); i++) { Field f = fields[i];/*from w w w .j a v a 2 s . c om*/ if (!excludedFieldList.contains(f.getName()) && (f.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(f.getModifiers())) && (!Modifier.isStatic(f.getModifiers()))) { try { Object lhsChild = f.get(lhs); Object rhsChild = f.get(rhs); Class testClass = getTestClass(lhsChild, rhsChild); boolean hasEqualsMethod = classHasEqualsMethod(testClass); if (testClass != null && !hasEqualsMethod) { reflectionAppend(lhsChild, rhsChild, testClass, builder, useTransients, excludeFields); } else { builder.append(lhsChild, rhsChild); } } catch (IllegalAccessException e) { // this can't happen. Would get a Security exception instead // throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException"); } } } // now for the parent clazz = clazz.getSuperclass(); reflectionAppend(lhs, rhs, clazz, builder, useTransients, excludeFields); } }
From source file:com.github.braully.web.DescriptorHtmlEntity.java
private void addHtmlListElement(Field field) { if (elementsList == null) { elementsList = new ArrayList<>(); }/*w w w . j av a2 s. c o m*/ final int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !hiddenListProperties.contains(field.getName())) { elementsList.add(buildDescriptorHtmlEntity(field)); } }
From source file:edu.brown.utils.ClassUtil.java
/** * @param clazz//from www . j a v a 2 s. c o m * @return */ public static <T> Field[] getFieldsByType(Class<?> clazz, Class<? extends T> fieldType) { List<Field> fields = new ArrayList<Field>(); for (Field f : clazz.getDeclaredFields()) { int modifiers = f.getModifiers(); if (Modifier.isTransient(modifiers) == false && Modifier.isPublic(modifiers) == true && Modifier.isStatic(modifiers) == false && ClassUtil.getSuperClasses(f.getType()).contains(fieldType)) { fields.add(f); } } // FOR return (fields.toArray(new Field[fields.size()])); }
From source file:org.apache.drill.exec.store.dfs.FormatPluginOptionsDescriptor.java
/** * Uses reflection to extract options based on the fields of the provided config class * ("List extensions" field is ignored, pending removal, Char is turned into String) * The class must be annotated with {@code @JsonTypeName("type name")} * @param pluginConfigClass the config class we want to extract options from through reflection *//*w w w . j a va 2 s . c o m*/ FormatPluginOptionsDescriptor(Class<? extends FormatPluginConfig> pluginConfigClass) { this.pluginConfigClass = pluginConfigClass; Map<String, TableParamDef> paramsByName = new LinkedHashMap<>(); Field[] fields = pluginConfigClass.getDeclaredFields(); // @JsonTypeName("text") JsonTypeName annotation = pluginConfigClass.getAnnotation(JsonTypeName.class); this.typeName = annotation != null ? annotation.value() : null; if (this.typeName != null) { paramsByName.put("type", new TableParamDef("type", String.class)); } for (Field field : fields) { if (Modifier.isStatic(field.getModifiers()) // we want to deprecate this field || (field.getName().equals("extensions") && field.getType() == List.class)) { continue; } Class<?> fieldType = field.getType(); if (fieldType == char.class) { // calcite does not like char type. Just use String and enforce later that length == 1 fieldType = String.class; } paramsByName.put(field.getName(), new TableParamDef(field.getName(), fieldType).optional()); } this.functionParamsByName = unmodifiableMap(paramsByName); }
From source file:de.codesourcery.eve.skills.util.SpringBeanInjector.java
private void doInjectDependencies(Object obj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class<?> currentClasz = obj.getClass(); do {/*from w ww. java 2 s. c o m*/ // inject fields for (Field f : currentClasz.getDeclaredFields()) { final int m = f.getModifiers(); if (Modifier.isStatic(m) || Modifier.isFinal(m)) { continue; } final Resource annot = f.getAnnotation(Resource.class); if (annot != null) { if (!f.isAccessible()) { f.setAccessible(true); } if (log.isTraceEnabled()) { log.trace("doInjectDependencies(): Setting field " + f.getName() + " with bean '" + annot.name() + "'"); } f.set(obj, getBean(annot.name())); } } // inject methods for (Method method : currentClasz.getDeclaredMethods()) { final int m = method.getModifiers(); if (Modifier.isStatic(m) || Modifier.isAbstract(m)) { continue; } if (method.getParameterTypes().length != 1) { continue; } if (!method.getName().startsWith("set")) { continue; } final Resource annot = method.getAnnotation(Resource.class); if (annot != null) { if (!method.isAccessible()) { method.setAccessible(true); } if (log.isTraceEnabled()) { log.trace("doInjectDependencies(): Invoking setter method " + method.getName() + " with bean '" + annot.name() + "'"); } method.invoke(obj, getBean(annot.name())); } } currentClasz = currentClasz.getSuperclass(); } while (currentClasz != null); }
From source file:edu.usu.sdl.openstorefront.doc.EntityProcessor.java
private static void addFields(Class entity, EntityDocModel docModel) { if (entity != null) { Field[] fields = entity.getDeclaredFields(); for (Field field : fields) { //Skip static field if ((Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) == false) { EntityFieldModel fieldModel = new EntityFieldModel(); fieldModel.setName(field.getName()); fieldModel.setType(field.getType().getSimpleName()); fieldModel.setOriginClass(entity.getSimpleName()); fieldModel.setEmbeddedType(ReflectionUtil.isComplexClass(field.getType())); if (ReflectionUtil.isCollectionClass(field.getType())) { DataType dataType = (DataType) field.getAnnotation(DataType.class); if (dataType != null) { String typeClass = dataType.value().getSimpleName(); if (StringUtils.isNotBlank(dataType.actualClassName())) { typeClass = dataType.actualClassName(); }/*from w w w . j a v a2 s.c o m*/ fieldModel.setGenericType(typeClass); } } APIDescription description = (APIDescription) field.getAnnotation(APIDescription.class); if (description != null) { fieldModel.setDescription(description.value()); } for (Annotation annotation : field.getAnnotations()) { if (annotation.annotationType().getName().equals(APIDescription.class.getName()) == false) { EntityConstraintModel entityConstraintModel = new EntityConstraintModel(); entityConstraintModel.setName(annotation.annotationType().getSimpleName()); APIDescription annotationDescription = (APIDescription) annotation.annotationType() .getAnnotation(APIDescription.class); if (annotationDescription != null) { entityConstraintModel.setDescription(annotationDescription.value()); } //rules Object annObj = field.getAnnotation(annotation.annotationType()); if (annObj instanceof NotNull) { entityConstraintModel.setRules("Field is required"); } else if (annObj instanceof PK) { PK pk = (PK) annObj; entityConstraintModel.setRules("<b>Generated:</b> " + pk.generated()); fieldModel.setPrimaryKey(true); } else if (annObj instanceof FK) { FK fk = (FK) annObj; StringBuilder sb = new StringBuilder(); sb.append("<b>Foreign Key:</b> ").append(fk.value().getSimpleName()); sb.append(" (<b>Enforce</b>: ").append(fk.enforce()); sb.append(" <b>Soft reference</b>: ").append(fk.softReference()); if (StringUtils.isNotBlank(fk.referencedField())) { sb.append(" <b>Reference Field</b>: ").append(fk.referencedField()); } sb.append(" )"); entityConstraintModel.setRules(sb.toString()); } else if (annObj instanceof ConsumeField) { entityConstraintModel.setRules(""); } else if (annObj instanceof Size) { Size size = (Size) annObj; entityConstraintModel .setRules("<b>Min:</b> " + size.min() + " <b>Max:</b> " + size.max()); } else if (annObj instanceof Pattern) { Pattern pattern = (Pattern) annObj; entityConstraintModel.setRules("<b>Pattern:</b> " + pattern.regexp()); } else if (annObj instanceof Sanitize) { Sanitize sanitize = (Sanitize) annObj; entityConstraintModel .setRules("<b>Sanitize:</b> " + sanitize.value().getSimpleName()); } else if (annObj instanceof Unique) { Unique unique = (Unique) annObj; entityConstraintModel.setRules("<b>Handler:</b> " + unique.value().getSimpleName()); } else if (annObj instanceof ValidValueType) { ValidValueType validValueType = (ValidValueType) annObj; StringBuilder sb = new StringBuilder(); if (validValueType.value().length > 0) { sb.append(" <b>Values:</b> ").append(Arrays.toString(validValueType.value())); } if (validValueType.lookupClass().length > 0) { sb.append(" <b>Lookups:</b> "); for (Class lookupClass : validValueType.lookupClass()) { sb.append(lookupClass.getSimpleName()).append(" "); } } if (validValueType.enumClass().length > 0) { sb.append(" <b>Enumerations:</b> "); for (Class enumClass : validValueType.enumClass()) { sb.append(enumClass.getSimpleName()).append(" ("); sb.append(Arrays.toString(enumClass.getEnumConstants())).append(")"); } } entityConstraintModel.setRules(sb.toString()); } else if (annObj instanceof Min) { Min min = (Min) annObj; entityConstraintModel.setRules("<b>Min value:</b> " + min.value()); } else if (annObj instanceof Max) { Max max = (Max) annObj; entityConstraintModel.setRules("<b>Max value:</b> " + max.value()); } else if (annObj instanceof Version) { entityConstraintModel.setRules("Entity version; For Multi-Version control"); } else if (annObj instanceof DataType) { DataType dataType = (DataType) annObj; String typeClass = dataType.value().getSimpleName(); if (StringUtils.isNotBlank(dataType.actualClassName())) { typeClass = dataType.actualClassName(); } entityConstraintModel.setRules("<b>Type:</b> " + typeClass); } else { entityConstraintModel.setRules(annotation.toString()); } //Annotations that have related classes if (annObj instanceof DataType) { DataType dataType = (DataType) annObj; entityConstraintModel.getRelatedClasses().add(dataType.value().getSimpleName()); } if (annObj instanceof FK) { FK fk = (FK) annObj; entityConstraintModel.getRelatedClasses().add(fk.value().getSimpleName()); } if (annObj instanceof ValidValueType) { ValidValueType validValueType = (ValidValueType) annObj; for (Class lookupClass : validValueType.lookupClass()) { entityConstraintModel.getRelatedClasses().add(lookupClass.getSimpleName()); } StringBuilder sb = new StringBuilder(); for (Class enumClass : validValueType.enumClass()) { sb.append("<br>"); sb.append(enumClass.getSimpleName()).append(": ("); sb.append(Arrays.toString(enumClass.getEnumConstants())).append(")"); } entityConstraintModel .setRules(entityConstraintModel.getRules() + " " + sb.toString()); } fieldModel.getConstraints().add(entityConstraintModel); } } docModel.getFieldModels().add(fieldModel); } } addFields(entity.getSuperclass(), docModel); } }
From source file:cn.lambdalib.s11n.SerializationHelper.java
private List<Field> buildExposedFields(Class<?> type) { return FieldUtils.getAllFieldsList(type).stream().filter(f -> { Class<?> declaringClass = f.getDeclaringClass(); SerializeStrategy anno = declaringClass.getAnnotation(SerializeStrategy.class); ExposeStrategy strategy = anno == null ? ExposeStrategy.PUBLIC : anno.strategy(); boolean serializeAll = anno == null ? false : anno.all(); if (f.isAnnotationPresent(SerializeIncluded.class)) { return true; } else if (f.isAnnotationPresent(SerializeExcluded.class)) { return false; } else {// w w w.j a v a 2s .c o m if (!serializeAll && !isS11nType(f.getType())) { return false; } else { int mod = f.getModifiers(); switch (strategy) { case PUBLIC: return Modifier.isPublic(mod) && !Modifier.isStatic(mod) && !Modifier.isFinal(mod); case ALL: return !Modifier.isStatic(mod) && !Modifier.isFinal(mod); default: return false; } } } }).map(f -> { f.setAccessible(true); return f; }).collect(Collectors.toList()); }