Java Code Examples for org.jboss.jandex.Index#getClassByName()

The following examples show how to use org.jboss.jandex.Index#getClassByName() . 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: TypeUtil.java    From serianalyzer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param i
 * @param methodReference
 * @param ci
 * @return whether any superclass implements the method
 */
public static boolean implementsMethodRecursive ( Index i, MethodReference methodReference, ClassInfo ci ) {
    if ( implementsMethod(methodReference, ci) ) {
        return true;
    }

    DotName superName = ci.superName();
    if ( superName != null ) {
        ClassInfo superByName = i.getClassByName(superName);
        if ( superByName == null || "java.lang.Object".equals(superByName.name().toString()) ) { //$NON-NLS-1$
            return false;
        }

        return implementsMethodRecursive(i, methodReference, superByName);
    }
    return false;
}
 
Example 2
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 3
Source File: BeanInfoQualifiersTest.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Test
public void testQualifiers() throws IOException {
    Index index = index(Foo.class, Bar.class, FooQualifier.class, AbstractList.class, AbstractCollection.class,
            List.class, Collection.class, Object.class, String.class, Iterable.class);
    DotName fooName = name(Foo.class);
    DotName fooQualifierName = name(FooQualifier.class);
    ClassInfo fooClass = index.getClassByName(fooName);

    BeanInfo bean = Beans.createClassBean(fooClass, BeanProcessor.builder().setIndex(index).build().getBeanDeployment(),
            null);

    AnnotationInstance requiredFooQualifier = index.getAnnotations(fooQualifierName).stream()
            .filter(a -> Kind.FIELD.equals(a.target().kind()) && a.target().asField().name().equals("foo")).findFirst()
            .orElse(null);

    assertNotNull(requiredFooQualifier);
    // FooQualifier#alpha() is @Nonbinding
    assertTrue(Beans.hasQualifier(bean, requiredFooQualifier));
}
 
