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

The following examples show how to use org.jboss.jandex.ClassInfo#enclosingClass() . 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: ConfigBuildStep.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private String getPropertyName(String name, ClassInfo declaringClass) {
    StringBuilder builder = new StringBuilder();
    if (declaringClass.enclosingClass() == null) {
        builder.append(declaringClass.name());
    } else {
        builder.append(declaringClass.enclosingClass()).append(".").append(declaringClass.simpleName());
    }
    return builder.append(".").append(name).toString();
}
 
Example 2
Source File: JandexUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a type for a ClassInfo
 */
private static Type getType(ClassInfo inputClassInfo, IndexView index) {
    List<TypeVariable> typeParameters = inputClassInfo.typeParameters();
    if (typeParameters.isEmpty())
        return ClassType.create(inputClassInfo.name(), Kind.CLASS);
    Type owner = null;
    // ignore owners for non-static classes
    if (inputClassInfo.enclosingClass() != null && !Modifier.isStatic(inputClassInfo.flags())) {
        owner = getType(fetchFromIndex(inputClassInfo.enclosingClass(), index), index);
    }
    return ParameterizedType.create(inputClassInfo.name(), typeParameters.toArray(new Type[0]), owner);
}
 
Example 3
Source File: InterceptorGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param interceptor bean
 * @return a collection of resources
 */
Collection<Resource> generate(InterceptorInfo interceptor) {

    Type providerType = interceptor.getProviderType();
    ClassInfo interceptorClass = interceptor.getTarget().get().asClass();
    String baseName;
    if (interceptorClass.enclosingClass() != null) {
        baseName = DotNames.simpleName(interceptorClass.enclosingClass()) + "_" + DotNames.simpleName(interceptorClass);
    } else {
        baseName = DotNames.simpleName(interceptorClass);
    }
    ClassInfo providerClass = getClassByName(interceptor.getDeployment().getIndex(), providerType.name());
    String providerTypeName = providerClass.name().toString();
    String targetPackage = DotNames.packageName(providerType.name());
    String generatedName = generatedNameFromTarget(targetPackage, baseName, BEAN_SUFFIX);
    beanToGeneratedName.put(interceptor, generatedName);
    if (existingClasses.contains(generatedName)) {
        return Collections.emptyList();
    }

    boolean isApplicationClass = applicationClassPredicate.test(interceptor.getBeanClass());
    ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
            name -> name.equals(generatedName) ? SpecialType.INTERCEPTOR_BEAN : null, generateSources);

    // MyInterceptor_Bean implements InjectableInterceptor<T>
    ClassCreator interceptorCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName)
            .interfaces(InjectableInterceptor.class, Supplier.class)
            .build();

    // Fields
    FieldCreator beanTypes = interceptorCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class)
            .setModifiers(ACC_PRIVATE | ACC_FINAL);
    FieldCreator bindings = interceptorCreator.getFieldCreator(FIELD_NAME_BINDINGS, Set.class)
            .setModifiers(ACC_PRIVATE | ACC_FINAL);

    Map<InjectionPointInfo, String> injectionPointToProviderField = new HashMap<>();
    Map<InterceptorInfo, String> interceptorToProviderField = new HashMap<>();
    initMaps(interceptor, injectionPointToProviderField, interceptorToProviderField);

    createProviderFields(interceptorCreator, interceptor, injectionPointToProviderField, interceptorToProviderField);
    createConstructor(classOutput, interceptorCreator, interceptor, baseName, injectionPointToProviderField,
            interceptorToProviderField, bindings.getFieldDescriptor(), reflectionRegistration);

    implementGetIdentifier(interceptor, interceptorCreator);
    implementSupplierGet(interceptorCreator);
    implementCreate(classOutput, interceptorCreator, interceptor, providerTypeName, baseName, injectionPointToProviderField,
            interceptorToProviderField,
            reflectionRegistration, targetPackage, isApplicationClass);
    implementGet(interceptor, interceptorCreator, providerTypeName, baseName);
    implementGetTypes(interceptorCreator, beanTypes.getFieldDescriptor());
    implementGetBeanClass(interceptor, interceptorCreator);
    // Interceptors are always @Dependent and have always default qualifiers

    // InjectableInterceptor methods
    implementGetInterceptorBindings(interceptorCreator, bindings.getFieldDescriptor());
    implementIntercepts(interceptorCreator, interceptor);
    implementIntercept(interceptorCreator, interceptor, providerTypeName, reflectionRegistration, isApplicationClass);
    implementGetPriority(interceptorCreator, interceptor);

    interceptorCreator.close();
    return classOutput.getResources();

}
 
