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

The following examples show how to use org.jboss.jandex.ClassInfo#superName() . 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: Beans.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static ScopeInfo inheritScope(ClassInfo beanClass, BeanDeployment beanDeployment) {
    DotName superClassName = beanClass.superName();
    while (!superClassName.equals(DotNames.OBJECT)) {
        ClassInfo classFromIndex = getClassByName(beanDeployment.getIndex(), superClassName);
        if (classFromIndex == null) {
            // class not in index
            LOGGER.warnf("Unable to determine scope for bean %s using inheritance because its super class " +
                    "%s is not part of Jandex index. Dependent scope will be used instead.", beanClass, superClassName);
            return null;
        }
        for (AnnotationInstance annotation : beanDeployment.getAnnotationStore().getAnnotations(classFromIndex)) {
            ScopeInfo scopeAnnotation = beanDeployment.getScope(annotation.name());
            if (scopeAnnotation != null && scopeAnnotation.declaresInherited()) {
                // found some scope, return
                return scopeAnnotation;
            }
        }
        superClassName = classFromIndex.superName();
    }
    // none found
    return null;
}
 
Example 2
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 3
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 4
Source File: FieldAccessImplementor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private FieldInfo getIdField(ClassInfo entityClass) {
    for (FieldInfo field : entityClass.fields()) {
        if (idFieldPredicate.test(field)) {
            return field;
        }
    }

    if (entityClass.superName() != null) {
        ClassInfo superClass = index.getClassByName(entityClass.superName());
        if (superClass != null) {
            return getIdField(superClass);
        }
    }

    return null;
}
 
Example 5
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 6
Source File: ResourcePropertiesAccessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private AnnotationInstance getAnnotation(ClassInfo classInfo) {
    if (classInfo.classAnnotation(RESOURCE_PROPERTIES_ANNOTATION) != null) {
        return classInfo.classAnnotation(RESOURCE_PROPERTIES_ANNOTATION);
    }
    if (classInfo.superName() != null) {
        ClassInfo superControllerInterface = index.getClassByName(classInfo.superName());
        if (superControllerInterface != null) {
            return getAnnotation(superControllerInterface);
        }
    }
    return null;
}
 
Example 7
Source File: MethodPropertiesAccessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private AnnotationInstance getAnnotation(ClassInfo resourceInterface, MethodMetadata methodMetadata) {
    Optional<MethodInfo> optionalMethod = getMethodInfo(resourceInterface, methodMetadata);
    if (optionalMethod.isPresent() && optionalMethod.get().hasAnnotation(OPERATION_PROPERTIES_ANNOTATION)) {
        return optionalMethod.get().annotation(OPERATION_PROPERTIES_ANNOTATION);
    }
    if (resourceInterface.superName() != null) {
        ClassInfo superResourceInterface = index.getClassByName(resourceInterface.superName());
        if (superResourceInterface != null) {
            return getAnnotation(superResourceInterface, methodMetadata);
        }
    }

    return null;
}
 
Example 8
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 9
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 10
Source File: SchedulerProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void collectScheduledMethods(IndexView index, AnnotationStore annotationStore, BeanInfo bean,
        ClassInfo beanClass, BuildProducer<ScheduledBusinessMethodItem> scheduledBusinessMethods) {

    for (MethodInfo method : beanClass.methods()) {
        List<AnnotationInstance> schedules = null;
        AnnotationInstance scheduledAnnotation = annotationStore.getAnnotation(method, SCHEDULED_NAME);
        if (scheduledAnnotation != null) {
            schedules = Collections.singletonList(scheduledAnnotation);
        } else {
            AnnotationInstance scheduledsAnnotation = annotationStore.getAnnotation(method, SCHEDULES_NAME);
            if (scheduledsAnnotation != null) {
                schedules = new ArrayList<>();
                for (AnnotationInstance scheduledInstance : scheduledsAnnotation.value().asNestedArray()) {
                    schedules.add(scheduledInstance);
                }
            }
        }
        if (schedules != null) {
            scheduledBusinessMethods.produce(new ScheduledBusinessMethodItem(bean, method, schedules));
            LOGGER.debugf("Found scheduled business method %s declared on %s", method, bean);
        }
    }

    DotName superClassName = beanClass.superName();
    if (superClassName != null) {
        ClassInfo superClass = index.getClassByName(superClassName);
        if (superClass != null) {
            collectScheduledMethods(index, annotationStore, bean, superClass, scheduledBusinessMethods);
        }
    }
}
 
