Java Code Examples for org.jboss.jandex.Type#create()

The following examples show how to use org.jboss.jandex.Type#create() . 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: AnnotationScanner.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts all methods from the provided class and its ancestors that are known to the instance's index
 * 
 * @param context the scanning context
 * @param resource the resource class
 * @return all methods from the provided class and its ancestors
 */
default List<MethodInfo> getResourceMethods(final AnnotationScannerContext context, ClassInfo resource) {
    Type resourceType = Type.create(resource.name(), Type.Kind.CLASS);
    Map<ClassInfo, Type> chain = JandexUtil.inheritanceChain(context.getIndex(), resource, resourceType);
    List<MethodInfo> methods = new ArrayList<>();

    for (ClassInfo classInfo : chain.keySet()) {
        methods.addAll(classInfo.methods());

        classInfo.interfaceTypes()
                .stream()
                .map(iface -> context.getIndex().getClassByName(TypeUtil.getName(iface)))
                .filter(Objects::nonNull)
                .flatMap(iface -> iface.methods().stream())
                .forEach(methods::add);
    }

    return methods;
}
 
Example 2
Source File: TypeResolverTests.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotatedInterfaceMethodOverridesStaticField() {
    AugmentedIndexView index = new AugmentedIndexView(indexOf(AbstractAnimal.class,
            Reptile.class,
            Lizard.class));

    ClassInfo leafKlazz = index.getClassByName(componentize(Lizard.class.getName()));
    Type leaf = Type.create(leafKlazz.name(), Type.Kind.CLASS);
    Map<String, TypeResolver> properties = TypeResolver.getAllFields(index, leaf, leafKlazz);

    TypeResolver resolver = properties.get("scaleColor");
    assertEquals(Kind.METHOD, resolver.getAnnotationTarget().kind());
    AnnotationInstance schema = TypeUtil.getSchemaAnnotation(resolver.getAnnotationTarget());
    assertEquals("scaleColor", schema.value("name").asString());
    assertNull(schema.value("deprecated"));
    assertEquals("The color of a reptile's scales", schema.value("description").asString());

    TypeResolver ageResolver = properties.get("age");
    assertEquals(Type.Kind.CLASS, ageResolver.getUnresolvedType().kind());
    assertEquals(DotName.createSimple(String.class.getName()), ageResolver.getUnresolvedType().name());
}
 
Example 3
Source File: BeanInfo.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private Type initProviderType(AnnotationTarget target, ClassInfo implClazz) {
    if (target != null) {
        switch (target.kind()) {
            case CLASS:
                return Types.getProviderType(target.asClass());
            case FIELD:
                return target.asField().type();
            case METHOD:
                return target.asMethod().returnType();
            default:
                break;
        }
    } else if (implClazz != null) {
        return Type.create(implClazz.name(), org.jboss.jandex.Type.Kind.CLASS);
    }
    throw new IllegalStateException("Cannot infer the provider type");
}
 
Example 4
Source File: TypeResolverTests.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotatedMethodOverridesParentSchema() {
    AugmentedIndexView index = new AugmentedIndexView(indexOf(AbstractAnimal.class,
            Feline.class,
            Cat.class));

    ClassInfo leafKlazz = index.getClassByName(componentize(Cat.class.getName()));
    Type leaf = Type.create(leafKlazz.name(), Type.Kind.CLASS);
    Map<String, TypeResolver> properties = TypeResolver.getAllFields(index, leaf, leafKlazz);
    TypeResolver resolver = properties.get("type");
    assertEquals(Kind.METHOD, resolver.getAnnotationTarget().kind());
    AnnotationInstance schema = TypeUtil.getSchemaAnnotation(resolver.getAnnotationTarget());
    assertEquals("type", schema.value("name").asString());
    assertEquals(false, schema.value("required").asBoolean());
    assertEquals("Cat", schema.value("example").asString());
    assertArrayEquals(new String[] { "age", "type", "name", "extinct" },
            properties.values().stream().map(TypeResolver::getPropertyName).toArray());
}
 
