List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:MyJavaP.java
/** * Format the fields and methods of one class, given its name. *//*from w w w.j a v a 2 s . c o m*/ protected void doClass(String className) { try { Class c = Class.forName(className); System.out.println(Modifier.toString(c.getModifiers()) + ' ' + c + " {"); int mods; Field fields[] = c.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (!Modifier.isPrivate(fields[i].getModifiers()) && !Modifier.isProtected(fields[i].getModifiers())) System.out.println("\t" + fields[i]); } Constructor[] constructors = c.getConstructors(); for (int j = 0; j < constructors.length; j++) { Constructor constructor = constructors[j]; System.out.println("\t" + constructor); } Method methods[] = c.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { if (!Modifier.isPrivate(methods[i].getModifiers()) && !Modifier.isProtected(methods[i].getModifiers())) System.out.println("\t" + methods[i]); } System.out.println("}"); } catch (ClassNotFoundException e) { System.err.println("Error: Class " + className + " not found!"); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void print_class(Class c) { if (c.isInterface()) { System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c)); } else if (c.getSuperclass() != null) { System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends " + typename(c.getSuperclass())); } else {// w ww . ja v a2 s .co m System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c)); } Class[] interfaces = c.getInterfaces(); if ((interfaces != null) && (interfaces.length > 0)) { if (c.isInterface()) System.out.print(" extends "); else System.out.print(" implements "); for (int i = 0; i < interfaces.length; i++) { if (i > 0) System.out.print(", "); System.out.print(typename(interfaces[i])); } } System.out.println(" {"); Constructor[] constructors = c.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) print_method_or_constructor(constructors[i]); Field[] fields = c.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { print_field(fields[i]); } Method[] methods = c.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) print_method_or_constructor(methods[i]); System.out.println("}"); }
From source file:natalia.dymnikova.cluster.scheduler.impl.RolesChecker.java
private boolean doCheck(final Set<String> roles, final Class<? extends Remote> aClass) { final Field[] declaredFields = aClass.getDeclaredFields(); final List<TypeFilter> fields = Arrays.stream(declaredFields) .filter(field -> field.isAnnotationPresent(Autowired.class)) // TODO deal with 3pp dependencies .filter(field -> field.getType().getPackage().getName().startsWith(basePackage)).map(Field::getType) .map(AssignableTypeFilter::new).collect(toList()); final MultiValueMap<String, Object> matchAll = new LinkedMultiValueMap<>(); matchAll.put("value", singletonList(roles.toArray(new String[roles.size()]))); final List<Set<ScannedGenericBeanDefinition>> set = scan(basePackage, fields); return set.stream().map(definitions -> definitions.stream().map(def -> { final MultiValueMap<String, Object> map = ofNullable( def.getMetadata().getAllAnnotationAttributes(Profile.class.getName())).orElse(matchAll); //noinspection unchecked return ((List<String[]>) (Object) map.get("value")).stream().flatMap(Arrays::stream).collect(toList()); }).flatMap(Collection::stream).collect(toList())) .allMatch(profiles -> profiles.stream().filter(roles::contains).findAny().isPresent()); }
From source file:eu.squadd.testing.objectspopulator.RandomValuePopulator.java
private Set<Field> getAllFields(Class targetClass, Predicate<Field> alwaysTrue) { Field[] fields = targetClass.getDeclaredFields(); Set<Field> result = new HashSet(); result.addAll(Arrays.asList(fields)); return result; }
From source file:com.adito.server.jetty.TunnelAdapter.java
private Object getPrivateMember(Object obj, String type) { Class secretClass = obj.getClass(); // Print all the field names & values Field fields[] = secretClass.getDeclaredFields(); if (log.isDebugEnabled()) log.debug("Access all the fields on " + obj.getClass().getName() + " looking for type " + type); for (int i = 0; i < fields.length; i++) { if (log.isDebugEnabled()) log.debug("Field Name: " + fields[i].getName()); fields[i].setAccessible(true);/*from w ww .jav a2 s . c o m*/ try { Object obj2 = fields[i].get(obj); if (obj2 != null && obj2.getClass().getName().equals(type)) { return obj2; } } catch (Throwable e) { log.error("Failed to set timeout on SSLSocketImpl", e); } } log.error("Could not find " + type + " on object " + obj.getClass().getName()); return null; }
From source file:com.nerve.commons.repository.utils.reflection.ReflectionUtils.java
/** * * ?annotationClass//from w ww .j a va 2 s. co m * * @param targetClass * Class * @param annotationClass * Class * * @return List */ public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) { Assert.notNull(targetClass, "targetClass?"); Assert.notNull(annotationClass, "annotationClass?"); List<T> result = new ArrayList<T>(); Annotation annotation = targetClass.getAnnotation(annotationClass); if (annotation != null) { result.add((T) annotation); } Constructor[] constructors = targetClass.getDeclaredConstructors(); // ? CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator()); Field[] fields = targetClass.getDeclaredFields(); // ? CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator()); Method[] methods = targetClass.getDeclaredMethods(); // ? CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator()); for (Class<?> superClass = targetClass.getSuperclass(); superClass == null || superClass == Object.class; superClass = superClass.getSuperclass()) { List<T> temp = (List<T>) getAnnotations(superClass, annotationClass); if (CollectionUtils.isNotEmpty(temp)) { CollectionUtils.addAll(result, temp.iterator()); } } return result; }
From source file:com.billing.ng.plugin.Parameters.java
/** * Returns the field name of javabeans properties annotated with the * {@link Parameter} annotation./*from w ww . j a v a 2 s . c o m*/ * * @param type plugin class with parameter annotations * @return map of collected parameter field names and the associated annotation */ public Map<String, Parameter> getAnnotatedFields(Class<T> type) { Map<String, Parameter> fields = new HashMap<String, Parameter>(); // collect public member methods of the class, including those defined on the interface // or those inherited from a super class or super interface. for (Method method : type.getMethods()) { Parameter annotation = method.getAnnotation(Parameter.class); if (annotation != null) { if (method.getName().startsWith("get") || method.getName().startsWith("set")) { fields.put(ClassUtils.getFieldName(method.getName()), annotation); } } } // collection all field annotations, including private fields that // we can to access via a public accessor method Class klass = type; while (klass != null) { for (Field field : klass.getDeclaredFields()) { Parameter annotation = field.getAnnotation(Parameter.class); if (annotation != null) { fields.put(field.getName(), annotation); } } // try the super class klass = klass.getSuperclass(); } return fields; }
From source file:org.suren.autotest.web.framework.data.YamlDataSource.java
/** * @param fieldMap/* w ww. j a v a 2s.c om*/ * @param targetPage */ private void pageParse(Map fieldMap, Page targetPage) { Class<? extends Page> targetPageCls = targetPage.getClass(); Field[] decFields = targetPageCls.getDeclaredFields(); for (Field field : decFields) { String name = field.getName(); Object data = fieldMap.get(name); try { try { setValue(field, targetPage, data); } catch (NoSuchMethodException | SecurityException | InvocationTargetException e) { e.printStackTrace(); } } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } }
From source file:atunit.spring.SpringContainer.java
protected void fillInMissingFieldBeans(Class<?> testClass, GenericApplicationContext ctx) throws Exception { for (Field field : testClass.getDeclaredFields()) { Bean beanAnno = field.getAnnotation(Bean.class); if (beanAnno == null) continue; String name = beanAnno.value(); if (!name.equals("") && !ctx.containsBean(name)) { ctx.registerBeanDefinition(name, defineAutowireBean(field.getType())); } else if (ctx.getBeansOfType(field.getType()).isEmpty()) { BeanDefinitionReaderUtils.registerWithGeneratedName(defineAutowireBean(field.getType()), ctx); }//from w w w. ja v a 2s . co m } }
From source file:kelly.core.injector.AbstractSpringInjector.java
private Field[] getFieldsIncludingSuperclass(Object bean) { Class<?> type = bean.getClass(); List<Field> fields = new ArrayList<Field>(); while (type != null) { fields.addAll(Arrays.asList(type.getDeclaredFields())); type = type.getSuperclass();/*ww w .j a v a 2 s. c om*/ } return fields.toArray(new Field[fields.size()]); }