@Override public MetaClass getEnclosingType(final HasAnnotations annotated) { return ((MetaField) annotated).getDeclaringClass(); }
private static void filterResultsField(final Collection<MetaField> result, final Set<String> packages, final String excludeRegEx) { final Pattern excludePattern; if (excludeRegEx != null) { excludePattern = Pattern.compile(excludeRegEx); } else { excludePattern = null; } final Iterator<MetaField> filterIterator = result.iterator(); while (filterIterator.hasNext()) { _removeIfNotMatches(filterIterator, filterIterator.next().getDeclaringClass(), packages, excludePattern); } }
private static void filterResultsField(final Collection<MetaField> result, final Set<String> packages, final String excludeRegEx) { final Pattern excludePattern; if (excludeRegEx != null) { excludePattern = Pattern.compile(excludeRegEx); } else { excludePattern = null; } final Iterator<MetaField> filterIterator = result.iterator(); while (filterIterator.hasNext()) { _removeIfNotMatches(filterIterator, filterIterator.next().getDeclaringClass(), packages, excludePattern); } }
public static boolean equals(final MetaField a, final MetaField b) { return a.getName().equals(b.getName()) && !a.getType().equals(b.getType()) && !a.getDeclaringClass().equals(b.getDeclaringClass()); }
public static boolean equals(final MetaField a, final MetaField b) { return a.getName().equals(b.getName()) && !a.getType().equals(b.getType()) && !a.getDeclaringClass().equals(b.getDeclaringClass()); }
/** * Returns the java.lang.reflect.Field object representing this MetaField. * * @return The Java Reflection Field object representing the same field as * this MetaField. Never null. * @throws IllegalStateException * if the field or its containing class cannot be located using Java * Reflection. */ public Field asField() { try { final Class<?> aClass = getDeclaringClass().asClass(); return aClass.getDeclaredField(getName()); } catch (Throwable e) { throw new IllegalStateException(e); } }
/** * Returns the java.lang.reflect.Field object representing this MetaField. * * @return The Java Reflection Field object representing the same field as * this MetaField. Never null. * @throws IllegalStateException * if the field or its containing class cannot be located using Java * Reflection. */ public Field asField() { try { final Class<?> aClass = getDeclaringClass().asClass(); return aClass.getDeclaredField(getName()); } catch (Throwable e) { throw new IllegalStateException(e); } }
public static String fieldAccess(final MetaField field) { if (field.isStatic()) { return "@" + field.getDeclaringClass().getFullyQualifiedName().replace('$', '.') + "::" + field.getName(); } else { return "instance.@" + field.getDeclaringClass().getFullyQualifiedName().replace('$', '.') + "::" + field.getName(); } }
public static String fieldAccess(final MetaField field) { if (field.isStatic()) { return "@" + field.getDeclaringClass().getFullyQualifiedName().replace('$', '.') + "::" + field.getName(); } else { return "instance.@" + field.getDeclaringClass().getFullyQualifiedName().replace('$', '.') + "::" + field.getName(); } }
/** * Returns a string that uniquely identifies this field for purposes of * comparison with other implementations of {@link MetaField}. The returned * string includes the declaring class name, the field name, and the field's * type. * * @return */ public String hashString() { if (_hashString != null) return _hashString; return _hashString = MetaField.class.getName() + ":" + getDeclaringClass().getFullyQualifiedName() + "." + getName() + "::" + getType().getFullyQualifiedName(); }
/** * Returns a string that uniquely identifies this field for purposes of * comparison with other implementations of {@link MetaField}. The returned * string includes the declaring class name, the field name, and the field's * type. * * @return */ public String hashString() { if (_hashString != null) return _hashString; return _hashString = MetaField.class.getName() + ":" + getDeclaringClass().getFullyQualifiedName() + "." + getName() + "::" + getType().getFullyQualifiedName(); }
private Set<Class<?>> extractValidatableBeans(final Set<MetaClass> beans, final GeneratorContext context) { final Set<Class<?>> allBeans = new HashSet<Class<?>>(); for (final MetaClass bean : beans) { allBeans.add(bean.asClass()); } for (final MetaField field : ClassScanner.getFieldsAnnotatedWith(Valid.class, null, context)) { allBeans.add(field.getDeclaringClass().asClass()); allBeans.add(field.getType().asClass()); } for (final MetaMethod method : ClassScanner.getMethodsAnnotatedWith(Valid.class, null, context)) { allBeans.add(method.getDeclaringClass().asClass()); allBeans.add(method.getReturnType().asClass()); } return allBeans; }
public static String getPrivateFieldAccessorName(final MetaField field) { return field.getDeclaringClass().getName() + "_" + getTypeName(field.getType()) + "_" + field.getName(); }
public static String getPrivateFieldAccessorName(final MetaField field) { return field.getDeclaringClass().getName() + "_" + getTypeName(field.getType()) + "_" + field.getName(); }
@Test public void testGetFields() { final List<String> expectedFields = Lists.newLinkedList(); expectedFields.add(Child.class.getCanonicalName() + "." + "childPublic"); expectedFields.add(Parent.class.getCanonicalName() + "." + "parentPublic"); final ArrayList<String> actualFields = new ArrayList<>(); for (final MetaField field : getMetaClass(Child.class).getFields()) { actualFields.add(field.getDeclaringClass().getCanonicalName() + "." + field.getName()); } Collections.sort(expectedFields); Collections.sort(actualFields); assertEquals(expectedFields.toString(), actualFields.toString()); }
@Test public void testGetDeclaredFields() { final List<String> expectedFields = Lists.newLinkedList(); expectedFields.add(Child.class.getCanonicalName() + "." + "childPrivate"); expectedFields.add(Child.class.getCanonicalName() + "." + "childPackage"); expectedFields.add(Child.class.getCanonicalName() + "." + "childProtected"); expectedFields.add(Child.class.getCanonicalName() + "." + "childPublic"); final ArrayList<String> actualFields = new ArrayList<>(); for (final MetaField field : getMetaClass(Child.class).getDeclaredFields()) { actualFields.add(field.getDeclaringClass().getCanonicalName() + "." + field.getName()); } Collections.sort(expectedFields); Collections.sort(actualFields); assertEquals(expectedFields.toString(), actualFields.toString()); }
@SuppressWarnings("unchecked") private SetMultimap<MetaClass, Annotation> getValidationConfig(Collection<MetaClass> validationAnnotations, GeneratorContext context) { final SetMultimap<MetaClass, Annotation> beans = HashMultimap.create(); for (final MetaClass annotation : validationAnnotations) { for (final MetaField field : ClassScanner.getFieldsAnnotatedWith((Class<? extends Annotation>) annotation.asClass(), null, context)) { beans.put(field.getDeclaringClass(), field.getAnnotation((Class<? extends Annotation>) annotation.asClass())); } for (final MetaMethod method : ClassScanner.getMethodsAnnotatedWith((Class<? extends Annotation>) annotation.asClass(), null, context)) { beans.put(method.getDeclaringClass(), method.getAnnotation((Class<? extends Annotation>) annotation.asClass())); } for (final MetaClass type : ClassScanner.getTypesAnnotatedWith((Class<? extends Annotation>) annotation.asClass(), null, context)) { beans.put(type, type.getAnnotation((Class<? extends Annotation>) annotation.asClass())); } } return beans; }
@Override public ContextualStatementBuilder call(final Statement instance, final HasAnnotations annotated, final BuildMetaClass factory, final Statement... params) { final MetaField field = (MetaField) annotated; if (field.isPublic()) { if (field.isStatic()) { return loadStatic(field.getDeclaringClass(), field.getName()); } else { return nestedCall(instance).loadField(field); } } else { final Object[] accessorParams = (field.isStatic() ? new Object[0] : new Object[] { instance }); return invokeStatic(notNull(factory), getPrivateFieldAccessorName(field), accessorParams); } } },
public static String initCachedField(final ClassStructureBuilder<?> classBuilder, final MetaField f) { createJavaReflectionFieldInitializerUtilMethod(classBuilder); final String fieldName = PrivateAccessUtil.getPrivateFieldAccessorName(f) + "_fld"; if (classBuilder.getClassDefinition().getField(fieldName) != null) { return fieldName; } classBuilder.privateField(fieldName, Field.class).modifiers(Modifier.Static) .initializesWith(Stmt.invokeStatic(classBuilder.getClassDefinition(), JAVA_REFL_FLD_UTIL_METH, f.getDeclaringClass(), f.getName())).finish(); return fieldName; }
public static String initCachedField(final ClassStructureBuilder<?> classBuilder, final MetaField f) { createJavaReflectionFieldInitializerUtilMethod(classBuilder); final String fieldName = PrivateAccessUtil.getPrivateFieldAccessorName(f) + "_fld"; if (classBuilder.getClassDefinition().getField(fieldName) != null) { return fieldName; } classBuilder.privateField(fieldName, Field.class).modifiers(Modifier.Static) .initializesWith(Stmt.invokeStatic(classBuilder.getClassDefinition(), JAVA_REFL_FLD_UTIL_METH, f.getDeclaringClass(), f.getName())).finish(); return fieldName; }