Java Code Examples for org.jboss.jandex.ClassInfo#method()

The following examples show how to use org.jboss.jandex.ClassInfo#method() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JpaSecurityDefinition.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static MethodInfo findGetter(Index index, ClassInfo annotatedClass, String methodName) {
    MethodInfo method = annotatedClass.method(methodName);
    if (method != null) {
        return method;
    }
    DotName superName = annotatedClass.superName();
    if (superName != null && !superName.equals(QuarkusSecurityJpaProcessor.DOTNAME_OBJECT)) {
        ClassInfo superClass = index.getClassByName(superName);
        if (superClass != null) {
            method = findGetter(index, superClass, methodName);
            if (method != null) {
                return method;
            }
        }
    }
    for (DotName interfaceName : annotatedClass.interfaceNames()) {
        ClassInfo interf = index.getClassByName(interfaceName);
        if (interf != null) {
            method = findGetter(index, interf, methodName);
            if (method != null) {
                return method;
            }
        }
    }
    return null;
}
 
Example 2
Source File: FormatHelperTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormattedLocalDate() throws Exception {
    Index complete = IndexCreator.index(AsyncApi.class);

    ClassInfo classByName = complete.getClassByName(DotName.createSimple(AsyncApi.class.getName()));
    MethodInfo nonNullString = classByName.method("formattedLocalDate");
    Type type = nonNullString.returnType();

    Annotations annotations = Annotations.getAnnotationsForMethod(nonNullString);

    Optional<TransformInfo> format = FormatHelper.getFormat(type, annotations);

    TransformInfo transformInfo = format.get();
    assertEquals("yyyy-MM-dd", transformInfo.getFormat());
}
 
Example 3
Source File: FormatHelperTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormattedCompletionStage() throws Exception {
    Index complete = IndexCreator.index(AsyncApi.class);

    ClassInfo classByName = complete.getClassByName(DotName.createSimple(AsyncApi.class.getName()));
    MethodInfo nonNullString = classByName.method("formattedCompletionStage");
    Type type = nonNullString.returnType();

    Annotations annotations = Annotations.getAnnotationsForMethod(nonNullString);

    Optional<TransformInfo> format = FormatHelper.getFormat(type, annotations);

    TransformInfo transformInfo = format.get();
    assertEquals("yyyy-MM-dd", transformInfo.getFormat());
}
 
Example 4
Source File: NonNullHelperTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonNullString() throws Exception {
    Index complete = IndexCreator.index(AsyncApi.class);

    ClassInfo classByName = complete.getClassByName(DotName.createSimple(AsyncApi.class.getName()));
    MethodInfo nonNullString = classByName.method("nonNullString");
    Type type = nonNullString.returnType();

    Annotations annotationsForMethod = Annotations.getAnnotationsForMethod(nonNullString);

    assertTrue(NonNullHelper.markAsNonNull(type, annotationsForMethod));
}
 
Example 5
Source File: NonNullHelperTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullableString() throws Exception {
    Index complete = IndexCreator.index(AsyncApi.class);

    ClassInfo classByName = complete.getClassByName(DotName.createSimple(AsyncApi.class.getName()));
    MethodInfo nonNullString = classByName.method("string");
    Type type = nonNullString.returnType();

    Annotations annotationsForMethod = Annotations.getAnnotationsForMethod(nonNullString);

    assertFalse(NonNullHelper.markAsNonNull(type, annotationsForMethod));
}
 
Example 6
Source File: NonNullHelperTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonNullCompletionStage() throws Exception {
    Index complete = IndexCreator.index(AsyncApi.class);

    ClassInfo classByName = complete.getClassByName(DotName.createSimple(AsyncApi.class.getName()));
    MethodInfo nonNullString = classByName.method("nonNullCompletionStage");
    Type type = nonNullString.returnType();

    Annotations annotationsForMethod = Annotations.getAnnotationsForMethod(nonNullString);

    assertTrue(NonNullHelper.markAsNonNull(type, annotationsForMethod));
}
 
Example 7
Source File: FieldNameTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldNamePriority() {
    Indexer indexer = new Indexer();
    indexDirectory(indexer, "io/smallrye/graphql/schema/creator/fieldnameapp");
    IndexView index = indexer.complete();

    ClassInfo classInfo = index.getClassByName(DotName.createSimple(SomeObjectAnnotatedGetters.class.getName()));
    // @Name
    MethodInfo methodInfo = classInfo.method("getName");
    Annotations annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("a", FieldCreator.getFieldName(Direction.OUT, annotations, "name"));

    // @Query
    methodInfo = classInfo.method("getQuery");
    annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("d", FieldCreator.getFieldName(Direction.OUT, annotations, "query"));

    // @JsonbProperty
    methodInfo = classInfo.method("getJsonbProperty");
    annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("f", FieldCreator.getFieldName(Direction.OUT, annotations, "jsonbProperty"));

    // no annotation
    methodInfo = classInfo.method("getFieldName");
    annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("fieldName", FieldCreator.getFieldName(Direction.OUT, annotations, "fieldName"));
}
 