Example 5
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_SubjectIsJavaLangObject() {
    final Class<?> subjectClass = Object.class;
    Index index = indexOf(subjectClass);
    Type testSubject = Type.create(DotName.createSimple(subjectClass.getName()), Type.Kind.CLASS);
    boolean result = TypeUtil.isA(index, testSubject, TYPE_COLLECTION);
    assertFalse(result);
}
 
Example 6
Source File: TypeResolverTests.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testJaxbCustomPropertyOrder() {
    AugmentedIndexView index = new AugmentedIndexView(indexOf(JaxbCustomPropertyOrder.class));
    ClassInfo leafKlazz = index.getClassByName(componentize(JaxbCustomPropertyOrder.class.getName()));
    Type leaf = Type.create(leafKlazz.name(), Type.Kind.CLASS);
    Map<String, TypeResolver> properties = TypeResolver.getAllFields(index, leaf, leafKlazz);
    assertEquals(4, properties.size());
    Iterator<Entry<String, TypeResolver>> iter = properties.entrySet().iterator();
    assertEquals("theName", iter.next().getValue().getPropertyName());
    assertEquals("comment2ActuallyFirst", iter.next().getValue().getPropertyName());
    assertEquals("comment", iter.next().getValue().getPropertyName());
    assertEquals("name2", iter.next().getValue().getPropertyName());
}
 
Example 7
Source File: TypeResolverTests.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testJacksonPropertyOrderDefault() {
    AugmentedIndexView index = new AugmentedIndexView(indexOf(JacksonPropertyOrderDefault.class));
    ClassInfo leafKlazz = index.getClassByName(componentize(JacksonPropertyOrderDefault.class.getName()));
    Type leaf = Type.create(leafKlazz.name(), Type.Kind.CLASS);
    Map<String, TypeResolver> properties = TypeResolver.getAllFields(index, leaf, leafKlazz);
    assertEquals(4, properties.size());
    Iterator<Entry<String, TypeResolver>> iter = properties.entrySet().iterator();
    assertEquals("comment", iter.next().getValue().getPropertyName());
    assertEquals("theName", iter.next().getValue().getPropertyName());
}
 
Example 8
Source File: SmallRyeMetricsProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Mark metric producer methods and fields as unremovable, they should be kept even if
 * there is no injection point for them.
 */
@BuildStep
void unremovableProducers(BuildProducer<UnremovableBeanBuildItem> unremovable) {
    Type type = Type.create(METRIC_INTERFACE, Type.Kind.CLASS);
    unremovable.produce(
            new UnremovableBeanBuildItem(new Predicate<io.quarkus.arc.processor.BeanInfo>() {
                @Override
                public boolean test(io.quarkus.arc.processor.BeanInfo beanInfo) {
                    io.quarkus.arc.processor.BeanInfo declaringBean = beanInfo.getDeclaringBean();
                    return (beanInfo.isProducerMethod() || beanInfo.isProducerField())
                            && beanInfo.getTypes().contains(type)
                            && !declaringBean.getBeanClass().toString().startsWith("io.smallrye.metrics");
                }
            }));
}
 
Example 9
Source File: Types.java    From quarkus with Apache License 2.0 5 votes vote down vote up
static Type getProviderType(ClassInfo classInfo) {
    List<TypeVariable> typeParameters = classInfo.typeParameters();
    if (!typeParameters.isEmpty()) {
        return ParameterizedType.create(classInfo.name(), typeParameters.toArray(new Type[] {}), null);
    } else {
        return Type.create(classInfo.name(), Kind.CLASS);
    }
}
 
