下面列出了java.lang.reflect.Field#getClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
<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;
}
/**
* @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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
}
@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");
}
}
}
@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;
}
}
}
/**
*
* @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);
}
}
}