List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:de.hska.ld.content.dto.CommentDto.java
public CommentDto(Comment comment) { try {//from www. jav a 2s .c om // extract and set values per reflection Class subclass = comment.getClass(); Class superclass = subclass.getSuperclass(); while (superclass != null) { Field[] declaredFields = superclass.getDeclaredFields(); for (Field commentfield : declaredFields) { commentfield.setAccessible(true); Object obj = commentfield.get(comment); commentfield.set(this, obj); } superclass = superclass.getSuperclass(); } Field[] declaredFields = comment.getClass().getDeclaredFields(); for (Field commentfield : declaredFields) { commentfield.setAccessible(true); Object obj = commentfield.get(comment); commentfield.set(this, obj); } this.setId(comment.getId()); } catch (IllegalAccessException e) { // } this.getJsonParentId(); }
From source file:management.limbr.test.util.PojoTester.java
@SuppressWarnings("unchecked") public void test() { Class<T> clazz = (Class<T>) pojo.getClass(); for (Field field : clazz.getDeclaredFields()) { String fieldName = field.getName(); Method getter = findGetter(clazz, fieldName, field.getType().equals(Boolean.class)); if (getter != null) { testGetter(getter, field);/* ww w.j av a 2s .c om*/ } Method setter = findSetter(clazz, fieldName, field.getType()); if (setter != null) { testSetter(setter, field); } } }
From source file:com.tasktop.c2c.server.common.service.tests.http.MultiUserClientHttpRequestFactoryTest.java
private HttpClient getHttpClient(ClientHttpRequest request) { Class<?> clazz = request.getClass(); while (clazz != Object.class) { for (Field field : clazz.getDeclaredFields()) { if (HttpClient.class.isAssignableFrom(field.getType())) { field.setAccessible(true); try { return (HttpClient) field.get(request); } catch (Exception e) { throw new IllegalStateException(e); }// w w w. j ava 2 s .c om } } clazz = clazz.getSuperclass(); } throw new IllegalStateException(); }
From source file:net.larry1123.elec.util.config.ConfigFile.java
/** * Will search Class and super Classes to find fields for use and add them to our known field list. * * @param config The Object to use to search * @param lastClass Mostly an internal PlaceHolder *///from w ww . j a va2 s . co m protected void collectFields(ConfigBase config, Class lastClass) { Class currentClass = lastClass == null ? config.getClass() : lastClass.getSuperclass(); for (Field field : currentClass.getDeclaredFields()) { if (field.isAnnotationPresent(ConfigField.class)) { configFields = ArrayUtils.add(configFields, field); } } if (currentClass.getSuperclass() != null) { collectFields(config, currentClass); } }
From source file:com.frand.easyandroid.db.sql.FFUpdateSqlBuilder.java
/** * where?where?primaryKey//from w ww. j ava 2 s .c o m * * @param entity * @return * @throws IllegalArgumentException * @throws IllegalAccessException * @throws FFDBException */ public FFArrayList buildWhere(Object entity) throws IllegalArgumentException, IllegalAccessException, FFDBException { Class<?> clazz = entity.getClass(); FFArrayList whereArrayList = new FFArrayList(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (!FFDBUtils.isTransient(field) && FFFieldUtil.isBaseDateType(field)) { Annotation annotation = field.getAnnotation(FFPrimaryKey.class); if (annotation != null) { String columnName = FFDBUtils.getColumnByField(field); whereArrayList.add( (columnName != null && !columnName.equals("")) ? columnName : field.getName(), field.get(entity).toString()); } } } if (whereArrayList.isEmpty()) { throw new FFDBException("?Where??"); } return whereArrayList; }
From source file:com.hc.wx.server.common.bytecode.ReflectUtils.java
private static Object getEmptyObject(Class<?> returnType, Map<Class<?>, Object> emptyInstances, int level) { if (level > 2) return null; if (returnType == null) { return null; } else if (returnType == boolean.class || returnType == Boolean.class) { return false; } else if (returnType == char.class || returnType == Character.class) { return '\0'; } else if (returnType == byte.class || returnType == Byte.class) { return (byte) 0; } else if (returnType == short.class || returnType == Short.class) { return (short) 0; } else if (returnType == int.class || returnType == Integer.class) { return 0; } else if (returnType == long.class || returnType == Long.class) { return 0L; } else if (returnType == float.class || returnType == Float.class) { return 0F; } else if (returnType == double.class || returnType == Double.class) { return 0D; } else if (returnType.isArray()) { return Array.newInstance(returnType.getComponentType(), 0); } else if (returnType.isAssignableFrom(ArrayList.class)) { return new ArrayList<Object>(0); } else if (returnType.isAssignableFrom(HashSet.class)) { return new HashSet<Object>(0); } else if (returnType.isAssignableFrom(HashMap.class)) { return new HashMap<Object, Object>(0); } else if (String.class.equals(returnType)) { return ""; } else if (!returnType.isInterface()) { try {//from ww w .j av a 2 s. co m Object value = emptyInstances.get(returnType); if (value == null) { value = returnType.newInstance(); emptyInstances.put(returnType, value); } Class<?> cls = value.getClass(); while (cls != null && cls != Object.class) { Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { Object property = getEmptyObject(field.getType(), emptyInstances, level + 1); if (property != null) { try { if (!field.isAccessible()) { field.setAccessible(true); } field.set(value, property); } catch (Throwable e) { } } } cls = cls.getSuperclass(); } return value; } catch (Throwable e) { return null; } } else { return null; } }
From source file:com.legstar.jaxb.plugin.EnumCases.java
/** * Check Get Set methods on enumerations. * @throws Exception if test fails/* w w w . j ava 2s .c o m*/ */ public void testGetSetEnum() throws Exception { /* Load the generated class */ URI loc = GEN_SRC_DIR.toURI(); URL[] ua = { loc.toURL() }; URLClassLoader cl = new URLClassLoader(ua); Class<?> clazzSearchRequestType = cl.loadClass("com.legstar.test.coxb.enumvar.SearchRequestType"); Object searchRequest = clazzSearchRequestType.newInstance(); /* Create an Enum type with a value */ Class<?> clazzSafeSearchOptionsType = cl.loadClass("com.legstar.test.coxb.enumvar.SafeSearchOptionsType"); Field[] fields = clazzSafeSearchOptionsType.getDeclaredFields(); for (Field field : fields) { _log.debug(field.getName()); } /* Create a new enum type with a value */ Method getValueMethod = clazzSafeSearchOptionsType.getMethod("value", (Class[]) null); Method fromValueMethod = clazzSafeSearchOptionsType.getMethod("fromValue", new Class[] { getValueMethod.getReturnType() }); Object safeSearchOption = fromValueMethod.invoke(null, new Object[] { "Strict" }); /* Get the value of an Enum */ getValueMethod.invoke(safeSearchOption, (Object[]) null); /* Set the Enum value*/ Class<?>[] param = { clazzSafeSearchOptionsType }; String setterName = "setSafeSearch"; Method setter = searchRequest.getClass().getMethod(setterName, param); setter.invoke(searchRequest, safeSearchOption); String getterName = "getSafeSearch"; Method getter = searchRequest.getClass().getMethod(getterName); Object result = getter.invoke(searchRequest); assertEquals("Strict", getValueMethod.invoke(result, (Object[]) null)); }
From source file:com.github.tddts.jet.config.spring.postprocessor.LoadContentAnnotationBeanPostProcessor.java
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Pair<Class<?>, Object> typeObjectPair = SpringUtil.checkForDinamicProxy(bean); Class<?> type = typeObjectPair.getLeft(); Object target = typeObjectPair.getRight(); try {//from w w w . j a v a2 s . c o m for (Field field : type.getDeclaredFields()) { if (field.isAnnotationPresent(LoadContent.class) && String.class.equals(field.getType())) { field.setAccessible(true); String fileName = getFileName(target, field); if (!StringUtils.isEmpty(fileName)) { field.set(target, Util.loadContent(fileName)); } } } } catch (Exception e) { throw new BeanInitializationException(e.getMessage(), e); } return bean; }
From source file:de.hsos.ecs.richwps.wpsmonitor.boundary.cli.command.annotation.CommandAnnotationProcessor.java
private List<Field> getFields(final Class<?> type) { List<Field> fields = new ArrayList<>(); fields.addAll(Arrays.asList(type.getDeclaredFields())); if (type.getSuperclass() != null) { fields.addAll(getFields(type.getSuperclass())); }//w ww .j a va 2s . com return fields; }
From source file:jp.gr.java_conf.ka_ka_xyz.processor.AnnotationProcessor.java
private void getFields(Class clazz, List<Field> fields) { if (clazz == null) { return;/* ww w. j a v a 2s. c o m*/ } Field[] temp = clazz.getDeclaredFields(); if (temp != null) { for (Field field : temp) { fields.add(field); } } Class superClazz = clazz.getSuperclass(); if (superClazz != null) { getFields(superClazz, fields); } }