Example usage for java.lang Class getDeclaredField

List of usage examples for java.lang Class getDeclaredField

Introduction

In this page you can find the example usage for java.lang Class getDeclaredField.

Prototype

@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Usage

From source file:com.datatorrent.contrib.enrich.POJOEnricher.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private PojoUtils.Getter generateGettersForField(Class<?> klass, String inputFieldName)
        throws NoSuchFieldException, SecurityException {
    Field f = klass.getDeclaredField(inputFieldName);
    Class c = ClassUtils.primitiveToWrapper(f.getType());
    return PojoUtils.createGetter(klass, inputFieldName, c);
}

From source file:com.datatorrent.contrib.enrich.POJOEnricher.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private PojoUtils.Setter generateSettersForField(Class<?> klass, String outputFieldName)
        throws NoSuchFieldException, SecurityException {
    Field f = klass.getDeclaredField(outputFieldName);
    Class c = ClassUtils.primitiveToWrapper(f.getType());
    return PojoUtils.createSetter(klass, outputFieldName, c);
}

From source file:org.jsonschema2pojo.integration.config.IncludeAccessorsIT.java

@Test
public void beansIncludeGettersAndSettersByDefault()
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException {

    ClassLoader resultsClassLoader = schemaRule
            .generateAndCompile("/schema/properties/primitiveProperties.json", "com.example");

    Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties");

    // throws NoSuchMethodException if method is not found
    generatedType.getDeclaredMethod("getA");
    generatedType.getDeclaredMethod("setA", Integer.class);
    assertThat(generatedType.getDeclaredField("a").getModifiers(), is(Modifier.PRIVATE));
}

From source file:com.datatorrent.contrib.avro.PojoToAvro.java

/**
 * This method generates the getters for provided field of a given class
 *
 * @return Getter/* w  w w  .  j  a  v a  2s  . c  o  m*/
 */
private Getter<?, ?> generateGettersForField(Class<?> cls, String inputFieldName)
        throws NoSuchFieldException, SecurityException {
    java.lang.reflect.Field f = cls.getDeclaredField(inputFieldName);
    Class<?> c = ClassUtils.primitiveToWrapper(f.getType());

    Getter<?, ?> classGetter = PojoUtils.createGetter(cls, inputFieldName, c);

    return classGetter;
}

From source file:org.atemsource.atem.impl.hibernate.PropertyDescriptor.java

public static PropertyDescriptor createInstance(final Class clazz, String propertyName) {
    PropertyDescriptor propertyDescriptor = new PropertyDescriptor();
    try {/*from w w  w.  j  a  v a2  s  . c  o m*/
        propertyDescriptor.declaringClass = clazz;
        Class currentClass = clazz;
        // this is probably necessary because the field is named exactly like the getter
        if (propertyName.startsWith("is")) {
            propertyName = propertyName.substring(2, 3).toLowerCase() + propertyName.substring(3);
        } else if (propertyName.startsWith("get")) {
            propertyName = propertyName.substring(3, 4).toLowerCase() + propertyName.substring(4);
        }
        java.beans.PropertyDescriptor propertyDescriptor2 = BeanUtils.getPropertyDescriptor(clazz,
                propertyName);
        if (propertyDescriptor2 != null) {
            propertyDescriptor.readMethod = propertyDescriptor2.getReadMethod();
            propertyDescriptor.writeMethod = propertyDescriptor2.getWriteMethod();

        } else {
            PropertyDescriptor propertyDescriptor3 = new PropertyDescriptor();
            while (currentClass != null) {
                try {
                    propertyDescriptor3.field = clazz.getDeclaredField(propertyName);
                    propertyDescriptor3.propertyName = propertyName;
                    propertyDescriptor3.declaringClass = currentClass;
                    return propertyDescriptor3;
                } catch (NoSuchFieldException e) {
                }
                currentClass = currentClass.getSuperclass();
            }
        }

        while (currentClass != null) {
            try {
                propertyDescriptor.field = clazz.getDeclaredField(propertyName);
            } catch (NoSuchFieldException e) {
            }
            currentClass = currentClass.getSuperclass();
        }
    } catch (SecurityException e) {
        return null;
    }
    propertyDescriptor.propertyName = propertyName;
    return propertyDescriptor;
}

