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:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override/*  ww  w.  ja  v  a2 s. co  m*/
public <T extends IdentifiedObject> List<Long> findAllIdsByXPath(Long id1, Class<T> clazz) {
    try {
        String findAllIdsByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ALL_IDS_BY_XPATH_1")
                .get(String.class);
        Query query = em.createNamedQuery(findAllIdsByXPath).setParameter("o1Id", id1);
        return query.getResultList();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findAllIdsByXPath(Long id1) Exception: %s - %s\n", clazz.toString(),
                e.toString());
        throw new RuntimeException(e);
    }

}

From source file:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override/*from   w  w  w.  jav a 2  s.  c om*/
public <T extends IdentifiedObject> List<Long> findAllIdsByXPath(Long id1, Long id2, Class<T> clazz) {
    try {
        String findAllIdsByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ALL_IDS_BY_XPATH_2")
                .get(String.class);
        Query query = em.createNamedQuery(findAllIdsByXPath).setParameter("o1Id", id1).setParameter("o2Id",
                id2);
        return query.getResultList();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findAllIdsByXPath(Long id1, Long id2) Exception: %s - %s\n", clazz.toString(),
                e.toString());
        throw new RuntimeException(e);
    }
}

From source file:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override//  ww w. j av a 2 s.  c om
public <T extends IdentifiedObject> List<Long> findAllIdsByXPath(Long id1, Long id2, Long id3, Class<T> clazz) {
    try {
        String findAllIdsByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ALL_IDS_BY_XPATH_3")
                .get(String.class);
        Query query = em.createNamedQuery(findAllIdsByXPath).setParameter("o1Id", id1).setParameter("o2Id", id2)
                .setParameter("o3Id", id3);
        return query.getResultList();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findAllIdsByXPath(Long id1, Long id2, Long id3) Exception: %s - %s\n",
                clazz.toString(), e.toString());
        throw new RuntimeException(e);
    }
}

From source file:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override//from  w  w w  .ja  va2 s. c o  m
public <T extends IdentifiedObject> T findByResourceUri(String uri, Class<T> clazz) {
    try {
        String findByResourceURI = (String) clazz.getDeclaredField("QUERY_FIND_BY_RESOURCE_URI")
                .get(String.class);
        Query query = em.createNamedQuery(findByResourceURI).setParameter("uri", uri);
        return (T) query.getSingleResult();

    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findByResourceUri(String uri) Exception: %s - %s\n", clazz.toString(),
                e.toString());
        throw new RuntimeException(e);
    }
}

From source file:BrowserLauncher.java

/**
 * Called by a static initializer to load any classes, fields, and methods required at runtime
 * to locate the user's web browser.//  ww w  .  jav a 2s. c  om
 * @return <code>true</code> if all intialization succeeded
 *         <code>false</code> if any portion of the initialization failed
 */
private static boolean loadClasses() {
    switch (jvm) {
    case MRJ_2_0:
        try {
            Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
            Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
            Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
            Class aeClass = Class.forName("com.apple.MacOS.ae");
            aeDescClass = Class.forName("com.apple.MacOS.AEDesc");

            aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[] { int.class });
            appleEventConstructor = appleEventClass.getDeclaredConstructor(
                    new Class[] { int.class, int.class, aeTargetClass, int.class, int.class });
            aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class });

            makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class[] { String.class });
            putParameter = appleEventClass.getDeclaredMethod("putParameter",
                    new Class[] { int.class, aeDescClass });
            sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] {});

            Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
            keyDirectObject = (Integer) keyDirectObjectField.get(null);
            Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
            kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
            Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
            kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
        } catch (ClassNotFoundException cnfe) {
            errorMessage = cnfe.getMessage();
            return false;
        } catch (NoSuchMethodException nsme) {
            errorMessage = nsme.getMessage();
            return false;
        } catch (NoSuchFieldException nsfe) {
            errorMessage = nsfe.getMessage();
            return false;
        } catch (IllegalAccessException iae) {
            errorMessage = iae.getMessage();
            return false;
        }
        break;
    case MRJ_2_1:
        try {
            mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
            mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
            Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
            kSystemFolderType = systemFolderField.get(null);
            findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass });
            getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class });
            getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class });
        } catch (ClassNotFoundException cnfe) {
            errorMessage = cnfe.getMessage();
            return false;
        } catch (NoSuchFieldException nsfe) {
            errorMessage = nsfe.getMessage();
            return false;
        } catch (NoSuchMethodException nsme) {
            errorMessage = nsme.getMessage();
            return false;
        } catch (SecurityException se) {
            errorMessage = se.getMessage();
            return false;
        } catch (IllegalAccessException iae) {
            errorMessage = iae.getMessage();
            return false;
        }
        break;
    case MRJ_3_0:
        try {
            Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
            Constructor constructor = linker.getConstructor(new Class[] { Class.class });
            linkage = constructor.newInstance(new Object[] { BrowserLauncher.class });
        } catch (ClassNotFoundException cnfe) {
            errorMessage = cnfe.getMessage();
            return false;
        } catch (NoSuchMethodException nsme) {
            errorMessage = nsme.getMessage();
            return false;
        } catch (InvocationTargetException ite) {
            errorMessage = ite.getMessage();
            return false;
        } catch (InstantiationException ie) {
            errorMessage = ie.getMessage();
            return false;
        } catch (IllegalAccessException iae) {
            errorMessage = iae.getMessage();
            return false;
        }
        break;
    case MRJ_3_1:
        try {
            mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
            openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class });
        } catch (ClassNotFoundException cnfe) {
            errorMessage = cnfe.getMessage();
            return false;
        } catch (NoSuchMethodException nsme) {
            errorMessage = nsme.getMessage();
            return false;
        }
        break;
    default:
        break;
    }
    return true;
}