Example 11
Source File: JandexBeanInfoAdapter.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public BeanInfo convert(ClassInfo input) {
    BeanInfo superClassInfo = null;
    DotName superName = input.superName();
    if (superName != null && indexView.getClassByName(superName) != null && !superName.equals(OBJECT)) {
        superClassInfo = this.convert(indexView.getClassByName(superName));
    }

    JandexAnnotationInfoAdapter annotationInfoAdapter = new JandexAnnotationInfoAdapter(indexView);

    // add all class-level annotations, including inherited - SmallRye expects them here
    List<AnnotationInfo> annotations = new ArrayList<>();
    ClassInfo clazz = input;
    while (clazz != null && clazz.superName() != null) {
        List<AnnotationInfo> annotationsSuper = clazz.classAnnotations()
                .stream()
                .filter(SmallRyeMetricsDotNames::isMetricAnnotation)
                .map(annotationInfoAdapter::convert)
                .collect(Collectors.toList());
        annotations.addAll(annotationsSuper);

        // a metric annotation can also be added through a CDI stereotype, so look into stereotypes
        List<AnnotationInfo> annotationsThroughStereotypes = clazz.classAnnotations()
                .stream()
                .flatMap(a -> getMetricAnnotationsThroughStereotype(a, indexView))
                .collect(Collectors.toList());
        annotations.addAll(annotationsThroughStereotypes);

        clazz = indexView.getClassByName(clazz.superName());
    }

    return new RawBeanInfo(input.simpleName(),
            input.name().prefix().toString(),
            annotations,
            superClassInfo);
}
 
Example 12
Source File: SmallRyeFaultToleranceProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private boolean hasFTAnnotations(IndexView index, AnnotationStore annotationStore, ClassInfo info) {
    if (info == null) {
        //should not happen, but guard against it
        //happens in this case due to a bug involving array types

        return false;
    }
    // first check annotations on type
    if (annotationStore.hasAnyAnnotation(info, FT_ANNOTATIONS)) {
        return true;
    }

    // then check on the methods
    for (MethodInfo method : info.methods()) {
        if (annotationStore.hasAnyAnnotation(method, FT_ANNOTATIONS)) {
            return true;
        }
    }

    // then check on the parent
    DotName parentClassName = info.superName();
    if (parentClassName == null || parentClassName.equals(DotNames.OBJECT)) {
        return false;
    }
    ClassInfo parentClassInfo = index.getClassByName(parentClassName);
    if (parentClassInfo == null) {
        return false;
    }
    return hasFTAnnotations(index, annotationStore, parentClassInfo);
}
 
Example 13
Source File: QuteProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to find a property with the specified name, ie. a public non-static non-synthetic field with the given name or a
 * public non-static non-synthetic method with no params and the given name.
 * 
 * @param name
 * @param clazz
 * @param index
 * @return the property or null
 */
private AnnotationTarget findProperty(String name, ClassInfo clazz, IndexView index) {
    while (clazz != null) {
        // Fields
        for (FieldInfo field : clazz.fields()) {
            if (Modifier.isPublic(field.flags()) && !Modifier.isStatic(field.flags())
                    && !ValueResolverGenerator.isSynthetic(field.flags()) && field.name().equals(name)) {
                return field;
            }
        }
        // Methods
        for (MethodInfo method : clazz.methods()) {
            if (Modifier.isPublic(method.flags()) && !Modifier.isStatic(method.flags())
                    && !ValueResolverGenerator.isSynthetic(method.flags()) && (method.name().equals(name)
                            || ValueResolverGenerator.getPropertyName(method.name()).equals(name))) {
                return method;
            }
        }
        DotName superName = clazz.superName();
        if (superName == null) {
            clazz = null;
        } else {
            clazz = index.getClassByName(clazz.superName());
        }
    }
    return null;
}
 
Example 14
Source File: Beans.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void collectCallbacks(ClassInfo clazz, List<MethodInfo> callbacks, DotName annotation, IndexView index) {
    for (MethodInfo method : clazz.methods()) {
        if (method.hasAnnotation(annotation) && method.returnType().kind() == Kind.VOID && method.parameters().isEmpty()) {
            callbacks.add(method);
        }
    }
    if (clazz.superName() != null) {
        ClassInfo superClass = getClassByName(index, clazz.superName());
        if (superClass != null) {
            collectCallbacks(superClass, callbacks, annotation, index);
        }
    }
}
 
Example 15
Source File: SmallRyeMetricsProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
AnnotationsTransformerBuildItem transformBeanScope(BeanArchiveIndexBuildItem index,
        CustomScopeAnnotationsBuildItem scopes) {
    return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

        @Override
        public int getPriority() {
            // this specifically should run after the JAX-RS AnnotationTransformers
            return BuildExtension.DEFAULT_PRIORITY - 100;
        }

        @Override
        public boolean appliesTo(AnnotationTarget.Kind kind) {
            return kind == Kind.CLASS;
        }

        @Override
        public void transform(TransformationContext ctx) {
            if (scopes.isScopeIn(ctx.getAnnotations())) {
                return;
            }
            ClassInfo clazz = ctx.getTarget().asClass();
            if (!isJaxRsEndpoint(clazz) && !isJaxRsProvider(clazz)) {
                while (clazz != null && clazz.superName() != null) {
                    Map<DotName, List<AnnotationInstance>> annotations = clazz.annotations();
                    if (annotations.containsKey(GAUGE)
                            || annotations.containsKey(SmallRyeMetricsDotNames.CONCURRENT_GAUGE)
                            || annotations.containsKey(SmallRyeMetricsDotNames.COUNTED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.METERED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.SIMPLY_TIMED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.TIMED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.METRIC)) {
                        LOGGER.debugf(
                                "Found metrics business methods on a class %s with no scope defined - adding @Dependent",
                                ctx.getTarget());
                        ctx.transform().add(Dependent.class).done();
                        break;
                    }
                    clazz = index.getIndex().getClassByName(clazz.superName());
                }
            }
        }
    });
}
 
