Java Code Examples for java.lang.reflect.Field#getClass()

The following examples show how to use java.lang.reflect.Field#getClass() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: influxdb-java   File: InfluxDBResultMapper.java    License: MIT License 6 votes vote down vote up
<T> boolean fieldValueModified(final Class<?> fieldType, final Field field, final T object, final Object value,
                               final TimeUnit precision)
  throws IllegalArgumentException, IllegalAccessException {
  if (String.class.isAssignableFrom(fieldType)) {
    field.set(object, String.valueOf(value));
    return true;
  }
  if (Instant.class.isAssignableFrom(fieldType)) {
    Instant instant;
    if (value instanceof String) {
      instant = Instant.from(RFC3339_FORMATTER.parse(String.valueOf(value)));
    } else if (value instanceof Long) {
      instant = Instant.ofEpochMilli(toMillis((long) value, precision));
    } else if (value instanceof Double) {
      instant = Instant.ofEpochMilli(toMillis(((Double) value).longValue(), precision));
    } else if (value instanceof Integer) {
      instant = Instant.ofEpochMilli(toMillis(((Integer) value).longValue(), precision));
    } else {
      throw new InfluxDBMapperException("Unsupported type " + field.getClass() + " for field " + field.getName());
    }
    field.set(object, instant);
    return true;
  }
  return false;
}
 
Example 2
/**
 * @see com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider#visitSerializableFields(java.lang.Object, com.thoughtworks.xstream.converters.reflection.ReflectionProvider.Visitor)
 */
@Override
public void visitSerializableFields(Object object, Visitor visitor) {
    for (Iterator iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) {
        Field field = (Field) iterator.next();
        if (!fieldModifiersSupported(field)) {
            continue;
        }
        validateFieldAccess(field);
        if (ignoreField(field)) {
            continue;
        }
        Object value = null;
        try {
            value = field.get(object);
            if (value != null && lda.isProxied(value)) {
                value = lda.resolveProxy(value);
            }
        } catch (Exception e) {
            throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName() + " on " + object, e);
        }
        visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);
    }
}
 
Example 3
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 4
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 5
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 6
Source Project: jdk8u60   File: MemberName.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 7
Source Project: JDKSourceCode1.8   File: MemberName.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 8
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 9
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 10
Source Project: Java8CN   File: MemberName.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 11
Source Project: hottub   File: MemberName.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 12
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 13
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 14
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 15
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 16
Source Project: jdk-1.7-annotated   File: MemberName.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 17
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
    fld.getClass();  // NPE check
    // fill in vmtarget, vmindex while we have fld in hand:
    MethodHandleNatives.init(this, fld);
    assert(isResolved() && this.clazz != null);
    this.name = fld.getName();
    this.type = fld.getType();
    assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
    byte refKind = this.getReferenceKind();
    assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
    if (makeSetter) {
        changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
    }
}
 
Example 18
Source Project: JSoap   File: TheSoapClass.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void getAttributeInfo(int index, AttributeInfo attributeInfo) {

    Field f = attributes_map.get(index);

    if (f.getType().equals(String.class))
        attributeInfo.type = AttributeInfo.STRING_CLASS;
    else
        attributeInfo.type = f.getClass();

    String prpname = f.getAnnotation(JSoapAttribute.class).name();
    if (prpname.equals("JSOAP_DEFAULT_ATTRIBUTE_NAME"))
        attributeInfo.name = f.getName();
    else
        attributeInfo.name = f.getAnnotation(JSoapAttribute.class).name();

    String prpns = f.getAnnotation(JSoapAttribute.class).namespace();
    if (prpns.equals("JSOAP_DEFAULT_ATTRIBUTE_NAMESPACE")) {
        if (this.getClass().getAnnotation(JSoapClass.class) != null) {
            String ns = this.getClass().getAnnotation(JSoapClass.class).namespace();
            if (ns==null)
                Log.e("JSoap", "Missing namespace in field " + f.getName() + " in class " + this.getClass() + ". Either declare it at the field SoapRequestAttribute annotation or at the class SoapRequestClass annotation");
            else
                attributeInfo.namespace = ns;
        }
        else {
            Log.e("JSoap", "Missing namespace in field " + f.getName() + " in class " + this.getClass() + ". Either declare it at the field SoapRequestAttribute annotation or at the class SoapRequestClass annotation");
        }
    }

}
 
Example 19
Source Project: JSoap   File: TheSoapClass.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
    if (fields_map != null) {
        Field f = fields_map.get(i);
        if (f.getType().equals(String.class))
            propertyInfo.type = PropertyInfo.STRING_CLASS;
        else
            propertyInfo.type = f.getClass();

        String prpname = f.getAnnotation(JSoapReqField.class).fieldName();
        if (prpname.equals("JSOAP_DEFAULT_FIELDNAME"))
            propertyInfo.name = f.getName();
        else
            propertyInfo.name = f.getAnnotation(JSoapReqField.class).fieldName();


        String prpns = f.getAnnotation(JSoapReqField.class).namespace();
        if (prpns.equals("JSOAP_DEFAULT_NAMESPACE")) {
            if (this.getClass().getAnnotation(JSoapClass.class) != null) {
                String ns = this.getClass().getAnnotation(JSoapClass.class).namespace();
                if (ns==null)
                    Log.e("JSoap", "Missing namespace in field " + f.getName() + " in class " + this.getClass() + ". Either declare it at the field SoapRequestElement annotation or at the class SoapRequestClass annotation");
                else
                    propertyInfo.namespace = ns;
            }
            else {
                Log.e("JSoap", "Missing namespace in field " + f.getName() + " in class " + this.getClass() + ". Either declare it at the field SoapRequestElement annotation or at the class SoapRequestClass annotation");
            }
        }
        else {
            propertyInfo.namespace = prpns;
        }
    }
}
 
Example 20
/**
 *
 * @param componentClass
 */
public DataTypeDefinitionImpl(final Class<?> componentClass) {
    this.clazz = componentClass;
    this.name = componentClass.getName();
    this.simpleName = componentClass.getSimpleName();
    // given this class extract the property elements
    final Field[] fields = componentClass.getDeclaredFields();

    for (final Field f : fields) {
        final Property propAnnotation = f.getAnnotation(Property.class);
        if (propAnnotation != null) {
            final TypeSchema ts = TypeSchema.typeConvert(f.getType());

            // array of strings, "a","b","c","d" to become map of {a:b}, {c:d}
            final String[] userSupplied = propAnnotation.schema();
            for (int i = 0; i < userSupplied.length; i += 2) {
                final String userKey = userSupplied[i];
                Object userValue;
                switch (userKey.toLowerCase()) {
                case "title":
                case "pattern":
                    userValue = userSupplied[i + 1];
                    break;
                case "uniqueitems":
                    userValue = Boolean.parseBoolean(userSupplied[i + 1]);
                    break;
                case "required":
                case "enum":
                    userValue = Stream.of(userSupplied[i + 1].split(",")).map(String::trim).toArray(String[]::new);
                    break;
                default:
                    userValue = Integer.parseInt(userSupplied[i + 1]);
                    break;
                }
                ts.put(userKey, userValue);
            }

            final PropertyDefinition propDef = new PropertyDefinitionImpl(f.getName(), f.getClass(), ts, f);
            this.properties.put(f.getName(), propDef);
        }
    }

}