From source file:net.larry1123.elec.util.test.config.AbstractConfigTest.java

protected Field getField(Object ob, String fieldName, Class lastClass) {
    Class currentClass = lastClass == null ? ob.getClass() : lastClass.getSuperclass();
    try {/*from w  ww  . j  av  a  2  s.  c  om*/
        Field ret = currentClass.getDeclaredField(fieldName);
        ret.setAccessible(true);
        return ret;
    } catch (NoSuchFieldException e) {
        return getField(ob, fieldName, currentClass);
    } catch (NullPointerException e) {
        Assert.fail("Can't get field, " + fieldName + ", from ConfigBase!");
        return null;
    }
}

From source file:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override//from   www  .  j  ava  2 s.  co m
public <T extends IdentifiedObject> List<Long> findAllIdsByUsagePointId(Long usagePointId, Class<T> clazz) {
    try {
        String queryFindAllIdsByUsagePointId = (String) clazz
                .getDeclaredField("QUERY_FIND_ALL_IDS_BY_USAGE_POINT_ID").get(String.class);

        return em.createNamedQuery(queryFindAllIdsByUsagePointId).setParameter("usagePointId", usagePointId)
                .getResultList();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** FindAllIdsByUsagePointId(Long usagePointId) Exception: %s - %s\n",
                clazz.toString(), e.toString());
        throw new RuntimeException(e);
    }
}

From source file:com.spotify.helios.client.DefaultRequestDispatcher.java

private void setRequestMethod(final HttpURLConnection connection, final String method, final boolean isHttps) {
    // Nasty workaround for ancient HttpURLConnection only supporting few methods
    final Class<?> httpURLConnectionClass = connection.getClass();
    try {//from  ww  w .  j av  a2  s. c  om
        Field methodField;
        HttpURLConnection delegate;
        if (isHttps) {
            final Field delegateField = httpURLConnectionClass.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            delegate = (HttpURLConnection) delegateField.get(connection);
            methodField = delegate.getClass().getSuperclass().getSuperclass().getSuperclass()
                    .getDeclaredField("method");
        } else {
            delegate = connection;
            methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method");
        }

        methodField.setAccessible(true);
        methodField.set(delegate, method);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.utest.dao.AuditTrailInterceptor.java

@SuppressWarnings("unchecked")
private Field[] getAllFields(final Class objectClass) {

    final List<Field> fields = new ArrayList<Field>();
    for (final Method method : objectClass.getMethods()) {
        if (method.isAnnotationPresent(Audit.class)) {
            try {
                final Field methodField = objectClass.getDeclaredField(
                        method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4));
                if (methodField != null) {
                    fields.add(methodField);
                }//ww w .j a  va  2 s . c  o  m
            } catch (final Exception e) {
            }
        }
    }
    for (final Field field : objectClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(Audit.class)) {
            fields.add(field);
        }
    }

    return fields.toArray(new Field[fields.size()]);

}

From source file:com.yahoo.pulsar.broker.loadbalance.SimpleLoadManagerImplTest.java

private void setObjectField(Class objClass, Object objInstance, String fieldName, Object newValue)
        throws Exception {
    Field field = objClass.getDeclaredField(fieldName);
    field.setAccessible(true);/*w w  w.  j a va  2s. c o  m*/
    field.set(objInstance, newValue);
}