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:ch.zhaw.iamp.rct.ui.GrammarWindow.java

/**
 * This has to be done this way because there is a bug in the SliderUI. See:
 * http://stackoverflow.com/questions/4460840/remove-value-displaying-over-thumb-in-jslider
 *//*from  w  w  w. ja  v  a2  s  . c  om*/
private void hideSliderValue() {
    try {
        Class<?> sliderUIClass = Class.forName("javax.swing.plaf.synth.SynthSliderUI");
        final Field paintValue = sliderUIClass.getDeclaredField("paintValue");
        paintValue.setAccessible(true);
        paintValue.set(zoomSlider.getUI(), false);
    } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | NoSuchFieldException
            | SecurityException ex) {
        System.out.println("Could not hide the slide value: " + ex.getMessage());
    }
}

From source file:net.minecraftforge.fml.common.FMLModContainer.java

private void parseSimpleFieldAnnotation(SetMultimap<String, ASMData> annotations, String annotationClassName,
        Function<ModContainer, Object> retriever) throws IllegalAccessException {
    String[] annName = annotationClassName.split("\\.");
    String annotationName = annName[annName.length - 1];
    for (ASMData targets : annotations.get(annotationClassName)) {
        String targetMod = (String) targets.getAnnotationInfo().get("value");
        Field f = null;//from   w  ww. j a  v  a 2  s . com
        Object injectedMod = null;
        ModContainer mc = this;
        boolean isStatic = false;
        Class<?> clz = modInstance.getClass();
        if (!Strings.isNullOrEmpty(targetMod)) {
            if (Loader.isModLoaded(targetMod)) {
                mc = Loader.instance().getIndexedModList().get(targetMod);
            } else {
                mc = null;
            }
        }
        if (mc != null) {
            try {
                clz = Class.forName(targets.getClassName(), true, Loader.instance().getModClassLoader());
                f = clz.getDeclaredField(targets.getObjectName());
                f.setAccessible(true);
                isStatic = Modifier.isStatic(f.getModifiers());
                injectedMod = retriever.apply(mc);
            } catch (Exception e) {
                Throwables.propagateIfPossible(e);
                FMLLog.log(getModId(), Level.WARN, e, "Attempting to load @%s in class %s for %s and failing",
                        annotationName, targets.getClassName(), mc.getModId());
            }
        }
        if (f != null) {
            Object target = null;
            if (!isStatic) {
                target = modInstance;
                if (!modInstance.getClass().equals(clz)) {
                    FMLLog.log(getModId(), Level.WARN,
                            "Unable to inject @%s in non-static field %s.%s for %s as it is NOT the primary mod instance",
                            annotationName, targets.getClassName(), targets.getObjectName(), mc.getModId());
                    continue;
                }
            }
            f.set(target, injectedMod);
        }
    }
}

From source file:ca.sqlpower.object.PersistedSPObjectTest.java

/**
 * All persistable {@link SPObject} implementations must define a static
 * final field which is a list defining the absolute ordering of that
 * class's child type classes. This method ensures that list is retrievable
 * by reflection from the object, that the field is public, static, and
 * final, and that it is nonempty for classes that allow children and empty
 * for classes that do not allow children.
 *///from w w  w .j  ava2 s.  c  om
@SuppressWarnings("unchecked")
public void testAllowedChildTypesField() throws Exception {
    Class<? extends SPObject> classUnderTest = getSPObjectUnderTest().getClass();
    Field childOrderField;
    try {
        childOrderField = classUnderTest.getDeclaredField("allowedChildTypes");
    } catch (NoSuchFieldException ex) {
        fail("Persistent " + classUnderTest + " must have a static final field called allowedChildTypes");
        throw new AssertionError(); // NOTREACHED
    }

    assertEquals("The allowedChildTypes field must be final", true,
            Modifier.isFinal(childOrderField.getModifiers()));

    assertEquals("The allowedChildTypes field must be static", true,
            Modifier.isStatic(childOrderField.getModifiers()));

    // Note: in the future, we will change this to require that the field is private
    assertEquals("The allowedChildTypes field must be public", true,
            Modifier.isPublic(childOrderField.getModifiers()));

    List<Class<? extends SPObject>> allowedChildTypes = (List<Class<? extends SPObject>>) childOrderField
            .get(null);
    if (getSPObjectUnderTest().allowsChildren()) {
        assertFalse(allowedChildTypes.isEmpty());
    } else {
        assertTrue(allowedChildTypes.isEmpty());
    }
}