Example 8
Source File: OperationCreatorTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailOnNonPublicOperation() throws Exception {
    Index complete = IndexCreator.index(TestApi.class);

    ClassInfo classByName = complete.getClassByName(DotName.createSimple(TestApi.class.getName()));
    MethodInfo method = classByName.method("nonPublicQuery");

    try {
        operationCreator().createOperation(method, OperationType.Query, null);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
 
Example 9
Source File: OperationCreatorTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void testPublicOperation() throws Exception {
    Index complete = IndexCreator.index(TestApi.class);

    ClassInfo classByName = complete.getClassByName(DotName.createSimple(TestApi.class.getName()));
    MethodInfo method = classByName.method("publicQuery");

    final Operation operation = operationCreator().createOperation(method, OperationType.Query, null);

    assertEquals("publicQuery", operation.getName());
}
 
Example 10
Source File: JaxRsAnnotationScanner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Build a map between exception class name and its corresponding @ApiResponse annotation in the jax-rs exception mapper
 * 
 */
private Map<DotName, AnnotationInstance> processExceptionMappers(final AnnotationScannerContext context) {
    Map<DotName, AnnotationInstance> exceptionHandlerMap = new HashMap<>();
    Collection<ClassInfo> exceptionMappers = context.getIndex()
            .getKnownDirectImplementors(JaxRsConstants.EXCEPTION_MAPPER);

    for (ClassInfo classInfo : exceptionMappers) {
        DotName exceptionDotName = classInfo.interfaceTypes()
                .stream()
                .filter(it -> it.name().equals(JaxRsConstants.EXCEPTION_MAPPER))
                .filter(it -> it.kind() == Type.Kind.PARAMETERIZED_TYPE)
                .map(Type::asParameterizedType)
                .map(type -> type.arguments().get(0)) // ExceptionMapper<?> has a single type argument
                .map(Type::name)
                .findFirst()
                .orElse(null);

        if (exceptionDotName == null) {
            continue;
        }

        MethodInfo toResponseMethod = classInfo.method(JaxRsConstants.TO_RESPONSE_METHOD_NAME,
                Type.create(exceptionDotName, Type.Kind.CLASS));

        if (ResponseReader.hasResponseCodeValue(toResponseMethod)) {
            exceptionHandlerMap.put(exceptionDotName, ResponseReader.getResponseAnnotation(toResponseMethod));
        }

    }

    return exceptionHandlerMap;
}
 
Example 11
Source File: BeanInfo.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void addConstructorLevelBindings(ClassInfo classInfo, Collection<AnnotationInstance> bindings) {
    MethodInfo constructor;
    Optional<Injection> constructorWithInject = getConstructorInjection();
    if (constructorWithInject.isPresent()) {
        constructor = constructorWithInject.get().target.asMethod();
    } else {
        constructor = classInfo.method(Methods.INIT);
    }
    if (constructor != null) {
        beanDeployment.getAnnotations(constructor).stream()
                .filter(a -> beanDeployment.getInterceptorBinding(a.name()) != null
                        && bindings.stream().noneMatch(e -> e.name().equals(a.name())))
                .forEach(a -> bindings.add(a));
    }
}
 
Example 12
Source File: ClassConfigPropertiesUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static boolean shouldCheckForDefaultValue(ClassInfo configPropertiesClassInfo, FieldInfo field) {
    String getterName = JavaBeanUtil.getGetterName(field.name(), field.type().name());
    MethodInfo getterMethod = configPropertiesClassInfo.method(getterName);
    if (getterMethod != null) {
        return Modifier.isPublic(getterMethod.flags());
    }

    return !Modifier.isFinal(field.flags()) && Modifier.isPublic(field.flags());
}
 
Example 13
Source File: FieldAccessImplementor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private MethodInfo getGetter(ClassInfo entityClass, FieldInfo field) {
    MethodInfo getter = entityClass.method(JavaBeanUtil.getGetterName(field.name(), field.type().name()));
    if (getter != null) {
        return getter;
    }

    if (entityClass.superName() != null) {
        ClassInfo superClass = index.getClassByName(entityClass.superName());
        if (superClass != null) {
            getGetter(superClass, field);
        }
    }

    return null;
}
 
Example 14
Source File: FieldAccessImplementor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private MethodInfo getSetter(ClassInfo entityClass, FieldInfo field) {
    MethodInfo setter = entityClass.method(JavaBeanUtil.getSetterName(field.name()), field.type());
    if (setter != null) {
        return setter;
    }

    if (entityClass.superName() != null) {
        ClassInfo superClass = index.getClassByName(entityClass.superName());
        if (superClass != null) {
            getSetter(superClass, field);
        }
    }

    return null;
}
 
Example 15
Source File: MappingHelper.java    From smallrye-graphql with Apache License 2.0 4 votes vote down vote up
/**
 * Get the mapping for a certain reference.
 * 
 * @param annotations the annotations
 * @return Potentially a MappingInfo model
 */
public static Optional<MappingInfo> getMapping(Reference r, Annotations annotations) {
    Type type = getMapTo(annotations);
    if (type != null) {
        String scalarName = getScalarName(type);
        Reference reference = Scalars.getScalar(scalarName);
        MappingInfo mappingInfo = new MappingInfo(reference);
        // Check the way to create this.
        String className = r.getClassName();
        if (!r.getType().equals(ReferenceType.SCALAR)) { // mapping to scalar stays on default NONE
            ClassInfo classInfo = ScanningContext.getIndex().getClassByName(DotName.createSimple(className));
            if (classInfo != null) {
                // Get Parameter type
                Type parameter = Type.create(DotName.createSimple(reference.getClassName()), Type.Kind.CLASS);

                // Check if we can use a constructor
                MethodInfo constructor = classInfo.method(CONTRUCTOR_METHOD_NAME, parameter);
                if (constructor != null) {
                    mappingInfo.setCreate(MappingInfo.Create.CONSTRUCTOR);
                } else {
                    // Check if we can use setValue
                    MethodInfo setValueMethod = classInfo.method("setValue", parameter);
                    if (setValueMethod != null) {
                        mappingInfo.setCreate(MappingInfo.Create.SET_VALUE);
                    } else {
                        // Check if we can use static fromXXXXX
                        String staticFromMethodName = "from" + scalarName;
                        MethodInfo staticFromMethod = classInfo.method(staticFromMethodName, parameter);
                        if (staticFromMethod != null) {
                            mappingInfo.setCreate(MappingInfo.Create.STATIC_FROM);
                        }
                    }
                }

            }
        }
        return Optional.of(mappingInfo);
    } else {
        // TODO: Support other than Scalar mapping 
    }
    return Optional.empty();
}
 
Example 16
Source File: TypesTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetTypeClosure() throws IOException {
    IndexView index = Basics.index(Foo.class, Baz.class, Producer.class, Object.class, List.class, Collection.class,
            Iterable.class);
    DotName bazName = DotName.createSimple(Baz.class.getName());
    DotName fooName = DotName.createSimple(Foo.class.getName());
    DotName producerName = DotName.createSimple(Producer.class.getName());
    ClassInfo fooClass = index.getClassByName(fooName);
    Map<ClassInfo, Map<TypeVariable, Type>> resolvedTypeVariables = new HashMap<>();
    BeanDeployment dummyDeployment = BeanProcessor.builder().setIndex(index).build().getBeanDeployment();

    // Baz, Foo<String>, Object
    Set<Type> bazTypes = Types.getTypeClosure(index.getClassByName(bazName), null,
            Collections.emptyMap(),
            dummyDeployment,
            resolvedTypeVariables::put);
    assertEquals(3, bazTypes.size());
    assertTrue(bazTypes.contains(Type.create(bazName, Kind.CLASS)));
    assertTrue(bazTypes.contains(ParameterizedType.create(fooName,
            new Type[] { Type.create(DotName.createSimple(String.class.getName()), Kind.CLASS) },
            null)));
    assertEquals(resolvedTypeVariables.size(), 1);
    assertTrue(resolvedTypeVariables.containsKey(fooClass));
    assertEquals(resolvedTypeVariables.get(fooClass).get(fooClass.typeParameters().get(0)),
            Type.create(DotName.createSimple(String.class.getName()), Kind.CLASS));

    resolvedTypeVariables.clear();
    // Foo<T>, Object
    Set<Type> fooTypes = Types.getClassBeanTypeClosure(fooClass,
            dummyDeployment);
    assertEquals(2, fooTypes.size());
    for (Type t : fooTypes) {
        if (t.kind().equals(Kind.PARAMETERIZED_TYPE)) {
            ParameterizedType fooType = t.asParameterizedType();
            assertEquals("T", fooType.arguments().get(0).asTypeVariable().identifier());
            assertEquals(DotNames.OBJECT, fooType.arguments().get(0).asTypeVariable().bounds().get(0).name());
        }
    }
    ClassInfo producerClass = index.getClassByName(producerName);
    String producersName = "produce";
    MethodInfo producerMethod = producerClass.method(producersName);
    // Object is the sole type
    Set<Type> producerMethodTypes = Types.getProducerMethodTypeClosure(producerMethod,
            dummyDeployment);
    assertEquals(1, producerMethodTypes.size());

    // Object is the sole type
    FieldInfo producerField = producerClass.field(producersName);
    Set<Type> producerFieldTypes = Types.getProducerFieldTypeClosure(producerField,
            dummyDeployment);
    assertEquals(1, producerFieldTypes.size());
}