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

The following examples show how to use java.lang.reflect.Field#getClass() . 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 File: XmlObjectSerializerServiceImpl.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * @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 2
Source File: InfluxDBResultMapper.java    From influxdb-java with 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 3
Source File: MemberName.java    From hottub with 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 4
Source File: TheSoapClass.java    From JSoap with 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 5
Source File: TheSoapClass.java    From JSoap with 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 6
Source File: MemberName.java    From jdk8u-dev-jdk with 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 File: MemberName.java    From jdk-1.7-annotated with 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 8
Source File: MemberName.java    From jdk8u-jdk with 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 9
Source File: MemberName.java    From jdk8u_jdk with 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 10
Source File: MemberName.java    From openjdk-8 with 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 11
Source File: MemberName.java    From openjdk-8-source with 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
Source File: MemberName.java    From jdk1.8-source-analysis with 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 13
Source File: MemberName.java    From Java8CN with 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 14
Source File: MemberName.java    From openjdk-jdk8u-backup with 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 15
Source File: MemberName.java    From openjdk-jdk8u with 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 16
Source File: MemberName.java    From JDKSourceCode1.8 with 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 17
Source File: MemberName.java    From jdk8u60 with 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 18
Source File: MemberName.java    From TencentKona-8 with 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 19
Source File: MemberName.java    From dragonwell8_jdk with 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 20
Source File: DataTypeDefinitionImpl.java    From fabric-chaincode-java with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @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);
        }
    }

}