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

The following examples show how to use org.jboss.jandex.ClassInfo#fields() . 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: JandexProtoGenerator.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<ClassInfo> extractDataClasses(Collection<ClassInfo> input, String targetDirectory) {
    Set<ClassInfo> dataModelClasses = new HashSet<>();
    for (ClassInfo modelClazz : input) {
        try {
            for (FieldInfo pd : modelClazz.fields()) {

                if (pd.type().name().toString().startsWith("java.lang")
                        || pd.type().name().toString().equals(Date.class.getCanonicalName())) {
                    continue;
                }

                dataModelClasses.add(index.getClassByName(pd.type().name()));
            }

            generateModelClassProto(modelClazz, targetDirectory);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    return dataModelClasses;
}
 
Example 2
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 3
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 4
Source File: KotlinPanacheEntityEnhancer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void collectFields(ClassInfo classInfo) {
    EntityModel<EntityField> entityModel = new EntityModel<>(classInfo);
    for (FieldInfo fieldInfo : classInfo.fields()) {
        String name = fieldInfo.name();
        if (Modifier.isPublic(fieldInfo.flags())
                && !Modifier.isStatic(fieldInfo.flags())
                && !fieldInfo.hasAnnotation(DOTNAME_TRANSIENT)) {
            entityModel.addField(new EntityField(name, DescriptorUtils.typeToString(fieldInfo.type())));
        }
    }
    modelInfo.addEntityModel(entityModel);
}
 
Example 5
Source File: KotlinPanacheCompanionEnhancer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void collectFields(ClassInfo classInfo) {
    EntityModel<EntityField> entityModel = new EntityModel<>(classInfo);
    for (FieldInfo fieldInfo : classInfo.fields()) {
        String name = fieldInfo.name();
        if (Modifier.isPublic(fieldInfo.flags())
                && !Modifier.isStatic(fieldInfo.flags())
                && !fieldInfo.hasAnnotation(DOTNAME_TRANSIENT)) {
            entityModel.addField(new EntityField(name, DescriptorUtils.typeToString(fieldInfo.type())));
        }
    }
    modelInfo.addEntityModel(entityModel);
}
 
Example 6
Source File: PanacheMongoEntityEnhancer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void collectFields(ClassInfo classInfo) {
    EntityModel<EntityField> entityModel = new EntityModel<>(classInfo);
    for (FieldInfo fieldInfo : classInfo.fields()) {
        String name = fieldInfo.name();
        if (Modifier.isPublic(fieldInfo.flags()) && !fieldInfo.hasAnnotation(DOTNAME_BSON_IGNORE)) {
            entityModel.addField(new EntityField(name, DescriptorUtils.typeToString(fieldInfo.type())));
        }
    }
    modelInfo.addEntityModel(entityModel);
}
 
Example 7
Source File: ReactivePanacheMongoEntityEnhancer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void collectFields(ClassInfo classInfo) {
    EntityModel<EntityField> entityModel = new EntityModel<>(classInfo);
    for (FieldInfo fieldInfo : classInfo.fields()) {
        String name = fieldInfo.name();
        if (Modifier.isPublic(fieldInfo.flags()) && !fieldInfo.hasAnnotation(DOTNAME_BSON_IGNORE)) {
            entityModel.addField(new EntityField(name, DescriptorUtils.typeToString(fieldInfo.type())));
        }
    }
    modelInfo.addEntityModel(entityModel);
}
 
Example 8
Source File: PanacheJpaEntityEnhancer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void collectFields(ClassInfo classInfo) {
    EntityModel<EntityField> entityModel = new EntityModel<>(classInfo);
    for (FieldInfo fieldInfo : classInfo.fields()) {
        String name = fieldInfo.name();
        if (Modifier.isPublic(fieldInfo.flags())
                && !fieldInfo.hasAnnotation(DOTNAME_TRANSIENT)) {
            entityModel.addField(new EntityField(name, DescriptorUtils.typeToString(fieldInfo.type())));
        }
    }
    modelInfo.addEntityModel(entityModel);
}
 
Example 9
Source File: JandexProtoGenerator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
protected ProtoMessage messageFromClass(Proto proto, ClassInfo clazz, IndexView index, String packageName,
        String messageComment, String fieldComment)
        throws Exception {

    if (isHidden(clazz)) {
        // since class is marked as hidden skip processing of that class
        return null;
    }

    String name = clazz.simpleName();
    String altName = getReferenceOfModel(clazz, "name");
    if (altName != null) {

        name = altName;
    }
    ProtoMessage message = new ProtoMessage(name, packageName == null ? clazz.name().prefix().toString() : packageName);

    for (FieldInfo pd : clazz.fields()) {
        String completeFieldComment = fieldComment;
        // ignore static and/or transient fields
        if (Modifier.isStatic(pd.flags()) || Modifier.isTransient(pd.flags())) {
            continue;
        }

        AnnotationInstance variableInfo = pd.annotation(variableInfoAnnotation);

        if (variableInfo != null) {
            completeFieldComment = fieldComment + "\n @VariableInfo(tags=\"" + variableInfo.value("tags").asString()
                    + "\")";
        }

        String fieldTypeString = pd.type().name().toString();

        DotName fieldType = pd.type().name();
        String protoType;
        if (pd.type().kind() == Kind.PARAMETERIZED_TYPE) {
            fieldTypeString = "Collection";

            List<Type> typeParameters = pd.type().asParameterizedType().arguments();
            if (typeParameters.isEmpty()) {
                throw new IllegalArgumentException("Field " + pd.name() + " of class " + clazz.name().toString()
                        + " uses collection without type information");
            }
            fieldType = typeParameters.get(0).name();
            protoType = protoType(fieldType.toString());
        } else {
            protoType = protoType(fieldTypeString);
        }

        if (protoType == null) {
            ClassInfo classInfo = index.getClassByName(fieldType);
            if (classInfo == null) {
                throw new IllegalStateException("Cannot find class info in jandex index for " + fieldType);
            }
            ProtoMessage another = messageFromClass(proto, classInfo, index, packageName,
                    messageComment, fieldComment);
            protoType = another.getName();
        }

        message.addField(applicabilityByType(fieldTypeString), protoType, pd.name()).setComment(completeFieldComment);
    }
    message.setComment(messageComment);
    proto.addMessage(message);
    return message;
}
 
Example 10
Source File: JpaJandexScavenger.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 * Add the class to the reflective list with full method and field access.
 * Add the superclasses recursively as well as the interfaces.
 * Un-indexed classes/interfaces are accumulated to be thrown as a configuration error in the top level caller method
 * <p>
 * TODO should we also return the return types of all methods and fields? It could contain Enums for example.
 */
private static void addClassHierarchyToReflectiveList(IndexView index, JpaEntitiesBuildItem domainObjectCollector,
        Set<String> enumTypeCollector, Set<String> javaTypeCollector, DotName className, Set<DotName> unindexedClasses) {
    if (className == null || isIgnored(className)) {
        // bail out if java.lang.Object or a class we want to ignore
        return;
    }

    // if the class is in the java. package and is not ignored, we want to register it for reflection
    if (isInJavaPackage(className)) {
        javaTypeCollector.add(className.toString());
        return;
    }

    ClassInfo classInfo = index.getClassByName(className);
    if (classInfo == null) {
        unindexedClasses.add(className);
        return;
    }
    // we need to check for enums
    for (FieldInfo fieldInfo : classInfo.fields()) {
        DotName fieldType = fieldInfo.type().name();
        ClassInfo fieldTypeClassInfo = index.getClassByName(fieldType);
        if (fieldTypeClassInfo != null && ENUM.equals(fieldTypeClassInfo.superName())) {
            enumTypeCollector.add(fieldType.toString());
        }
    }

    //Capture this one (for various needs: Reflective access enablement, Hibernate enhancement, JPA Template)
    collectDomainObject(domainObjectCollector, classInfo);

    // add superclass recursively
    addClassHierarchyToReflectiveList(index, domainObjectCollector, enumTypeCollector, javaTypeCollector,
            classInfo.superName(),
            unindexedClasses);
    // add interfaces recursively
    for (DotName interfaceDotName : classInfo.interfaceNames()) {
        addClassHierarchyToReflectiveList(index, domainObjectCollector, enumTypeCollector, javaTypeCollector,
                interfaceDotName,
                unindexedClasses);
    }
}
 
Example 11
Source File: ReflectiveHierarchyStep.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void addClassTypeHierarchy(CombinedIndexBuildItem combinedIndexBuildItem,
        ReflectiveHierarchyBuildItem reflectiveHierarchyBuildItem,
        String source,
        DotName name,
        Set<DotName> processedReflectiveHierarchies,
        Map<DotName, Set<String>> unindexedClasses,
        Predicate<ClassInfo> finalFieldsWritable,
        BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
    if (reflectiveHierarchyBuildItem.getIgnorePredicate().test(name)) {
        return;
    }

    ClassInfo info = (reflectiveHierarchyBuildItem.getIndex() != null ? reflectiveHierarchyBuildItem.getIndex()
            : combinedIndexBuildItem.getIndex()).getClassByName(name);

    if (info == null) {
        unindexedClasses.putIfAbsent(name, new TreeSet<>());
        unindexedClasses.get(name).add(source);
    }

    if (processedReflectiveHierarchies.contains(name)) {
        return;
    }

    reflectiveClass.produce(
            ReflectiveClassBuildItem
                    .builder(name.toString())
                    .methods(true)
                    .fields(true)
                    .finalFieldsWritable(doFinalFieldsNeedToBeWritable(info, finalFieldsWritable))
                    .build());

    processedReflectiveHierarchies.add(name);

    if (info == null) {
        return;
    }

    addClassTypeHierarchy(combinedIndexBuildItem, reflectiveHierarchyBuildItem, source, info.superName(),
            processedReflectiveHierarchies,
            unindexedClasses, finalFieldsWritable, reflectiveClass);
    for (FieldInfo field : info.fields()) {
        if (Modifier.isStatic(field.flags()) || field.name().startsWith("this$") || field.name().startsWith("val$")) {
            // skip the static fields (especially loggers)
            // also skip the outer class elements (unfortunately, we don't have a way to test for synthetic fields in Jandex)
            continue;
        }
        addReflectiveHierarchy(combinedIndexBuildItem, reflectiveHierarchyBuildItem, source, field.type(),
                processedReflectiveHierarchies,
                unindexedClasses, finalFieldsWritable, reflectiveClass);
    }
    for (MethodInfo method : info.methods()) {
        if (method.parameters().size() > 0 || Modifier.isStatic(method.flags())
                || method.returnType().kind() == Kind.VOID) {
            // we will only consider potential getters
            continue;
        }
        addReflectiveHierarchy(combinedIndexBuildItem, reflectiveHierarchyBuildItem, source, method.returnType(),
                processedReflectiveHierarchies,
                unindexedClasses, finalFieldsWritable, reflectiveClass);
    }
}