List of usage examples for java.lang.reflect Field toString
public String toString()
From source file:org.broadinstitute.sting.commandline.ArgumentTypeDescriptor.java
/** * Return the component type of a field, or String.class if the type cannot be found. * @param field The reflected field to inspect. * @return The parameterized component type, or String.class if the parameterized type could not be found. * @throws IllegalArgumentException If more than one parameterized type is found on the field. *///from w w w . j a v a 2 s .c om @Override protected Type getCollectionComponentType(Field field) { // Multiplex arguments must resolve to maps from which the clp should extract the second type. if (field.getGenericType() instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); if (parameterizedType.getActualTypeArguments().length != 2) throw new IllegalArgumentException( "Unable to determine collection type of field: " + field.toString()); return (Class) parameterizedType.getActualTypeArguments()[1]; } else return String.class; }
From source file:org.broadinstitute.sting.commandline.ArgumentTypeDescriptor.java
/** * Return the component type of a field, or String.class if the type cannot be found. * @param field The reflected field to inspect. * @return The parameterized component type, or String.class if the parameterized type could not be found. * @throws IllegalArgumentException If more than one parameterized type is found on the field. *//*from w ww. j a v a2 s . c o m*/ @Override protected Type getCollectionComponentType(Field field) { // If this is a parameterized collection, find the contained type. If blow up if more than one type exists. if (field.getGenericType() instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); if (parameterizedType.getActualTypeArguments().length > 1) throw new IllegalArgumentException( "Unable to determine collection type of field: " + field.toString()); return parameterizedType.getActualTypeArguments()[0]; } else return String.class; }
From source file:org.apache.camel.dataformat.bindy.BindyKeyValuePairFactory.java
public void initAnnotatedFields() { for (Class<?> cl : models) { List<Field> linkFields = new ArrayList<Field>(); for (Field field : cl.getDeclaredFields()) { KeyValuePairField keyValuePairField = field.getAnnotation(KeyValuePairField.class); if (keyValuePairField != null) { if (LOG.isDebugEnabled()) { LOG.debug("Key declared in the class : " + cl.getName() + ", key : " + keyValuePairField.tag() + ", Field : " + keyValuePairField.toString()); }//from ww w . ja v a2 s . c o m keyValuePairFields.put(keyValuePairField.tag(), keyValuePairField); annotedFields.put(keyValuePairField.tag(), field); } Link linkField = field.getAnnotation(Link.class); if (linkField != null) { if (LOG.isDebugEnabled()) { LOG.debug("Class linked : " + cl.getName() + ", Field" + field.toString()); } linkFields.add(field); } } if (!linkFields.isEmpty()) { annotatedLinkFields.put(cl.getName(), linkFields); } } }
From source file:org.apache.camel.dataformat.bindy.BindyCsvFactory.java
public void initAnnotatedFields() { for (Class<?> cl : models) { List<Field> linkFields = new ArrayList<Field>(); if (LOG.isDebugEnabled()) { LOG.debug("Class retrieved : " + cl.getName()); }// w w w . java 2s .c o m for (Field field : cl.getDeclaredFields()) { DataField dataField = field.getAnnotation(DataField.class); if (dataField != null) { if (LOG.isDebugEnabled()) { LOG.debug("Position defined in the class : " + cl.getName() + ", position : " + dataField.pos() + ", Field : " + dataField.toString()); } if (dataField.required()) { ++numberMandatoryFields; } else { ++numberOptionalFields; } dataFields.put(dataField.pos(), dataField); annotedFields.put(dataField.pos(), field); } Link linkField = field.getAnnotation(Link.class); if (linkField != null) { if (LOG.isDebugEnabled()) { LOG.debug("Class linked : " + cl.getName() + ", Field" + field.toString()); } linkFields.add(field); } } if (!linkFields.isEmpty()) { annotatedLinkFields.put(cl.getName(), linkFields); } totalFields = numberMandatoryFields + numberOptionalFields; if (LOG.isDebugEnabled()) { LOG.debug("Number of optional fields : " + numberOptionalFields); LOG.debug("Number of mandatory fields : " + numberMandatoryFields); LOG.debug("Total : " + totalFields); } } }
From source file:net.camelpe.extension.cdi.spi.CamelInjectionTargetWrapper.java
private void injectEndpointInto(final T instance, final Field camelInjectAnnotatedField) throws ResolutionException { final Object valueToInject; if (camelInjectAnnotatedField.isAnnotationPresent(org.apache.camel.EndpointInject.class)) { final org.apache.camel.EndpointInject endpointInjectAnnotation = camelInjectAnnotatedField .getAnnotation(org.apache.camel.EndpointInject.class); valueToInject = getCamelPostProcessorHelper().getInjectionValue(camelInjectAnnotatedField.getType(), endpointInjectAnnotation.uri(), endpointInjectAnnotation.ref(), camelInjectAnnotatedField.getName(), instance, instance.getClass().getName()); } else if (camelInjectAnnotatedField.isAnnotationPresent(org.apache.camel.Produce.class)) { final org.apache.camel.Produce produceAnnotation = camelInjectAnnotatedField .getAnnotation(org.apache.camel.Produce.class); valueToInject = getCamelPostProcessorHelper().getInjectionValue(camelInjectAnnotatedField.getType(), produceAnnotation.uri(), produceAnnotation.ref(), camelInjectAnnotatedField.getName(), instance, instance.getClass().getName()); } else {//ww w . j a va 2 s . com throw new IllegalStateException("Neither [" + org.apache.camel.EndpointInject.class.getName() + "] nor [" + org.apache.camel.Produce.class + "] are present on field [" + camelInjectAnnotatedField.toString() + "]"); } setField(instance, camelInjectAnnotatedField, valueToInject); }
From source file:it.grid.storm.config.Configuration.java
@Override public String toString() { StringBuilder configurationStringBuilder = new StringBuilder(); try {/* w w w. j a va2 s. c o m*/ // This class methods Method methods[] = Configuration.instance.getClass().getDeclaredMethods(); // This class fields Field[] fields = Configuration.instance.getClass().getDeclaredFields(); HashMap<String, String> methodKeyMap = new HashMap<String, String>(); for (Field field : fields) { String fieldName = field.getName(); if (fieldName.endsWith("KEY") && field.getType().equals(String.class)) { // from a field like GROUP_TAPE_WRITE_BUFFER_KEY = // "tape.buffer.group.write" // puts in the map the pair // <getgrouptapewritebuffer,tape.buffer.group.write> String mapKey = "get" + fieldName.substring(0, fieldName.lastIndexOf("_")).replaceAll("_", "").toLowerCase(); if (methodKeyMap.containsKey(mapKey)) { String value = methodKeyMap.get(mapKey); methodKeyMap.put(mapKey, value + " , " + (String) field.get(Configuration.instance)); } else { methodKeyMap.put(mapKey, (String) field.get(Configuration.instance)); } } } Object field = null; Object[] dummyArray = new Object[0]; for (Method method : methods) { /* * with method.getModifiers() == 1 we check that the method is public * (otherwise he can request real parameters) */ if (method.getName().substring(0, 3).equals("get") && (!method.getName().equals("getInstance")) && method.getModifiers() == 1) { field = method.invoke(Configuration.instance, dummyArray); if (field.getClass().isArray()) { field = ArrayUtils.toString(field); } String value = methodKeyMap.get(method.getName().toLowerCase()); if (value == null) { configurationStringBuilder.insert(0, "!! Unable to find method " + method.getName() + " in methode key map!"); } else { configurationStringBuilder.append("Property " + value + " : "); } if (field.getClass().equals(String.class)) { field = '\'' + ((String) field) + '\''; } configurationStringBuilder.append(method.getName() + "() == " + field.toString() + "\n"); } } return configurationStringBuilder.toString(); } catch (Exception e) { if (e.getClass().isAssignableFrom(java.lang.reflect.InvocationTargetException.class)) { configurationStringBuilder.insert(0, "!!! Cannot do toString! Got an Exception: " + e.getCause() + "\n"); } else { configurationStringBuilder.insert(0, "!!! Cannot do toString! Got an Exception: " + e + "\n"); } return configurationStringBuilder.toString(); } }
From source file:at.alladin.rmbt.shared.hstoreparser.HstoreParser.java
/** * //from ww w . ja va 2s. com * @param hstore * @return * @throws HstoreParseException */ @SuppressWarnings({ "unchecked", "rawtypes" }) public T fromJson(JSONObject json) throws HstoreParseException { T object; try { object = constructor.newInstance(); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new HstoreParseException(HstoreParseException.HSTORE_PARSE_EXCEPTION + e.getLocalizedMessage(), e); } if (object == null) { throw new HstoreParseException( HstoreParseException.HSTORE_COULD_NOT_INSTANTIATE + clazz.getCanonicalName()); } //iterate through all json entries: Iterator<String> jsonKeys = json.keys(); while (jsonKeys.hasNext()) { String key = jsonKeys.next(); //if fieldsWithKeys contain a key (=it was annotated with @HstoreKey) then try to set the value of the field in T object if (fieldsWithKeys.containsKey(key)) { final Field field = fieldsWithKeys.get(key); try { Object value = getFromJsonByField(json, key, field); field.setAccessible(true); //cast / new instance needed? if (field.isAnnotationPresent(HstoreCollection.class)) { Class<?> fieldClazz = field.getType(); //System.out.println("FieldClazz: " + fieldClazz + " -> " + fieldClazz.isAssignableFrom(Collection.class)); if (Collection.class.isAssignableFrom(fieldClazz)) { //get collection and instantiate if necessary Collection collection = (Collection) field.get(object); if (collection == null) { collection = (Collection) fieldClazz.newInstance(); } ParameterizedType fieldType = (ParameterizedType) field.getGenericType(); Class<?> genericClazz = (Class<?>) fieldType.getActualTypeArguments()[0]; //System.out.println("genericClazz: " + genericClazz); //Object jsonObject = hstore.toJson((String) value, genericClazz); Object jsonObject = null; //System.out.println(value); if (!value.equals(JSONObject.NULL)) { if (value instanceof JSONArray || value instanceof JSONObject) { jsonObject = hstore.toJson(value.toString(), genericClazz); } else { jsonObject = hstore.toJson((String) value, genericClazz); } } else { jsonObject = hstore.toJson(null, genericClazz); } if (jsonObject != null) { if (jsonObject instanceof JSONArray) { //System.out.println(jsonObject); Object[] array = hstore.fromJSONArray((JSONArray) jsonObject, genericClazz); for (int i = 0; i < array.length; i++) { //System.out.println("Created element: " + array[i]); collection.add(array[i]); } } else { Object element = hstore.fromString((String) value, genericClazz); //System.out.println("Created element: " + element); collection.add(element); } } value = collection; } else { throw new HstoreParseException(HstoreParseException.HSTORE_MUST_BE_A_COLLECTION + field + " - " + clazz.getCanonicalName()); } } if (field.isAnnotationPresent(HstoreCast.class)) { HstoreCast castDef = field.getAnnotation(HstoreCast.class); if (castDef.simpleCast()) { //simple cast field.set(object, castDef.clazz().cast(value)); } else { //new instance field.set(object, castDef.clazz().getConstructor(castDef.constructorParamClazz()) .newInstance(value)); } } else { if (JSONObject.NULL.equals(value)) { field.set(object, null); } else { field.set(object, parseFieldValue(field, value)); } } } catch (IllegalAccessException | IllegalArgumentException | JSONException e) { throw new HstoreParseException( HstoreParseException.HSTORE_COULD_NOT_INSTANTIATE + clazz.getCanonicalName(), e); } catch (InstantiationException e) { throw new HstoreParseException( HstoreParseException.HSTORE_COULD_NOT_INSTANTIATE + clazz.getCanonicalName(), e); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassCastException e) { System.out.println("field: " + field.toString() + ", key: " + key); e.printStackTrace(); } } } return object; }