List of usage examples for java.lang.reflect Field getDeclaringClass
@Override
public Class<?> getDeclaringClass()
From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java
/** * Make the given field accessible, explicitly setting it accessible if * necessary. The {@code setAccessible(true)} method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active).//from ww w . j ava 2 s . co m * @param field the field to make accessible * @see java.lang.reflect.Field#setAccessible */ public static void makeAccessible(Field field) { if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) { field.setAccessible(true); } }
From source file:org.apache.axis2.jaxws.utility.XMLRootElementUtil.java
/** * The JAXBClass has a set of bean properties each represented by a PropertyDescriptor Each of * the fields of the class has an associated xml name. The method returns a map where the key is * the xml name and value is the PropertyDescriptor * * @param jaxbClass/*from w ww . j ava 2s . c o m*/ * @return map */ public static Map<String, PropertyDescriptorPlus> createPropertyDescriptorMap(Class jaxbClass) throws NoSuchFieldException, IntrospectionException { if (log.isDebugEnabled()) { log.debug("Get the PropertyDescriptor[] for " + jaxbClass); } PropertyDescriptor[] pds = Introspector.getBeanInfo(jaxbClass).getPropertyDescriptors(); Map<String, PropertyDescriptorPlus> map = new HashMap<String, PropertyDescriptorPlus>(); // Unfortunately the element names are stored on the fields. // Get all of the fields in the class and super classes List<Field> fields = getFields(jaxbClass); // Now match up the fields with the property descriptors...Sigh why didn't JAXB put the @XMLElement annotations on the // property methods! for (PropertyDescriptor pd : pds) { // Skip over the class property..it is never represented as an xml element if (pd.getName().equals("class")) { continue; } // For the current property, find a matching field...so that we can get the xml name boolean found = false; if (log.isDebugEnabled()) { log.debug(" Start: Find xmlname for property:" + pd.getName()); } for (Field field : fields) { String fieldName = field.getName(); // Use the name of the field and property to find the match if (fieldName.equalsIgnoreCase(pd.getDisplayName()) || fieldName.equalsIgnoreCase(pd.getName())) { // Get the xmlElement name for this field QName xmlName = getXmlElementRefOrElementQName(field.getDeclaringClass(), field); found = true; if (log.isDebugEnabled()) { log.debug(" Found field " + field.getName() + " which has xmlname=" + xmlName); } if (map.get(xmlName) != null) { if (log.isDebugEnabled()) { log.debug(" ALERT: property " + map.get(xmlName).getPropertyName() + " already has this same xmlName..this may cause problems."); } } map.put(xmlName.getLocalPart(), new PropertyDescriptorPlus(pd, xmlName)); break; } // Unfortunately, sometimes the field name is preceeded by an underscore if (fieldName.startsWith("_")) { fieldName = fieldName.substring(1); if (fieldName.equalsIgnoreCase(pd.getDisplayName()) || fieldName.equalsIgnoreCase(pd.getName())) { // Get the xmlElement name for this field QName xmlName = getXmlElementRefOrElementQName(field.getDeclaringClass(), field); found = true; if (log.isDebugEnabled()) { log.debug(" Found field " + field.getName() + " which has xmlname=" + xmlName); } if (map.get(xmlName) != null) { if (log.isDebugEnabled()) { log.debug(" ALERT: property " + map.get(xmlName).getPropertyName() + " already has this same xmlName..this may cause problems."); } } map.put(xmlName.getLocalPart(), new PropertyDescriptorPlus(pd, xmlName)); break; } } } // We didn't find a field. Default the xmlname to the property name if (!found) { String xmlName = pd.getName(); if (log.isDebugEnabled()) { log.debug(" A matching field was not found. Defaulting xmlname to " + xmlName); } if (map.get(xmlName) != null) { if (log.isDebugEnabled()) { log.debug(" ALERT: property " + map.get(xmlName).getPropertyName() + " already has this same xmlName..this may cause problems."); } } map.put(xmlName, new PropertyDescriptorPlus(pd, xmlName)); } if (log.isDebugEnabled()) { log.debug(" End: Find xmlname for property:" + pd.getName()); } } return map; }
From source file:com.yukthi.utils.beans.PropertyMapper.java
/** * Validates the field information specified in the "mapping" and adds the mapping information * to specified bean info/* w ww. ja va2 s .co m*/ * @param beanInfo Bean info to which mapping needs to be added * @param field Field on which mapping is found * @param mapping Mapping to be added */ private static void addMapping(BeanInfo beanInfo, Field field, PropertyMapping mapping) { NestedProperty externalProperty = null, localProperty = null; try { externalProperty = NestedProperty.getNestedProperty(mapping.type(), mapping.from()); String localPropertyName = field.getName(); //if local sub property is specified if (mapping.subproperty().length() > 0) { localPropertyName = localPropertyName + "." + mapping.subproperty(); } localProperty = NestedProperty.getNestedProperty(field.getDeclaringClass(), localPropertyName); } catch (Exception ex) { throw new InvalidConfigurationException(ex, "Invalid property mapping specified on field - {}.{}. " + "An error occurred while processing mapping properties.", field.getDeclaringClass().getName(), field.getName()); } //ensure target and source are of same types if (!CommonUtils.isAssignable(externalProperty.getType(), localProperty.getType())) { throw new InvalidConfigurationException( "Invalid property mapping specified on field - {}.{}. " + "Source property type and target property type are not matching", field.getDeclaringClass().getName(), field.getName()); } beanInfo.addCustomMapping(mapping.type(), new MappingInfo(externalProperty, localProperty)); }
From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java
private static AlchemyGenerator<?> determineGeneratorFor(Class<?> typeOfField, Field field, Map<Class<?>, AlchemyGenerator<?>> generatorMappings) { AlchemyGenerator<?> generator = generatorMappings.get(typeOfField); if (generator != null) { //Already found it return generator; }// www .j a v a 2 s.com if (isCollectionType(typeOfField)) { if (lacksGenericTypeArguments(field)) { LOG.warn("POJO {} contains a Collection field {} which is not type-parametrized. Cannot inject.", field.getDeclaringClass(), field); return null; } generator = determineGeneratorForCollectionField(field, typeOfField, generatorMappings); } else if (isEnumType(typeOfField)) { Object[] enumValues = typeOfField.getEnumConstants(); if (enumValues == null) { LOG.warn("Enum Class {} has no Enum Values: " + typeOfField); return null; } generator = () -> { int position = one(integers(0, enumValues.length)); return enumValues[position]; }; } else { //Assume Pojo and recurse generator = pojos(typeOfField); } return generator; }
From source file:org.apache.sqoop.model.ConfigUtils.java
public static Input getInputAnnotation(Field field, boolean strict) { Input annotation = field.getAnnotation(Input.class); if (strict && annotation == null) { throw new SqoopException(ModelError.MODEL_003, "Missing annotation Input on Field " + field.getName() + " on class " + field.getDeclaringClass().getName()); }/*from ww w .java 2 s. c o m*/ return annotation; }
From source file:org.apache.sqoop.model.ConfigUtils.java
public static Config getConfigAnnotation(Field field, boolean strict) { Config annotation = field.getAnnotation(Config.class); if (strict && annotation == null) { throw new SqoopException(ModelError.MODEL_003, "Missing annotation Config on Field " + field.getName() + " on class " + field.getDeclaringClass().getName()); }/* w w w .java 2 s .c om*/ return annotation; }
From source file:org.uimafit.factory.ConfigurationParameterFactory.java
/** * This method generates the default name of a configuration parameter that is defined by an * {@link org.uimafit.descriptor.ConfigurationParameter} annotation when no name is given *///from ww w. j av a 2 s. com public static String getConfigurationParameterName(Field field) { if (isConfigurationParameterField(field)) { org.uimafit.descriptor.ConfigurationParameter annotation = field .getAnnotation(org.uimafit.descriptor.ConfigurationParameter.class); String name = annotation.name(); if (name.equals(org.uimafit.descriptor.ConfigurationParameter.USE_FIELD_NAME)) { name = field.getDeclaringClass().getName() + "." + field.getName(); } return name; } return null; }
From source file:org.dbg4j.core.DebugUtils.java
/** * Method creates <code>Map<String, String></code> that contains instance field names * and their values. By default parameter value evaluates by {@link DefaultEvaluationAdapter} unless another * evaluator is set by {@link Adapter} annotation. * * @param instance/*from w ww . j a va2 s . co m*/ * @param fieldsForDebug * @return */ @Nonnull public static Map<String, String> getFieldValues(@Nonnull Object instance, @Nonnull Collection<Field> fieldsForDebug) { Map<String, String> result = new HashMap<String, String>(); for (Field field : fieldsForDebug) { String fieldValue = DefaultDebuggingAdapter.UNKNOWN_VALUE; try { EvaluationAdapter evaluationAdapter = null; if (field.isAnnotationPresent(Adapter.class)) { Adapter adapterAnnotation = field.getAnnotation(Adapter.class); evaluationAdapter = adapterAnnotation.value().newInstance(); } if (evaluationAdapter == null) { evaluationAdapter = new DefaultEvaluationAdapter(); } if (Modifier.isStatic(field.getModifiers())) { fieldValue = evaluationAdapter.evaluate(field.getDeclaringClass(), FieldUtils.readDeclaredStaticField(instance.getClass(), field.getName(), true)); } else { fieldValue = evaluationAdapter.evaluate(field.getDeclaringClass(), FieldUtils.readDeclaredField(instance, field.getName(), true)); } } catch (Exception ignored) { } result.put(field.getName(), fieldValue); } return result; }
From source file:adalid.core.XS1.java
static void logFieldAnnotationErrorMessage(Field field, Class<?> annotation, String string) { String name = field.getName(); Class<?> type = field.getDeclaringClass(); logFieldAnnotationErrorMessage(name, type, annotation, string); }
From source file:Main.java
public static List<Field> getAccessibleFields(Class<?> clazz, Class<?> limit) { Package topPackage = clazz.getPackage(); List<Field> fieldList = new ArrayList<Field>(); int topPackageHash = topPackage == null ? 0 : topPackage.hashCode(); boolean top = true; do {// w ww . j a v a 2 s . co m if (clazz == null) { break; } Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { if (top == true) { // add all top declared fields fieldList.add(field); continue; } int modifier = field.getModifiers(); if (Modifier.isPrivate(modifier) == true) { continue; // ignore super private fields } if (Modifier.isPublic(modifier) == true) { addFieldIfNotExist(fieldList, field); // add super public methods continue; } if (Modifier.isProtected(modifier) == true) { addFieldIfNotExist(fieldList, field); // add super protected methods continue; } // add super default methods from the same package Package pckg = field.getDeclaringClass().getPackage(); int pckgHash = pckg == null ? 0 : pckg.hashCode(); if (pckgHash == topPackageHash) { addFieldIfNotExist(fieldList, field); } } top = false; } while ((clazz = clazz.getSuperclass()) != limit); return fieldList; }