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.web.server.util.ClassLoaderUtil.java

public static CopyOnWriteArrayList closeClassLoader(ClassLoader cl) {
    CopyOnWriteArrayList jars = new CopyOnWriteArrayList();
    boolean res = false;
    Class classURLClassLoader = null;
    if (cl instanceof URLClassLoader)
        classURLClassLoader = URLClassLoader.class;
    else if (cl instanceof VFSClassLoader) {
        classURLClassLoader = VFSClassLoader.class;
    }// w w  w.  ja va 2  s .c o m
    Field f = null;
    try {
        f = classURLClassLoader.getDeclaredField("ucp");
        System.out.println(f);
    } catch (NoSuchFieldException e1) {
        // e1.printStackTrace();
        // log.info(e1.getMessage(), e1);

    }
    if (f != null) {
        f.setAccessible(true);
        Object obj = null;
        try {
            obj = f.get(cl);
        } catch (IllegalAccessException e1) {
            // e1.printStackTrace();
            // log.info(e1.getMessage(), e1);
        }
        if (obj != null) {
            final Object ucp = obj;
            f = null;
            try {
                f = ucp.getClass().getDeclaredField("loaders");
                System.out.println(f);
            } catch (NoSuchFieldException e1) {
                // e1.printStackTrace();
                // log.info(e1.getMessage(), e1);
            }
            if (f != null) {
                f.setAccessible(true);
                ArrayList loaders = null;
                try {
                    loaders = (ArrayList) f.get(ucp);
                    res = true;
                } catch (IllegalAccessException e1) {
                    // e1.printStackTrace();
                }
                for (int i = 0; loaders != null && i < loaders.size(); i++) {
                    obj = loaders.get(i);
                    f = null;
                    try {
                        f = obj.getClass().getDeclaredField("jar");
                        // log.info(f);
                    } catch (NoSuchFieldException e) {
                        // e.printStackTrace();
                        // log.info(e.getMessage(), e);
                    }
                    if (f != null) {
                        f.setAccessible(true);
                        try {
                            obj = f.get(obj);
                        } catch (IllegalAccessException e1) {
                            // e1.printStackTrace();
                            // log.info(e1.getMessage(), e1);
                        }
                        if (obj instanceof JarFile) {
                            final JarFile jarFile = (JarFile) obj;
                            System.out.println(jarFile.getName());
                            jars.add(jarFile.getName().replace("/", "\\").trim().toUpperCase());
                            // try {
                            // jarFile.getManifest().clear();
                            // } catch (IOException e) {
                            // // ignore
                            // }
                            try {
                                jarFile.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                                // ignore
                                // log.info(e.getMessage(), e);
                            }
                        }
                    }
                }
            }
        }
    }
    return jars;
}

From source file:com.github.gekoh.yagen.util.MappingUtils.java