Example 4
Source File: CompositeIndex.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @see {@link Index#getClassByName(org.jboss.jandex.DotName)}
 */
public ClassInfo getClassByName(final DotName className) {
    for (Index index : indexes) {
        final ClassInfo info = index.getClassByName(className);
        if (info != null) {
            return info;
        }
    }
    return null;
}
 
Example 5
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 6
Source File: TypeUtil.java    From serianalyzer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param bInfo
 * @param aInfo
 * @return
 * @throws SerianalyzerException
 */
private static boolean extendsClass ( Index i, ClassInfo extendor, ClassInfo base ) throws SerianalyzerException {
    if ( extendor.equals(base) ) {
        return true;
    }
    DotName superName = extendor.superName();
    if ( superName != null ) {
        ClassInfo superByName = i.getClassByName(superName);
        if ( superByName == null ) {
            throw new SerianalyzerException("Failed to find super class " + superName); //$NON-NLS-1$
        }
        return extendsClass(i, superByName, base);
    }
    return false;
}
 
Example 7
Source File: Serianalyzer.java    From serianalyzer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param index
 * @param classByName
 * @return
 */
private boolean isTypeSerializable ( Index index, String typeName ) {

    Boolean s = this.serializableCache.get(typeName);
    if ( s != null ) {
        return s;
    }
    ClassInfo classByName = index.getClassByName(DotName.createSimple(typeName));
    s = TypeUtil.isSerializable(index, classByName);
    this.serializableCache.put(typeName, s);
    return s;
}
 
Example 8
Source File: SimpleGeneratorTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void init() throws IOException {
    TestClassOutput classOutput = new TestClassOutput();
    Index index = index(MyService.class, PublicMyService.class, BaseService.class, MyItem.class, String.class,
            CompletionStage.class,
            List.class);
    ValueResolverGenerator generator = new ValueResolverGenerator(index, classOutput, Collections.emptyMap());
    ClassInfo myServiceClazz = index.getClassByName(DotName.createSimple(MyService.class.getName()));
    generator.generate(myServiceClazz);
    generator.generate(index.getClassByName(DotName.createSimple(PublicMyService.class.getName())));
    generator.generate(index.getClassByName(DotName.createSimple(MyItem.class.getName())));
    generator.generate(index.getClassByName(DotName.createSimple(String.class.getName())));
    generator.generate(index.getClassByName(DotName.createSimple(List.class.getName())));
    generatedTypes.addAll(generator.getGeneratedTypes());

    ExtensionMethodGenerator extensionMethodGenerator = new ExtensionMethodGenerator(classOutput);
    MethodInfo extensionMethod = index.getClassByName(DotName.createSimple(MyService.class.getName())).method(
            "getDummy", Type.create(myServiceClazz.name(), Kind.CLASS), PrimitiveType.INT,
            Type.create(DotName.createSimple(String.class.getName()), Kind.CLASS));
    extensionMethodGenerator.generate(extensionMethod, null, null);
    extensionMethod = index.getClassByName(DotName.createSimple(MyService.class.getName())).method(
            "getDummy", Type.create(myServiceClazz.name(), Kind.CLASS), PrimitiveType.INT,
            PrimitiveType.LONG);
    extensionMethodGenerator.generate(extensionMethod, null, null);
    extensionMethod = index.getClassByName(DotName.createSimple(MyService.class.getName())).method(
            "getDummyVarargs", Type.create(myServiceClazz.name(), Kind.CLASS), PrimitiveType.INT,
            Type.create(DotName.createSimple("[L" + String.class.getName() + ";"), Kind.ARRAY));
    extensionMethodGenerator.generate(extensionMethod, null, null);
    generatedTypes.addAll(extensionMethodGenerator.getGeneratedTypes());
}
 
Example 9
Source File: BeanInfoTypesTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolver() throws IOException {

    Index index = index(Foo.class, Bar.class, FooQualifier.class, AbstractList.class, AbstractCollection.class,
            Collection.class, List.class,
            Iterable.class, Object.class, String.class);

    BeanDeployment deployment = BeanProcessor.builder().setIndex(index).build().getBeanDeployment();
    DotName fooName = name(Foo.class);

    ClassInfo fooClass = index.getClassByName(fooName);
    BeanInfo fooBean = Beans.createClassBean(fooClass, deployment, null);
    Set<Type> types = fooBean.getTypes();
    // Foo, AbstractList<String>, AbstractCollection<String>, List<String>, Collection<String>, Iterable<String>, Object
    assertEquals(7, types.size());
    assertTrue(types.contains(Type.create(fooName, Kind.CLASS)));
    assertTrue(types.contains(ParameterizedType.create(name(AbstractList.class),
            new Type[] { Type.create(name(String.class), Kind.CLASS) }, null)));
    assertTrue(types.contains(
            ParameterizedType.create(name(List.class), new Type[] { Type.create(name(String.class), Kind.CLASS) }, null)));
    assertTrue(types.contains(ParameterizedType.create(name(Collection.class),
            new Type[] { Type.create(name(String.class), Kind.CLASS) }, null)));
    assertTrue(types.contains(ParameterizedType.create(name(AbstractCollection.class),
            new Type[] { Type.create(name(String.class), Kind.CLASS) }, null)));
    assertTrue(types.contains(ParameterizedType.create(name(Iterable.class),
            new Type[] { Type.create(name(String.class), Kind.CLASS) }, null)));

}
 
Example 10
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_SubjectSameAsObject() {
    final Class<?> subjectClass = MapContainer.class;
    final DotName subjectName = DotName.createSimple(subjectClass.getName());
    Index index = indexOf(subjectClass);
    ClassInfo subjectInfo = index.getClassByName(subjectName);
    Type testSubject = subjectInfo.field("theMap").type();
    boolean result = TypeUtil.isA(index, testSubject, TYPE_MAP);
    assertTrue(result);
}
 
Example 11
Source File: JandexUtilTests.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnumValue() {
    Index index = IndexScannerTestBase.indexOf(Implementor2.class);
    ClassInfo clazz = index.getClassByName(DotName.createSimple(Implementor2.class.getName()));
    AnnotationInstance annotation = clazz.method("getData")
            .annotation(DotName.createSimple(APIResponse.class.getName()))
            .value("content")
            .asNestedArray()[0]
                    .value("encoding")
                    .asNestedArray()[0];
    Encoding.Style style = JandexUtil.enumValue(annotation, "style", Encoding.Style.class);
    assertEquals(Encoding.Style.PIPE_DELIMITED, style);
}
 
Example 12
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 13
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 14
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 15
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 16
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 17
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 18
Source File: BeanInfoInjectionsTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void testInjections() throws IOException {

    Index index = index(Foo.class, Bar.class, FooQualifier.class, AbstractList.class, AbstractCollection.class,
            Collection.class, List.class,
            Iterable.class, Object.class, String.class);
    DotName barName = name(Bar.class);
    ClassInfo barClass = index.getClassByName(barName);
    Type fooType = Type.create(name(Foo.class), Kind.CLASS);
    Type listStringType = ParameterizedType.create(name(List.class),
            new Type[] { Type.create(name(String.class), Kind.CLASS) }, null);

    BeanDeployment deployment = BeanProcessor.builder().setIndex(index).build().getBeanDeployment();
    deployment.registerCustomContexts(Collections.emptyList());
    deployment.registerBeans(Collections.emptyList());
    BeanInfo barBean = deployment.getBeans().stream().filter(b -> b.getTarget().get().equals(barClass)).findFirst().get();
    List<Injection> injections = barBean.getInjections();
    assertEquals(3, injections.size());
    for (Injection injection : injections) {
        if (injection.target.kind().equals(org.jboss.jandex.AnnotationTarget.Kind.FIELD)
                && injection.target.asField().name().equals("foo")) {
            assertEquals(1, injection.injectionPoints.size());
            assertEquals(fooType, injection.injectionPoints.get(0).getRequiredType());
            assertEquals(1, injection.injectionPoints.get(0).getRequiredQualifiers().size());
        } else if (injection.target.kind().equals(org.jboss.jandex.AnnotationTarget.Kind.METHOD)
                && injection.target.asMethod().name().equals("<init>")) {
            // Constructor
            assertEquals(2, injection.injectionPoints.size());
            assertEquals(listStringType, injection.injectionPoints.get(0).getRequiredType());
            assertEquals(fooType, injection.injectionPoints.get(1).getRequiredType());
            assertEquals(1, injection.injectionPoints.get(1).getRequiredQualifiers().size());
        } else if (injection.target.kind().equals(org.jboss.jandex.AnnotationTarget.Kind.METHOD)
                && injection.target.asMethod().name().equals("init")) {
            // Initializer
            assertEquals(2, injection.injectionPoints.size());
            assertEquals(listStringType, injection.injectionPoints.get(1).getRequiredType());
            assertEquals(fooType, injection.injectionPoints.get(0).getRequiredType());
        } else {
            fail();
        }

    }

}
 
Example 19
Source File: QuarkusSecurityJpaProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void setupRoles(Index index, JpaSecurityDefinition jpaSecurityDefinition, Set<String> panacheClasses, String name,
        MethodCreator methodCreator, AssignableResultHandle userVar, AssignableResultHandle builderVar) {
    ResultHandle role = jpaSecurityDefinition.roles.readValue(methodCreator, userVar);
    // role: user.getRole()
    boolean handledRole = false;
    Type rolesType = jpaSecurityDefinition.roles.type();
    switch (rolesType.kind()) {
        case ARRAY:
            // FIXME: support non-JPA-backed array roles?
            break;
        case CLASS:
            if (rolesType.name().equals(DOTNAME_STRING)) {
                // addRoles(builder, :role)
                methodCreator.invokeVirtualMethod(
                        MethodDescriptor.ofMethod(name, "addRoles", void.class,
                                QuarkusSecurityIdentity.Builder.class, String.class),
                        methodCreator.getThis(),
                        builderVar,
                        role);
                handledRole = true;
            }
            break;
        case PARAMETERIZED_TYPE:
            DotName roleType = rolesType.name();
            if (roleType.equals(DOTNAME_LIST)
                    || roleType.equals(DOTNAME_COLLECTION)
                    || roleType.equals(DOTNAME_SET)) {
                Type elementType = rolesType.asParameterizedType().arguments().get(0);
                String elementClassName = elementType.name().toString();
                String elementClassTypeDescriptor = "L" + elementClassName.replace('.', '/') + ";";
                FieldOrMethod rolesFieldOrMethod;
                if (!elementType.name().equals(DOTNAME_STRING)) {
                    ClassInfo roleClass = index.getClassByName(elementType.name());
                    if (roleClass == null) {
                        throw new RuntimeException(
                                "The role element type must be indexed by Jandex: " + elementType);
                    }
                    AnnotationTarget annotatedRolesValue = getSingleAnnotatedElement(index, DOTNAME_ROLES_VALUE);
                    rolesFieldOrMethod = JpaSecurityDefinition.getFieldOrMethod(index, roleClass,
                            annotatedRolesValue, isPanache(roleClass, panacheClasses));
                    if (rolesFieldOrMethod == null) {
                        throw new RuntimeException(
                                "Missing @RoleValue annotation on (non-String) role element type: " + elementType);
                    }
                } else {
                    rolesFieldOrMethod = null;
                }
                // for(:elementType roleElement : :role){
                //    ret.addRoles(:role.roleField);
                //    // or for String collections:
                //    ret.addRoles(:role);
                // }
                foreach(methodCreator, role, elementClassTypeDescriptor, (creator, var) -> {
                    ResultHandle roleElement;
                    if (rolesFieldOrMethod != null) {
                        roleElement = rolesFieldOrMethod.readValue(creator, var);
                    } else {
                        roleElement = var;
                    }
                    creator.invokeVirtualMethod(
                            MethodDescriptor.ofMethod(name, "addRoles", void.class,
                                    QuarkusSecurityIdentity.Builder.class, String.class),
                            methodCreator.getThis(),
                            builderVar,
                            roleElement);
                });
                handledRole = true;
            }
            break;
    }
    if (!handledRole) {
        throw new RuntimeException("Unsupported @Roles field/getter type: " + rolesType);
    }

    // return builder.build()
    methodCreator.returnValue(methodCreator.invokeVirtualMethod(
            MethodDescriptor.ofMethod(QuarkusSecurityIdentity.Builder.class,
                    "build",
                    QuarkusSecurityIdentity.class),
            builderVar));
}