public Entity(Class entityClass) { this.entityClass = entityClass; FieldUtils.getAllFields(entityClass) .stream().filter(f -> !Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) .forEach(f -> { fields.put(f.getName(), f); f.setAccessible(true); }); }
private void produceDefaultColumnConsumer(){ List<String> cols = Arrays.asList(columns); for (Field field : FieldUtils.getAllFields(clz)) { if (columnConsumer.keySet().contains(field.getName()) || !cols.contains(field.getName())) { continue; } Consumer<T> setter = getDefaultSetter(field); Optional.ofNullable(setter).ifPresent(it -> columnConsumer.put(field.getName(), it)); } }
@StaticInit static void staticInit() { BeanUtils.reflections.getTypesAnnotatedWith(Entity.class).forEach(clz -> { Metadata m = new Metadata(); m.entityClass = clz; FieldUtils.getAllFields(clz).forEach(f -> { m.allFieldNames.add(f.getName()); if (f.isAnnotationPresent(Id.class)) { m.fieldPrimaryKeys.add(f); } }); metadata.put(clz, m); }); }
private static void collectDynamicObjectMetadata() { reflections.getSubTypesOf(DynamicObject.class).forEach(clz -> { DynamicObjectMetadata metadata = new DynamicObjectMetadata(); FieldUtils.getAllFields(clz).forEach(f -> { f.setAccessible(true); metadata.fields.put(f.getName(), f); }); Class p = clz; while (p != Object.class) { for (Method m : p.getDeclaredMethods()) { m.setAccessible(true); metadata.methods.put(m.getName(), m); } p = p.getSuperclass(); } dynamicObjectMetadata.put(clz, metadata); }); }
private void classToApiMessageGroovyInformation(StringBuilder sb, Class<?> clazz) { if (Modifier.isStatic(clazz.getModifiers())) { return; } String name = clazz.getSimpleName().replace("\\.", "_").toUpperCase(); sb.append(String.format("\n%sdef %s = [name: '%s',", whiteSpace(4), name, clazz.getName())); List<Field> fs = FieldUtils.getAllFields(clazz); List<String> mandatoryFields = new ArrayList<String>(); for (Field f : fs) { APIParam at = f.getAnnotation(APIParam.class); if (at != null && at.required()) { mandatoryFields.add(String.format("'%s'", f.getName())); } } sb.append(String.format("requiredFields: [%s]]\n", StringUtils.join(mandatoryFields.iterator(), ","))); }
public static <T> T copy(T dst, Object src) { Class dstClass = dst.getClass(); Class srcClass = null; Class clazz = src.getClass(); do { if (clazz.isAssignableFrom(dstClass)) { srcClass = clazz; break; } clazz = clazz.getSuperclass(); } while (clazz != Object.class); if (srcClass == null) { throw new RuntimeException(String.format("%s is not assignable from %s", src.getClass().getName(), dstClass.getName())); } try { Map<String, Field> dstFields = new HashMap<String, Field>(); for (Field f : FieldUtils.getAllFields(dstClass)) { dstFields.put(f.getName(), f); } for (Field f : FieldUtils.getAllFields(srcClass)) { f.setAccessible(true); Field dstf = dstFields.get(f.getName()); dstf.setAccessible(true); dstf.set(dst, f.get(src)); } return dst; } catch (Exception e) { throw new RuntimeException(e); } }
private void generateVOViewSql(StringBuilder sb, Class<?> entityClass) { if (!entityClass.isAnnotationPresent(Entity.class)) { throw new IllegalArgumentException(String.format("class[%s] is annotated by @EO but not annotated by @Entity", entityClass.getName())); } EO at = entityClass.getAnnotation(EO.class); if (!at.needView()) { return; } Class EOClazz = at.EOClazz(); List<Field> fs = FieldUtils.getAllFields(entityClass); sb.append(String.format("\nCREATE VIEW `zstack`.`%s` AS SELECT ", entityClass.getSimpleName())); List<String> cols = new ArrayList<String>(); for (Field f : fs) { if (!f.isAnnotationPresent(Column.class) || f.isAnnotationPresent(NoView.class)) { continue; } cols.add(f.getName()); } sb.append(org.apache.commons.lang.StringUtils.join(cols, ", ")); sb.append(String.format(" FROM `zstack`.`%s` WHERE %s IS NULL;\n", EOClazz.getSimpleName(), at.softDeletedColumn())); }
private void checkBoxTypeInInventory() { if (!CoreGlobalProperty.CHECK_BOX_TYPE_IN_INVENTORY) { return; } Set<Class<?>> inventoryClasses = BeanUtils.reflections.getTypesAnnotatedWith(Inventory.class); List<String> errors = new ArrayList<>(); for (Class clz : inventoryClasses) { boolean error = false; StringBuilder sb = new StringBuilder(String.format("inventory class[%s] contains below primitive fields:", clz.getName())); for (Field f : FieldUtils.getAllFields(clz)) { if (f.isAnnotationPresent(APINoSee.class)) { continue; } if (TypeUtils.isPrimitiveType(f.getType())) { error = true; sb.append(String.format("\n%s[%s]", f.getName(), f.getType().getName())); } } if (error) { errors.add(sb.toString()); } } if (!errors.isEmpty()) { throw new CloudRuntimeException(String.format("detected some inventory class using primitive type." + " Please change those primitive type field to corresponding box type:\n %s", StringUtils.join(errors, "\n\n"))); } }
public RestResponseWrapper(RestResponse annotation, Class apiResponseClass) { this.annotation = annotation; this.apiResponseClass = apiResponseClass; if (annotation.fieldsTo().length > 0) { responseMappingFields = new HashMap<>(); if (annotation.fieldsTo().length == 1 && "all".equals(annotation.fieldsTo()[0])) { List<Field> apiFields = FieldUtils.getAllFields(apiResponseClass); apiFields = apiFields.stream().filter(f -> !f.isAnnotationPresent(APINoSee.class) && !Modifier.isStatic(f.getModifiers())).collect(Collectors.toList()); for (Field f : apiFields) { responseMappingFields.put(f.getName(), f.getName()); } } else { for (String mf : annotation.fieldsTo()) { String[] kv = mf.split("="); if (kv.length == 2) { responseMappingFields.put(kv[0].trim(), kv[1].trim()); } else if (kv.length == 1) { responseMappingFields.put(kv[0].trim(), kv[0].trim()); } else { throw new CloudRuntimeException(String.format("bad mappingFields[%s] of %s", mf, apiResponseClass)); } } } } } }
private static void collectResponseMappingFields() { BeanUtils.reflections.getSubTypesOf(APIResponse.class).stream().filter(clz-> clz != APIReply.class && clz != APIEvent.class && !Modifier.isStatic(clz.getModifiers()) && clz.isAnnotationPresent(RestResponse.class)).forEach(clz -> { RestResponse annotation = clz.getAnnotation(RestResponse.class); Map responseMappingFields = new HashMap(); if (!annotation.allTo().equals("")) { responseMappingFields.put(annotation.allTo(), annotation.allTo()); } else if (annotation.fieldsTo().length > 0) { if (annotation.fieldsTo().length == 1 && "all".equals(annotation.fieldsTo()[0])) { List<Field> apiFields = FieldUtils.getAllFields(clz); apiFields = apiFields.stream().filter(f -> !f.isAnnotationPresent(APINoSee.class) && !Modifier.isStatic(f.getModifiers())).collect(Collectors.toList()); for (Field f : apiFields) { responseMappingFields.put(f.getName(), f.getName()); } } else { for (String mf : annotation.fieldsTo()) { String[] kv = mf.split("="); if (kv.length == 2) { responseMappingFields.put(kv[0].trim(), kv[1].trim()); } else if (kv.length == 1) { responseMappingFields.put(kv[0].trim(), kv[0].trim()); } else { throw new CloudRuntimeException(String.format("bad mappingFields[%s] of %s", mf, clz)); } } } } APIResponse.responseMappingFields.put(clz, responseMappingFields); }); }
List<Field> fs = FieldUtils.getAllFields(obj.getClass()); for (Field f : fs) { if (Modifier.isStatic(f.getModifiers())) {
private void build(Object o, Stack<String> paths) throws IllegalAccessException { List<Field> fields = FieldUtils.getAllFields(o.getClass());
List<Field> allFields = FieldUtils.getAllFields(clz); for (Field f : allFields) { APIParam at = f.getAnnotation(APIParam.class);
private void initSystemTags() throws IllegalAccessException { Set<Class<?>> classes = BeanUtils.reflections.getTypesAnnotatedWith(TagDefinition.class); for (Class clz : classes) { List<Field> fields = FieldUtils.getAllFields(clz); for (Field f : fields) { if (!Modifier.isStatic(f.getModifiers())) {
List<Field> fs = FieldUtils.getAllFields(apiClass); fs = fs.stream().filter(f -> !f.isAnnotationPresent(APINoSee.class) && !Modifier.isStatic(f.getModifiers())).collect(Collectors.toList()); for (Field f : fs) {
List<Field> fields = FieldUtils.getAllFields(clz);
void loadFromVO(GarbageCollectorVO vo) { Object dataObj = JSONObjectUtil.toObject(vo.getContext(), getClass()); for (Field f : FieldUtils.getAllFields(getClass())) { if (!f.isAnnotationPresent(GC.class)) { continue; } try { f.setAccessible(true); f.set(this, f.get(dataObj)); } catch (Exception e) { throw new CloudRuntimeException(e); } } uuid = vo.getUuid(); vo.setStatus(GCStatus.Idle); vo.setManagementNodeUuid(Platform.getManagementServerId()); dbf.update(vo); gcMgr.registerGC(this); }
List<Field> allFields = FieldUtils.getAllFields(inventoryClass); for (Field f : allFields) { f.setAccessible(true);
private void buildApiParams(ApiMessageDescriptor desc) { Class msgClz = desc.getClazz(); List<Field> fields = FieldUtils.getAllFields(msgClz);
final protected void saveToDb() { Map context = new HashMap<>(); for (Field f : FieldUtils.getAllFields(getClass())) { if (!f.isAnnotationPresent(GC.class)) { continue;