Example 4
Source File: BeanGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
Collection<Resource> generateClassBean(BeanInfo bean, ClassInfo beanClass) {

        String baseName;
        if (beanClass.enclosingClass() != null) {
            baseName = DotNames.simpleName(beanClass.enclosingClass()) + UNDERSCORE + DotNames.simpleName(beanClass);
        } else {
            baseName = DotNames.simpleName(beanClass);
        }
        Type providerType = bean.getProviderType();
        String providerTypeName = providerType.name().toString();
        String targetPackage = DotNames.packageName(providerType.name());
        String generatedName = generatedNameFromTarget(targetPackage, baseName, BEAN_SUFFIX);
        beanToGeneratedName.put(bean, generatedName);
        if (existingClasses.contains(generatedName)) {
            return Collections.emptyList();
        }

        boolean isApplicationClass = applicationClassPredicate.test(beanClass.name());
        ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
                name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);

        // Foo_Bean implements InjectableBean<T>
        ClassCreator beanCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName)
                .interfaces(InjectableBean.class, Supplier.class).build();

        // Fields
        FieldCreator beanTypes = beanCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class)
                .setModifiers(ACC_PRIVATE | ACC_FINAL);
        FieldCreator qualifiers = null;
        if (!bean.getQualifiers().isEmpty() && !bean.hasDefaultQualifiers()) {
            qualifiers = beanCreator.getFieldCreator(FIELD_NAME_QUALIFIERS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
        }
        if (bean.getScope().isNormal()) {
            // For normal scopes a client proxy is generated too
            initializeProxy(bean, baseName, beanCreator);
        }
        FieldCreator stereotypes = null;
        if (!bean.getStereotypes().isEmpty()) {
            stereotypes = beanCreator.getFieldCreator(FIELD_NAME_STEREOTYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
        }

        Map<InjectionPointInfo, String> injectionPointToProviderSupplierField = new HashMap<>();
        Map<InterceptorInfo, String> interceptorToProviderSupplierField = new HashMap<>();
        initMaps(bean, injectionPointToProviderSupplierField, interceptorToProviderSupplierField);

        createProviderFields(beanCreator, bean, injectionPointToProviderSupplierField, interceptorToProviderSupplierField);
        createConstructor(classOutput, beanCreator, bean, baseName, injectionPointToProviderSupplierField,
                interceptorToProviderSupplierField,
                annotationLiterals, reflectionRegistration);

        implementGetIdentifier(bean, beanCreator);
        implementSupplierGet(beanCreator);
        if (!bean.hasDefaultDestroy()) {
            implementDestroy(bean, beanCreator, providerTypeName, injectionPointToProviderSupplierField, reflectionRegistration,
                    isApplicationClass, baseName);
        }
        implementCreate(classOutput, beanCreator, bean, providerTypeName, baseName, injectionPointToProviderSupplierField,
                interceptorToProviderSupplierField,
                reflectionRegistration, targetPackage, isApplicationClass);
        implementGet(bean, beanCreator, providerTypeName, baseName);

        implementGetTypes(beanCreator, beanTypes.getFieldDescriptor());
        if (!BuiltinScope.isDefault(bean.getScope())) {
            implementGetScope(bean, beanCreator);
        }
        if (qualifiers != null) {
            implementGetQualifiers(bean, beanCreator, qualifiers.getFieldDescriptor());
        }
        if (bean.isAlternative()) {
            implementGetAlternativePriority(bean, beanCreator);
        }
        if (stereotypes != null) {
            implementGetStereotypes(bean, beanCreator, stereotypes.getFieldDescriptor());
        }
        implementGetBeanClass(bean, beanCreator);
        implementGetName(bean, beanCreator);
        if (bean.isDefaultBean()) {
            implementIsDefaultBean(bean, beanCreator);
        }

        beanCreator.close();
        return classOutput.getResources();
    }
 
Example 5
Source File: BeanGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
Collection<Resource> generateProducerMethodBean(BeanInfo bean, MethodInfo producerMethod) {

        ClassInfo declaringClass = producerMethod.declaringClass();
        String declaringClassBase;
        if (declaringClass.enclosingClass() != null) {
            declaringClassBase = DotNames.simpleName(declaringClass.enclosingClass()) + UNDERSCORE
                    + DotNames.simpleName(declaringClass);
        } else {
            declaringClassBase = DotNames.simpleName(declaringClass);
        }

        Type providerType = bean.getProviderType();
        StringBuilder sigBuilder = new StringBuilder();
        sigBuilder.append(producerMethod.name())
                .append(UNDERSCORE)
                .append(producerMethod.returnType().name().toString());

        for (Type i : producerMethod.parameters()) {
            sigBuilder.append(i.name().toString());
        }

        String baseName = declaringClassBase + PRODUCER_METHOD_SUFFIX + UNDERSCORE + producerMethod.name() + UNDERSCORE
                + Hashes.sha1(sigBuilder.toString());
        String providerTypeName = providerType.name().toString();
        String targetPackage = DotNames.packageName(declaringClass.name());
        String generatedName = generatedNameFromTarget(targetPackage, baseName, BEAN_SUFFIX);
        beanToGeneratedName.put(bean, generatedName);
        if (existingClasses.contains(generatedName)) {
            return Collections.emptyList();
        }

        boolean isApplicationClass = applicationClassPredicate.test(declaringClass.name());
        ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
                name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);

        // Foo_Bean implements InjectableBean<T>
        ClassCreator beanCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName)
                .interfaces(InjectableBean.class, Supplier.class).build();

        // Fields
        FieldCreator beanTypes = beanCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class)
                .setModifiers(ACC_PRIVATE | ACC_FINAL);
        FieldCreator qualifiers = null;
        if (!bean.getQualifiers().isEmpty() && !bean.hasDefaultQualifiers()) {
            qualifiers = beanCreator.getFieldCreator(FIELD_NAME_QUALIFIERS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
        }
        if (bean.getScope().isNormal()) {
            // For normal scopes a client proxy is generated too
            initializeProxy(bean, baseName, beanCreator);
        }
        FieldCreator stereotypes = null;
        if (!bean.getStereotypes().isEmpty()) {
            stereotypes = beanCreator.getFieldCreator(FIELD_NAME_STEREOTYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
        }

        Map<InjectionPointInfo, String> injectionPointToProviderField = new HashMap<>();
        // Producer methods are not intercepted
        initMaps(bean, injectionPointToProviderField, null);

        createProviderFields(beanCreator, bean, injectionPointToProviderField, Collections.emptyMap());
        createConstructor(classOutput, beanCreator, bean, baseName, injectionPointToProviderField, Collections.emptyMap(),
                annotationLiterals, reflectionRegistration);

        implementGetIdentifier(bean, beanCreator);
        implementSupplierGet(beanCreator);
        if (!bean.hasDefaultDestroy()) {
            implementDestroy(bean, beanCreator, providerTypeName, injectionPointToProviderField, reflectionRegistration,
                    isApplicationClass, baseName);
        }
        implementCreate(classOutput, beanCreator, bean, providerTypeName, baseName, injectionPointToProviderField,
                Collections.emptyMap(),
                reflectionRegistration, targetPackage, isApplicationClass);
        implementGet(bean, beanCreator, providerTypeName, baseName);

        implementGetTypes(beanCreator, beanTypes.getFieldDescriptor());
        if (!BuiltinScope.isDefault(bean.getScope())) {
            implementGetScope(bean, beanCreator);
        }
        if (qualifiers != null) {
            implementGetQualifiers(bean, beanCreator, qualifiers.getFieldDescriptor());
        }
        if (bean.isAlternative()) {
            implementGetAlternativePriority(bean, beanCreator);
        }
        implementGetDeclaringBean(beanCreator);
        if (stereotypes != null) {
            implementGetStereotypes(bean, beanCreator, stereotypes.getFieldDescriptor());
        }
        implementGetBeanClass(bean, beanCreator);
        implementGetName(bean, beanCreator);
        if (bean.isDefaultBean()) {
            implementIsDefaultBean(bean, beanCreator);
        }
        implementGetKind(beanCreator, InjectableBean.Kind.PRODUCER_METHOD);

        beanCreator.close();
        return classOutput.getResources();
    }
 
Example 6
Source File: BeanGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
Collection<Resource> generateProducerFieldBean(BeanInfo bean, FieldInfo producerField) {

        ClassInfo declaringClass = producerField.declaringClass();
        String declaringClassBase;
        if (declaringClass.enclosingClass() != null) {
            declaringClassBase = DotNames.simpleName(declaringClass.enclosingClass()) + UNDERSCORE
                    + DotNames.simpleName(declaringClass);
        } else {
            declaringClassBase = DotNames.simpleName(declaringClass);
        }

        Type providerType = bean.getProviderType();
        String baseName = declaringClassBase + PRODUCER_FIELD_SUFFIX + UNDERSCORE + producerField.name();
        String providerTypeName = providerType.name().toString();
        String targetPackage = DotNames.packageName(declaringClass.name());
        String generatedName = generatedNameFromTarget(targetPackage, baseName, BEAN_SUFFIX);
        beanToGeneratedName.put(bean, generatedName);
        if (existingClasses.contains(generatedName)) {
            return Collections.emptyList();
        }

        boolean isApplicationClass = applicationClassPredicate.test(declaringClass.name());
        ResourceClassOutput classOutput = new ResourceClassOutput(isApplicationClass,
                name -> name.equals(generatedName) ? SpecialType.BEAN : null, generateSources);

        // Foo_Bean implements InjectableBean<T>
        ClassCreator beanCreator = ClassCreator.builder().classOutput(classOutput).className(generatedName)
                .interfaces(InjectableBean.class, Supplier.class).build();

        // Fields
        FieldCreator beanTypes = beanCreator.getFieldCreator(FIELD_NAME_BEAN_TYPES, Set.class)
                .setModifiers(ACC_PRIVATE | ACC_FINAL);
        FieldCreator qualifiers = null;
        if (!bean.getQualifiers().isEmpty() && !bean.hasDefaultQualifiers()) {
            qualifiers = beanCreator.getFieldCreator(FIELD_NAME_QUALIFIERS, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
        }
        if (bean.getScope().isNormal()) {
            // For normal scopes a client proxy is generated too
            initializeProxy(bean, baseName, beanCreator);
        }
        FieldCreator stereotypes = null;
        if (!bean.getStereotypes().isEmpty()) {
            stereotypes = beanCreator.getFieldCreator(FIELD_NAME_STEREOTYPES, Set.class).setModifiers(ACC_PRIVATE | ACC_FINAL);
        }

        createProviderFields(beanCreator, bean, Collections.emptyMap(), Collections.emptyMap());
        createConstructor(classOutput, beanCreator, bean, baseName, Collections.emptyMap(), Collections.emptyMap(),
                annotationLiterals, reflectionRegistration);

        implementGetIdentifier(bean, beanCreator);
        implementSupplierGet(beanCreator);
        if (!bean.hasDefaultDestroy()) {
            implementDestroy(bean, beanCreator, providerTypeName, null, reflectionRegistration, isApplicationClass, baseName);
        }
        implementCreate(classOutput, beanCreator, bean, providerTypeName, baseName, Collections.emptyMap(),
                Collections.emptyMap(), reflectionRegistration,
                targetPackage, isApplicationClass);
        implementGet(bean, beanCreator, providerTypeName, baseName);

        implementGetTypes(beanCreator, beanTypes.getFieldDescriptor());
        if (!BuiltinScope.isDefault(bean.getScope())) {
            implementGetScope(bean, beanCreator);
        }
        if (qualifiers != null) {
            implementGetQualifiers(bean, beanCreator, qualifiers.getFieldDescriptor());
        }
        if (bean.isAlternative()) {
            implementGetAlternativePriority(bean, beanCreator);
        }
        implementGetDeclaringBean(beanCreator);
        if (stereotypes != null) {
            implementGetStereotypes(bean, beanCreator, stereotypes.getFieldDescriptor());
        }
        implementGetBeanClass(bean, beanCreator);
        implementGetName(bean, beanCreator);
        if (bean.isDefaultBean()) {
            implementIsDefaultBean(bean, beanCreator);
        }
        implementGetKind(beanCreator, InjectableBean.Kind.PRODUCER_FIELD);

        beanCreator.close();
        return classOutput.getResources();
    }
 
Example 7
Source File: ExtensionMethodGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public void generate(MethodInfo method, String matchName, Integer priority) {

        // Validate the method first
        validate(method);
        ClassInfo declaringClass = method.declaringClass();
        AnnotationInstance extensionAnnotation = method.annotation(TEMPLATE_EXTENSION);

        if (matchName == null && extensionAnnotation != null) {
            // No explicit name defined, try annotation
            AnnotationValue matchNameValue = extensionAnnotation.value(MATCH_NAME);
            if (matchNameValue != null) {
                matchName = matchNameValue.asString();
            }
        }
        if (matchName == null || matchName.equals(TemplateExtension.METHOD_NAME)) {
            matchName = method.name();
        }
        List<Type> parameters = method.parameters();
        if (matchName.equals(TemplateExtension.ANY)) {
            // Special constant used - the second parameter must be a string
            if (parameters.size() < 2 || !parameters.get(1).name().equals(STRING)) {
                throw new IllegalStateException(
                        "Template extension method matching multiple names must declare at least two parameters and the second parameter must be string: "
                                + method);
            }
        }

        if (priority == null && extensionAnnotation != null) {
            // No explicit priority set, try annotation
            AnnotationValue priorityValue = extensionAnnotation.value(PRIORITY);
            if (priorityValue != null) {
                priority = priorityValue.asInt();
            }
        }
        if (priority == null) {
            priority = TemplateExtension.DEFAULT_PRIORITY;
        }

        String baseName;
        if (declaringClass.enclosingClass() != null) {
            baseName = simpleName(declaringClass.enclosingClass()) + ValueResolverGenerator.NESTED_SEPARATOR
                    + simpleName(declaringClass);
        } else {
            baseName = simpleName(declaringClass);
        }
        String targetPackage = packageName(declaringClass.name());

        String suffix = SUFFIX + "_" + method.name() + "_" + sha1(parameters.toString());
        String generatedName = generatedNameFromTarget(targetPackage, baseName, suffix);
        generatedTypes.add(generatedName.replace('/', '.'));

        ClassCreator valueResolver = ClassCreator.builder().classOutput(classOutput).className(generatedName)
                .interfaces(ValueResolver.class).build();

        implementGetPriority(valueResolver, priority);
        implementAppliesTo(valueResolver, method, matchName);
        implementResolve(valueResolver, declaringClass, method, matchName);

        valueResolver.close();
    }
 
Example 8
Source File: ValueResolverGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public void generate(ClassInfo clazz) {
    Objects.requireNonNull(clazz);
    String clazzName = clazz.name().toString();
    if (analyzedTypes.contains(clazzName)) {
        return;
    }
    analyzedTypes.add(clazzName);
    boolean ignoreSuperclasses = false;

    // @TemplateData declared on class takes precedence
    AnnotationInstance templateData = clazz.classAnnotation(TEMPLATE_DATA);
    if (templateData == null) {
        // Try to find @TemplateData declared on other classes
        templateData = uncontrolled.get(clazz.name());
    } else {
        AnnotationValue ignoreSuperclassesValue = templateData.value(IGNORE_SUPERCLASSES);
        if (ignoreSuperclassesValue != null) {
            ignoreSuperclasses = ignoreSuperclassesValue.asBoolean();
        }
    }

    Predicate<AnnotationTarget> filters = initFilters(templateData);

    LOGGER.debugf("Analyzing %s", clazzName);

    String baseName;
    if (clazz.enclosingClass() != null) {
        baseName = simpleName(clazz.enclosingClass()) + NESTED_SEPARATOR + simpleName(clazz);
    } else {
        baseName = simpleName(clazz);
    }
    String targetPackage = packageName(clazz.name());
    String generatedName = generatedNameFromTarget(targetPackage, baseName, SUFFIX);
    generatedTypes.add(generatedName.replace('/', '.'));

    ClassCreator valueResolver = ClassCreator.builder().classOutput(classOutput).className(generatedName)
            .interfaces(ValueResolver.class).build();

    implementGetPriority(valueResolver);
    implementAppliesTo(valueResolver, clazz);
    implementResolve(valueResolver, clazzName, clazz, filters);

    valueResolver.close();

    DotName superName = clazz.superName();
    if (!ignoreSuperclasses && (superName != null && !superName.equals(OBJECT))) {
        ClassInfo superClass = index.getClassByName(clazz.superClassType().name());
        if (superClass != null) {
            generate(superClass);
        } else {
            LOGGER.warnf("Skipping super class %s - not found in the index", clazz.superClassType());
        }
    }
}