From source file:de.javakaffee.web.msm.serializer.jackson.JacksonTranscoderTest.java

private Field getField(final Class<?> clazz, final String name) throws NoSuchFieldException {
    final Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);/*from w  w w. j  a  v  a  2 s.c om*/
    return field;
}

From source file:cn.homecredit.web.listener.MyOsgiHost.java

@Override
protected void addSpringOSGiSupport() {
    // see the javadoc for org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext for more details
    // OsgiBundleXmlWebApplicationContext expects the the BundleContext to be set in the ServletContext under the attribute
    // OsgiBundleXmlWebApplicationContext.BUNDLE_CONTEXT_ATTRIBUTE
    try {//w  w w.  j  ava2s  .co  m
        Class clazz = Class
                .forName("org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext");
        String key = (String) clazz.getDeclaredField("BUNDLE_CONTEXT_ATTRIBUTE").get(null);
        servletContext.setAttribute(key, felix.getBundleContext());
    } catch (ClassNotFoundException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Spring OSGi support is not enabled");
        }
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(
                    "The API of Spring OSGi has changed and the field [#0] is no longer available. The OSGi plugin needs to be updated",
                    e,
                    "org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext.BUNDLE_CONTEXT_ATTRIBUTE");
        }
    }
}

From source file:com.microsoft.rest.ServiceResponseBuilder.java

/**
 * Register a destination type for errors with models.
 *
 * @param type the type to deserialize.// www  .j  ava 2  s.  com
 * @return the same builder instance.
 */
public ServiceResponseBuilder<T, E> registerError(final Class<? extends RestException> type) {
    this.exceptionType = type;
    try {
        Field f = type.getDeclaredField("body");
        this.responseTypes.put(0, f.getType());
    } catch (NoSuchFieldException e) {
        // AutoRestException always has a body. Register Object as a fallback plan.
        this.responseTypes.put(0, Object.class);
    }
    return this;
}

From source file:org.jsonschema2pojo.integration.JavaNameIT.java

@Test
public void propertiesHaveCorrectTypes() throws IllegalAccessException, InstantiationException,
        ClassNotFoundException, NoSuchFieldException, IntrospectionException {

    ClassLoader javaNameClassLoader = schemaRule.generateAndCompile("/schema/javaName/javaName.json",
            "com.example.javaname");
    Class<?> classWithJavaNames = javaNameClassLoader.loadClass("com.example.javaname.JavaName");
    Object instance = classWithJavaNames.newInstance();

    assertThat(classWithJavaNames.getDeclaredField("javaEnum").getType(),
            typeCompatibleWith(javaNameClassLoader.loadClass("com.example.javaname.JavaName$JavaEnum")));
    assertThat(classWithJavaNames.getDeclaredField("enumWithoutJavaName").getType(), typeCompatibleWith(
            javaNameClassLoader.loadClass("com.example.javaname.JavaName$EnumWithoutJavaName")));
    assertThat(classWithJavaNames.getDeclaredField("javaObject").getType(),
            typeCompatibleWith(javaNameClassLoader.loadClass("com.example.javaname.JavaObject")));
    assertThat(classWithJavaNames.getDeclaredField("objectWithoutJavaName").getType(),
            typeCompatibleWith(javaNameClassLoader.loadClass("com.example.javaname.ObjectWithoutJavaName")));

}

From source file:org.kuali.coeus.s2sgen.impl.generate.support.stylesheet.StylesheetValidationTest.java

private SchemaType getSchemaType(Class<? extends S2SFormGenerator> generatorClass) {
    final Class<?> returnType;
    try {//from  w  w  w .ja v  a2s  .co m
        returnType = generatorClass
                .getDeclaredMethod("getFormObject", ProposalDevelopmentDocumentContract.class).getReturnType();
        return (SchemaType) returnType.getDeclaredField("type").get(null);
    } catch (NoSuchMethodException | IllegalAccessException | NoSuchFieldException e) {
        throw new RuntimeException(generatorClass.getName() + " cannot find SchemaType", e);
    }
}