Example 10
Source File: TypeResolverTests.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotatedFieldsOverridesInterfaceSchema() {
    AugmentedIndexView index = new AugmentedIndexView(indexOf(AbstractAnimal.class,
            Feline.class,
            Cat.class));

    ClassInfo leafKlazz = index.getClassByName(componentize(Cat.class.getName()));
    Type leaf = Type.create(leafKlazz.name(), Type.Kind.CLASS);
    Map<String, TypeResolver> properties = TypeResolver.getAllFields(index, leaf, leafKlazz);
    TypeResolver resolver = properties.get("name");
    assertEquals(Kind.FIELD, resolver.getAnnotationTarget().kind());
    AnnotationInstance schema = TypeUtil.getSchemaAnnotation(resolver.getAnnotationTarget());
    assertEquals(true, schema.value("required").asBoolean());
    assertEquals("Felix", schema.value("example").asString());
}
 
Example 11
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_SubjectSuperSameAsObject() {
    final Class<?> subjectClass = TestEnum.class;
    Index index = indexOf(subjectClass);
    Type testSubject = Type.create(DotName.createSimple(subjectClass.getName()), Type.Kind.CLASS);
    boolean result = TypeUtil.isA(index, testSubject, TYPE_ENUM);
    assertTrue(result);
}
 
Example 12
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_IndexedSubjectExtendsUnindexedCollection() {
    final Class<?> subjectClass = ChildCollection.class;
    Index index = indexOf(subjectClass);
    Type testSubject = Type.create(DotName.createSimple(subjectClass.getName()), Type.Kind.CLASS);
    boolean result = TypeUtil.isA(index, testSubject, TYPE_COLLECTION);
    assertTrue(result);
}
 
Example 13
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_IndexedSubjectImplementsOther() {
    final Class<?> subjectClass = CustomMap.class;
    Index index = indexOf(subjectClass);
    Type testSubject = Type.create(DotName.createSimple(subjectClass.getName()), Type.Kind.CLASS);
    boolean result = TypeUtil.isA(index, testSubject, TYPE_COLLECTION);
    assertFalse(result);
}
 
Example 14
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_IndexedSubjectImplementsObject() {
    final Class<?> subjectClass = CustomCollection.class;
    Index index = indexOf(subjectClass);
    Type testSubject = Type.create(DotName.createSimple(subjectClass.getName()), Type.Kind.CLASS);
    boolean result = TypeUtil.isA(index, testSubject, TYPE_COLLECTION);
    assertTrue(result);
}
 
Example 15
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_ObjectIndexed() {
    final Class<?> subjectClass = ArrayCollection.class;
    Index index = indexOf(Collection.class);
    Type testSubject = Type.create(DotName.createSimple(subjectClass.getName()), Type.Kind.CLASS);
    boolean result = TypeUtil.isA(index, testSubject, TYPE_COLLECTION);
    assertTrue(result);
}
 
Example 16
Source File: TypeInfos.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static Type resolveType(String value) {
    int angleIdx = value.indexOf(LEFT_ANGLE);
    if (angleIdx == -1) {
        return Type.create(DotName.createSimple(value), Kind.CLASS);
    } else {
        String name = value.substring(0, angleIdx);
        DotName rawName = DotName.createSimple(name);
        String[] parts = value.substring(angleIdx + 1, value.length() - 1).split(",");
        Type[] arguments = new Type[parts.length];
        for (int i = 0; i < arguments.length; i++) {
            arguments[i] = resolveType(parts[i]);
        }
        return ParameterizedType.create(rawName, arguments, null);
    }
}
 
Example 17
Source File: TypeUtilTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsA_BothIndexed() {
    final Class<?> subjectClass = ArrayCollection.class;
    Index index = indexOf(subjectClass, Collection.class);
    Type testSubject = Type.create(DotName.createSimple(subjectClass.getName()), Type.Kind.CLASS);
    boolean result = TypeUtil.isA(index, testSubject, TYPE_COLLECTION);
    assertTrue(result);
}
 
Example 18
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 19
Source File: JacksonProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void addReflectiveHierarchyClass(DotName className) {
    Type jandexType = Type.create(className, Type.Kind.CLASS);
    reflectiveHierarchyClass.produce(new ReflectiveHierarchyBuildItem(jandexType));
}
 
Example 20
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();
        }

    }

}