Example 16
Source File: QuteProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 * Find a non-static non-synthetic method with the given name, matching number of params and assignable parameter types.
 * 
 * @param virtualMethod
 * @param clazz
 * @param expression
 * @param index
 * @param templateIdToPathFun
 * @param results
 * @return the method or null
 */
private AnnotationTarget findMethod(VirtualMethodPart virtualMethod, ClassInfo clazz, Expression expression,
        IndexView index, Function<String, String> templateIdToPathFun, Map<String, Match> results) {
    while (clazz != null) {
        for (MethodInfo method : clazz.methods()) {
            if (Modifier.isPublic(method.flags()) && !Modifier.isStatic(method.flags())
                    && !ValueResolverGenerator.isSynthetic(method.flags())
                    && method.name().equals(virtualMethod.getName())) {
                boolean isVarArgs = ValueResolverGenerator.isVarArgs(method);
                List<Type> parameters = method.parameters();
                int lastParamIdx = parameters.size() - 1;

                if (isVarArgs) {
                    // For varargs methods match the minimal number of params
                    if (lastParamIdx > virtualMethod.getParameters().size()) {
                        continue;
                    }
                } else {
                    if (virtualMethod.getParameters().size() != parameters.size()) {
                        // Number of params must be equal
                        continue;
                    }
                }

                // Check parameter types if available
                boolean matches = true;
                byte idx = 0;

                for (Expression param : virtualMethod.getParameters()) {
                    Match result = results.get(param.toOriginalString());
                    if (result != null && !result.isEmpty()) {
                        // Type info available - validate parameter type
                        Type paramType;
                        if (isVarArgs && idx >= lastParamIdx) {
                            // Replace the type for varargs methods
                            paramType = parameters.get(lastParamIdx).asArrayType().component();
                        } else {
                            paramType = parameters.get(idx);
                        }
                        if (!Types.isAssignableFrom(result.type,
                                paramType, index)) {
                            matches = false;
                            break;
                        }
                    } else {
                        LOGGER.debugf(
                                "Type info not available - skip validation for parameter [%s] of method [%s] for expression [%s] in template [%s] on line %s",
                                method.parameterName(idx),
                                method.declaringClass().name() + "#" + method,
                                expression.toOriginalString(),
                                templateIdToPathFun.apply(expression.getOrigin().getTemplateId()),
                                expression.getOrigin().getLine());
                    }
                    idx++;
                }
                return matches ? method : null;
            }
        }
        DotName superName = clazz.superName();
        if (superName == null || DotNames.OBJECT.equals(superName)) {
            clazz = null;
        } else {
            clazz = index.getClassByName(clazz.superName());
        }
    }
    return null;
}
 
Example 17
Source File: Serianalyzer.java    From serianalyzer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param methodReference
 * @param cl
 * @param callers
 * @return
 * @throws IOException
 */
private boolean doCheckInSuperClasses ( MethodReference methodReference, Logger cl, Set<MethodReference> callers ) throws IOException {
    DotName dn = methodReference.getTypeName();
    ClassInfo ci = this.input.getIndex().getClassByName(dn);
    do {
        if ( ci == null || "java.lang.Object".equals(dn.toString()) ) { //$NON-NLS-1$
            break;
        }

        DotName superName;
        if ( ci.superName() == null ) {
            superName = DotName.createSimple("java.lang.Object"); //$NON-NLS-1$
        }
        else {
            superName = ci.superName();
        }

        if ( "java.lang.Object".equals(superName.toString()) && TypeUtil.isObjectMethod(methodReference) ) { //$NON-NLS-1$
            return true;
        }

        ci = this.input.getIndex().getClassByName(superName);
        if ( ci == null ) {
            log.error("Failed to find super class " + superName); //$NON-NLS-1$
            return false;
        }

        MethodReference superRef = methodReference.adaptToType(superName);
        if ( TypeUtil.implementsMethod(methodReference, ci) && doCheckClassInternal(cl, methodReference, superRef) ) {
            this.state.traceCalls(superRef, callers);
            return true;
        }

        if ( cl.isDebugEnabled() ) {
            cl.debug("Failed to find in " + superName); //$NON-NLS-1$
        }
        dn = superName;
    }
    while ( true );
    return false;
}
 
Example 18
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());
        }
    }
}