From source file:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java

private <T> Field findPropertyFieldExactUncached(Class<T> classType, String propName) {
    for (Field field : classType.getDeclaredFields()) {
        XmlElement xmlElement = field.getAnnotation(XmlElement.class);
        if (xmlElement != null && xmlElement.name().equals(propName)) {
            return field;
        }/*  ww w .ja v a  2  s  .  c  o m*/
        XmlAttribute xmlAttribute = field.getAnnotation(XmlAttribute.class);
        if (xmlAttribute != null && xmlAttribute.name().equals(propName)) {
            return field;
        }
    }
    try {
        return classType.getDeclaredField(propName);
    } catch (NoSuchFieldException e) {
        // nothing found
    }
    Class<? super T> superclass = classType.getSuperclass();
    if (superclass == null || Object.class.equals(superclass)) {
        return null;
    }
    return findPropertyField(superclass, propName);
}

From source file:com.atlassian.jira.functest.framework.AdministrationImpl.java

private Object reflectivelyGetField(final Object self, final Class<?> clazz, final String fieldName) {
    Preconditions.checkNotNull(self, "cannot get field %s on null object", fieldName);
    Field field = null;//from   w w w. j a v a  2 s .  c  o m
    try {
        field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.get(self);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(String.format(
                "Error getting field '%s' for %s : possibly a security manager has prevented access to this field",
                fieldName, clazz.getName()), e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(String.format(
                "Error getting field '%s' for %s : possibly a library update has changed this field", fieldName,
                clazz.getName()), e);
    } finally {
        if (field != null) {
            field.setAccessible(false);
        }
    }
}

From source file:com.canappi.connector.yp.yhere.BakeryView.java

public void viewDidLoad() {

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        Set<String> keys = extras.keySet();
        for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
            String key = iter.next();
            Class c = SearchView.class;
            try {
                Field f = c.getDeclaredField(key);
                Object extra = extras.get(key);
                String value = extra.toString();
                f.set(this, extras.getString(value));
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();/*ww w.ja v  a  2  s  .c om*/
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {

    }

    bakeryViewIds = new HashMap();
    bakeryViewValues = new HashMap();

    isUserDefault = false;

    resultsListView = (ListView) findViewById(R.id.resultsTable);
    resultsAdapter = new ResultsEfficientAdapter(this);

    businessNameArray = new ArrayList<String>();

    latitudeArray = new ArrayList<String>();

    longitudeArray = new ArrayList<String>();

    listingIdArray = new ArrayList<String>();

    phoneArray = new ArrayList<String>();

    callArray = new ArrayList<String>();

    streetArray = new ArrayList<String>();

    cityArray = new ArrayList<String>();
    resultsListView.setAdapter(resultsAdapter);
    didSelectViewController();

}

From source file:com.canappi.connector.yp.yhere.GameView.java

public void viewDidLoad() {

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        Set<String> keys = extras.keySet();
        for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
            String key = iter.next();
            Class c = SearchView.class;
            try {
                Field f = c.getDeclaredField(key);
                Object extra = extras.get(key);
                String value = extra.toString();
                f.set(this, extras.getString(value));
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//from  ww w .j  a v  a  2  s.c  o m
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {

    }

    gameViewIds = new HashMap();
    gameViewValues = new HashMap();

    isUserDefault = false;

    resultsListView = (ListView) findViewById(R.id.resultsTable);
    resultsAdapter = new ResultsEfficientAdapter(this);

    businessNameArray = new ArrayList<String>();

    latitudeArray = new ArrayList<String>();

    longitudeArray = new ArrayList<String>();

    listingIdArray = new ArrayList<String>();

    phoneArray = new ArrayList<String>();

    callArray = new ArrayList<String>();

    streetArray = new ArrayList<String>();

    cityArray = new ArrayList<String>();
    resultsListView.setAdapter(resultsAdapter);
    didSelectViewController();

}

From source file:com.canappi.connector.yp.yhere.GasStationView.java

public void viewDidLoad() {

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        Set<String> keys = extras.keySet();
        for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
            String key = iter.next();
            Class c = SearchView.class;
            try {
                Field f = c.getDeclaredField(key);
                Object extra = extras.get(key);
                String value = extra.toString();
                f.set(this, extras.getString(value));
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();/*w  w  w  .j a va 2  s . c om*/
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {

    }

    gasStationViewIds = new HashMap();
    gasStationViewValues = new HashMap();

    isUserDefault = false;

    resultsListView = (ListView) findViewById(R.id.resultsTable);
    resultsAdapter = new ResultsEfficientAdapter(this);

    businessNameArray = new ArrayList<String>();

    latitudeArray = new ArrayList<String>();

    longitudeArray = new ArrayList<String>();

    listingIdArray = new ArrayList<String>();

    phoneArray = new ArrayList<String>();

    callArray = new ArrayList<String>();

    streetArray = new ArrayList<String>();

    cityArray = new ArrayList<String>();
    resultsListView.setAdapter(resultsAdapter);
    didSelectViewController();

}

From source file:com.canappi.connector.yp.yhere.GroceryView.java

public void viewDidLoad() {

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        Set<String> keys = extras.keySet();
        for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
            String key = iter.next();
            Class c = SearchView.class;
            try {
                Field f = c.getDeclaredField(key);
                Object extra = extras.get(key);
                String value = extra.toString();
                f.set(this, extras.getString(value));
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//from   w ww.j a  v  a2 s . co  m
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {

    }

    groceryViewIds = new HashMap();
    groceryViewValues = new HashMap();

    isUserDefault = false;

    resultsListView = (ListView) findViewById(R.id.resultsTable);
    resultsAdapter = new ResultsEfficientAdapter(this);

    businessNameArray = new ArrayList<String>();

    latitudeArray = new ArrayList<String>();

    longitudeArray = new ArrayList<String>();

    listingIdArray = new ArrayList<String>();

    phoneArray = new ArrayList<String>();

    callArray = new ArrayList<String>();

    streetArray = new ArrayList<String>();

    cityArray = new ArrayList<String>();
    resultsListView.setAdapter(resultsAdapter);
    didSelectViewController();

}

From source file:com.canappi.connector.yp.yhere.LubeView.java

public void viewDidLoad() {

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        Set<String> keys = extras.keySet();
        for (Iterator<String> iter = keys.iterator(); iter.hasNext();) {
            String key = iter.next();
            Class c = SearchView.class;
            try {
                Field f = c.getDeclaredField(key);
                Object extra = extras.get(key);
                String value = extra.toString();
                f.set(this, extras.getString(value));
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//ww w  .j a va 2  s  .  co m
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {

    }

    lubeViewIds = new HashMap();
    lubeViewValues = new HashMap();

    isUserDefault = false;

    resultsListView = (ListView) findViewById(R.id.resultsTable);
    resultsAdapter = new ResultsEfficientAdapter(this);

    businessNameArray = new ArrayList<String>();

    latitudeArray = new ArrayList<String>();

    longitudeArray = new ArrayList<String>();

    listingIdArray = new ArrayList<String>();

    phoneArray = new ArrayList<String>();

    callArray = new ArrayList<String>();

    streetArray = new ArrayList<String>();

    cityArray = new ArrayList<String>();
    resultsListView.setAdapter(resultsAdapter);
    didSelectViewController();

}