@Test public void methodAndTypeUse() throws Exception { Class<ImmutableDoNotPropagateTypeAnnotation> c = ImmutableDoNotPropagateTypeAnnotation.class; check(c.getDeclaredField("a").getAnnotatedType().getAnnotation(MethodAndTypeUse.class)).isNull(); check(c.getDeclaredField("b").getAnnotatedType().getAnnotation(MethodAndTypeUse.class)).isNull(); check(c.getDeclaredField("c").getAnnotatedType().getAnnotation(TypeUseOnly.class)).notNull(); } }
private static AnnotatedType zilch() { try { return ParameterTypeContext.class.getDeclaredField("zilch").getAnnotatedType(); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } }
// get the email field Field emailAddressField = MyClass.class.getDeclaredField("emailAddresses"); // the field's type is both parameterized and annotated, // cast it to the right type representation AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType) emailAddressField.getAnnotatedType(); // get all type parameters AnnotatedType[] annotatedActualTypeArguments = annotatedParameterizedType.getAnnotatedActualTypeArguments(); // the String parameter which contains the annotation AnnotatedType stringParameterType = annotatedActualTypeArguments[0]; // The actual annotation Annotation emailAnnotation = stringParameterType.getAnnotations()[0]; System.out.println(emailAnnotation); // @Email()
@Override public void configure(AnnotatedType annotatedType) { super.configure(annotatedType); for (int i = 0; i < fields.size(); ++i) fieldGenerators.get(i).configure(fields.get(i).getAnnotatedType()); }
@Override public Generator<?> field(Field field) { return produceGenerator( new ParameterTypeContext( field.getName(), field.getAnnotatedType(), field.getDeclaringClass().getName() ).annotate(field)); }
public static ParameterTypeContext typeOf(Class<?> c, String fieldName) throws NoSuchFieldException { return new ParameterTypeContext( fieldName, c.getDeclaredField(fieldName).getAnnotatedType(), c.getName()) .allowMixedTypes(true); } }
protected final AnnotatedType annotatedType() throws Exception { try { return typeBearerField().getAnnotatedType(); } catch (Exception e) { return typeBearerParameter().getAnnotatedType(); } }
private static AnnotatedType zilch() { try { return ParameterTypeContext.class.getDeclaredField("zilch").getAnnotatedType(); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } }
@Override public AnnotatedType getAnnotatedType() { return getHost().getAnnotatedType(); }
private GraphQLInputType getType(Field field) { return (GraphQLInputType) typeFunction.buildType(true, field.getType(), field.getAnnotatedType(), container); }
@Override public void configure(AnnotatedType annotatedType) { super.configure(annotatedType); for (int i = 0; i < fields.size(); ++i) fieldGenerators.get(i).configure(fields.get(i).getAnnotatedType()); }
Set<Type> getContextFieldTypes() { return replacedFields.stream() .filter(f -> f.isAnnotationPresent(Context.class) || f.isAnnotationPresent(ContextResolved.class)) .map(f -> f.getJavaMember().getAnnotatedType().getType()) .collect(toSet()); }
@Override public Generator<?> field(Field field) { return produceGenerator( new ParameterTypeContext( field.getName(), field.getAnnotatedType(), field.getDeclaringClass().getName() ).annotate(field)); }
/** * Finds type arguments constraints for fields. */ protected Set<MetaConstraint<?>> findTypeAnnotationConstraints(Field field) { return findTypeArgumentsConstraints( field, new TypeArgumentFieldLocation( field ), field.getAnnotatedType() ); }
private CascadingMetaDataBuilder findCascadingMetaData(Field field) { TypeVariable<?>[] typeParameters = field.getType().getTypeParameters(); AnnotatedType annotatedType = field.getAnnotatedType(); Map<TypeVariable<?>, CascadingMetaDataBuilder> containerElementTypesCascadingMetaData = getTypeParametersCascadingMetadata( annotatedType, typeParameters ); return getCascadingMetaData( ReflectionHelper.typeOf( field ), field, containerElementTypesCascadingMetaData ); }
Set<Type> getContextFieldTypes() { return replacedFields.stream() .filter(f -> f.isAnnotationPresent(Context.class) || f.isAnnotationPresent(ContextResolved.class)) .map(f -> f.getJavaMember().getAnnotatedType().getType()) .collect(toSet()); }
@Test public void buildType_integerFieldAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchFieldException { // Arrange DefaultTypeFunction instance = testedDefaultTypeFunction(); Field idIntegerField = IDFunctionTests.class.getField("idIntegerField"); // Act+Assert assertEquals(instance.buildType(idIntegerField.getType(), idIntegerField.getAnnotatedType(),null), GraphQLID); }
@Test public void buildType_stringFieldAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchFieldException { // Arrange DefaultTypeFunction instance = testedDefaultTypeFunction(); Field idStringField = IDFunctionTests.class.getField("idStringField"); // Act+Assert assertEquals(instance.buildType(idStringField.getType(), idStringField.getAnnotatedType(),null), GraphQLID); }
@Test public void buildType_intFieldAnnotatedWithGraphQLID_returnsGraphQLID() throws NoSuchFieldException { // Arrange DefaultTypeFunction instance = testedDefaultTypeFunction(); Field idIntField = IDFunctionTests.class.getField("idIntField"); // Act+Assert assertEquals(instance.buildType(idIntField.getType(), idIntField.getAnnotatedType(),null), GraphQLID); }
@Test public void recursiveTypes() throws Exception { ProcessingElementsContainer container = testedProcessingElementsContainer(); GraphQLType type = container.getDefaultTypeFunction().buildType(Class1.class, Class2.class.getField("class1").getAnnotatedType(),container); GraphQLFieldDefinition class1class2 = ((GraphQLObjectType) type).getFieldDefinition("class2"); assertNotNull(class1class2); assertTrue(((GraphQLObjectType) class1class2.getType()).getFieldDefinition("class1").getType() instanceof GraphQLTypeReference); assertTrue(((GraphQLObjectType) class1class2.getType()).getFieldDefinition("class2").getType() instanceof GraphQLTypeReference); GraphQLAnnotations.instance = new GraphQLAnnotations(); }