public static boolean fkIsNullable(Field field) {
    if (field.isAnnotationPresent(ManyToOne.class) && !field.getAnnotation(ManyToOne.class).optional()) {
        return false;
    }//www .ja v a2s . c o m
    if (field.isAnnotationPresent(JoinColumn.class) && !field.getAnnotation(JoinColumn.class).nullable()) {
        return false;
    }
    if (field.isAnnotationPresent(OneToMany.class)) {
        String mappedBy = field.getAnnotation(OneToMany.class).mappedBy();
        if (StringUtils.isNotEmpty(mappedBy)) {
            try {
                Class targetEntity = MappingUtils.determineTargetEntity(field);
                Field fkField = targetEntity.getDeclaredField(mappedBy);
                if (fkField.isAnnotationPresent(JoinColumn.class)
                        && !fkField.getAnnotation(JoinColumn.class).nullable()) {
                    return false;
                }
            } catch (NoSuchFieldException e) {
                LOG.error("unable to get mappedBy field", e);
            }
        }
    }
    return true;
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Field getDeclaredField(Class<?> clazz, String field)
        throws SecurityException, NoSuchFieldException {
    if (clazz == Object.class) {
        throw new NoSuchFieldException(
                "Field `" + field + "` in class `" + clazz.getName() + "` cannot be found.");
    }//from   w ww.ja v a2  s.co m
    try {
        return clazz.getDeclaredField(field);
    } catch (SecurityException e) {
        throw e;
    } catch (NoSuchFieldException e) {
        return getDeclaredField(clazz.getSuperclass(), field);
    }
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

public static Object getField(Class<?> clazz, Object obj, String fieldName)
        throws NoSuchFieldException, IllegalArgumentException {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);/*from   w  w w  . j ava 2  s.com*/

    try {
        return field.get(obj);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:net.kamhon.ieagle.util.ReflectionUtil.java

/**
 * different with class.getDeclaredField(does not include the field in super class). this one include the field in
 * super class also./*from  www .jav  a 2 s .c  o m*/
 * 
 * @return
 * @throws NoSuchFieldException
 */
public static Field getDeclaredField(Class<?> clazz, String fieldName) throws NoSuchFieldException {

    Field field = null;
    while (clazz != null && field == null) {
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (Exception e) {
        }
        clazz = clazz.getSuperclass();
    }
    if (clazz == null && field == null)
        throw new NoSuchFieldException(clazz + ". Field name=" + fieldName);
    return field;
}

From source file:com.ocs.dynamo.utils.ClassUtils.java

/**
 * Returns a field with a certain name from a class
 * /*  w  w  w  .j av  a 2  s. c o  m*/
 * @param clazz
 *            the class
 * @param fieldName
 *            the name of the filed
 * @return
 */
public static Field getField(Class<?> clazz, String fieldName) {
    Field field = null;
    if (clazz != null) {
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            LOG.debug(e.getMessage(), e);
            if (clazz.getSuperclass() != null) {
                return getField(clazz.getSuperclass(), fieldName);
            }
        }
    }
    return field;
}

From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java

private static boolean is64bitSWT() throws Exception {
    Class<Library> swtLibraryClass = Library.class;
    Field is64 = swtLibraryClass.getDeclaredField("IS_64");
    is64.setAccessible(true);//from  www. ja v  a2s.c  o m
    return is64.getBoolean(swtLibraryClass);
}

From source file:com.github.reinert.jjschema.HyperSchemaGeneratorV4.java

private static <T> ObjectNode transformJsonToHyperSchema(Class<T> type, ObjectNode jsonSchema) {
    ObjectNode properties = (ObjectNode) jsonSchema.get("properties");
    Iterator<String> namesIterator = properties.fieldNames();

    while (namesIterator.hasNext()) {
        String prop = namesIterator.next();
        try {/*from w w w  .j  a v  a 2s.  c om*/
            Field field = type.getDeclaredField(prop);
            com.github.reinert.jjschema.Media media = field
                    .getAnnotation(com.github.reinert.jjschema.Media.class);
            if (media != null) {
                ObjectNode hyperProp = (ObjectNode) properties.get(prop);
                hyperProp.put(MEDIA_TYPE, media.type());
                hyperProp.put(BINARY_ENCODING, media.binaryEncoding());
                if (hyperProp.get("type").isArray()) {
                    TextNode typeString = new TextNode("string");
                    ((ArrayNode) hyperProp.get("type")).set(0, typeString);
                } else {
                    hyperProp.put("type", "string");
                }
                properties.put(prop, hyperProp);
            }
        } catch (NoSuchFieldException e) {
            //e.printStackTrace();
        } catch (SecurityException e) {
            //e.printStackTrace();
        }
    }
    return jsonSchema;
}

From source file:com.vmware.photon.controller.common.dcp.ServiceHostUtils.java

/**
 * Returns the self link of the service.
 *
 * @param serviceLinkFieldName//from  w w  w. j a v  a  2s .  com
 * @param service
 * @return
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
public static String getServiceSelfLink(String serviceLinkFieldName, Class service)
        throws NoSuchFieldException, IllegalAccessException {
    Field serviceLinkField = service.getDeclaredField(serviceLinkFieldName);
    return (String) serviceLinkField.get(null);
}

From source file:com.ms.commons.summer.web.util.json.JsonBeanUtils.java

private static boolean isTransientField(String name, Class beanClass) {
    try {/*from w  w w  .  ja  va2 s. c  o  m*/
        Field field = beanClass.getDeclaredField(name);
        return (field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT;
    } catch (Exception e) {
        // swallow exception
    }
    return false;
}