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

@Override
public <T extends IdentifiedObject> Long findIdByXPath(Long id1, Class<T> clazz) {
    try {//from   w  ww  . j  a  v a2 s .co m
        String findIdByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ID_BY_XPATH").get(String.class);
        Query query = em.createNamedQuery(findIdByXPath).setParameter("o1Id", id1);
        return (Long) query.getSingleResult();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findIdByXPath(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   www .j ava2s  .  c  om
public <T extends IdentifiedObject> List<Long> findAllIds(Class<T> clazz) {
    try {
        String queryFindById = (String) clazz.getDeclaredField("QUERY_FIND_ALL_IDS").get(String.class);

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

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

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

From source file:org.mybatisorm.annotation.handler.JoinHandler.java

private Field getField(Class<?> clazz, String fieldName) {
    Field field;//from   ww w .ja va  2s. co  m
    try {
        field = clazz.getDeclaredField(fieldName);
    } catch (Exception e) {
        throw new InvalidJoinException(
                "The class " + clazz.getSimpleName() + " has no '" + fieldName + "' property.");
    }
    return field;
}

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

@Override
public <T extends IdentifiedObject> Long findIdByXPath(Long id1, Long id2, Long id3, Class<T> clazz) {
    try {/*w w  w  . j  av a  2  s .c om*/
        String findIdByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ID_BY_XPATH").get(String.class);
        Query query = em.createNamedQuery(findIdByXPath).setParameter("o1Id", id1).setParameter("o2Id", id2)
                .setParameter("o3Id", id3);
        return (Long) query.getSingleResult();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findIdByXPath(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

@Override
public <T extends IdentifiedObject> Long findIdByXPath(Long id1, Long id2, Long id3, Long id4, Class<T> clazz) {
    try {/*from www  . j  a  v  a2 s . c om*/
        String findIdByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ID_BY_XPATH").get(String.class);
        Query query = em.createNamedQuery(findIdByXPath).setParameter("o1Id", id1).setParameter("o2Id", id2)
                .setParameter("o3Id", id3).setParameter("o4Id", id4);
        return (Long) query.getSingleResult();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findIdByXPath(Long id1, Long id2, Long id3, Long id4) 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.  j a v a2 s  .co m
public <T> T findByUUID(UUID uuid, Class<T> clazz) {
    try {
        String queryFindById = (String) clazz.getDeclaredField("QUERY_FIND_BY_UUID").get(String.class);

        return (T) em.createNamedQuery(queryFindById).setParameter("uuid", uuid.toString().toUpperCase())
                .getSingleResult();
    } catch (IllegalAccessException | NoSuchFieldException e) {
        System.out.printf("**** findByUUID(UUID uuid) 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 ww.  j  ava2  s . c o m
public <T extends IdentifiedObject> T findById(Long id, Class<T> clazz) {
    try {
        String queryFindById = (String) clazz.getDeclaredField("QUERY_FIND_BY_ID").get(String.class);

        return (T) em.createNamedQuery(queryFindById).setParameter("id", id).getSingleResult();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** FindbyId(Long id) Exception: %s - %s id: %s\n", clazz.toString(), e.toString(),
                id);
        throw new RuntimeException(e);
    }

}

From source file:ClassReader.java

protected final Field resolveField(int i) throws IOException, ClassNotFoundException, NoSuchFieldException {
    int oldPos = pos;
    try {/*from   ww w . j a  v a 2  s. c o  m*/
        Field f = (Field) cpool[i];
        if (f == null) {
            pos = cpoolIndex[i];
            Class owner = resolveClass(readShort());
            NameAndType nt = resolveNameAndType(readShort());
            cpool[i] = owner.getDeclaredField(nt.name);
            f = owner.getDeclaredField(nt.name);
        }
        return f;
    } finally {
        pos = oldPos;
    }
}

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

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

}