Java Code Examples for org.jboss.jandex.FieldInfo#name()

The following examples show how to use org.jboss.jandex.FieldInfo#name() . 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: AvroProcessor.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void recordAvroSchemasResigtration(BeanArchiveIndexBuildItem beanArchiveIndex,
        BeanContainerBuildItem beanContainer, AvroRecorder avroRecorder) {
    IndexView index = beanArchiveIndex.getIndex();
    for (AnnotationInstance annotation : index.getAnnotations(BUILD_TIME_AVRO_DATAFORMAT_ANNOTATION)) {
        String schemaResourceName = annotation.value().asString();
        FieldInfo fieldInfo = annotation.target().asField();
        String injectedFieldId = fieldInfo.declaringClass().name() + "." + fieldInfo.name();
        try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaResourceName)) {
            Schema avroSchema = new Schema.Parser().parse(is);
            avroRecorder.recordAvroSchemaResigtration(beanContainer.getValue(), injectedFieldId, avroSchema);
            LOG.debug("Parsed the avro schema at build time from resource named " + schemaResourceName);
        } catch (SchemaParseException | IOException ex) {
            final String message = "An issue occured while parsing schema resource on field " + injectedFieldId;
            throw new RuntimeException(message, ex);
        }
    }
}
 
Example 2
Source File: MethodNameParser.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void validateFieldWithOperation(String operation, FieldInfo fieldInfo, String methodName) {
    DotName fieldTypeDotName = fieldInfo.type().name();
    if (STRING_LIKE_OPERATIONS.contains(operation) && !DotNames.STRING.equals(fieldTypeDotName)) {
        throw new UnableToParseMethodException(
                operation + " cannot be specified for field" + fieldInfo.name() + " of method "
                        + methodName + " because it is not a String type");
    }

    if (BOOLEAN_OPERATIONS.contains(operation) && !DotNames.BOOLEAN.equals(fieldTypeDotName)
            && !DotNames.PRIMITIVE_BOOLEAN.equals(fieldTypeDotName)) {
        throw new UnableToParseMethodException(
                operation + " cannot be specified for field" + fieldInfo.name() + " of method "
                        + methodName + " because it is not a boolean type");
    }
}
 
Example 3
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 4
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 5
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 6
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 7
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 8
Source File: FieldCreator.java    From smallrye-graphql with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a field from a public field.
 * Used by Type and Input
 * 
 * @param direction the direction (in/out)
 * @param fieldInfo the java property
 * @return a Field model object
 */
public Optional<Field> createFieldForPojo(Direction direction, FieldInfo fieldInfo) {
    if (Modifier.isPublic(fieldInfo.flags())) {
        Annotations annotationsForPojo = Annotations.getAnnotationsForPojo(direction, fieldInfo);

        if (!IgnoreHelper.shouldIgnore(annotationsForPojo, fieldInfo)) {

            // Name
            String name = getFieldName(direction, annotationsForPojo, fieldInfo.name());

            // Field Type
            Type fieldType = fieldInfo.type();

            // Description
            Optional<String> maybeDescription = DescriptionHelper.getDescriptionForField(annotationsForPojo, fieldType);

            Reference reference = referenceCreator.createReferenceForPojoField(direction, fieldType, fieldType,
                    annotationsForPojo);

            Field field = new Field(fieldInfo.name(),
                    MethodHelper.getPropertyName(direction, fieldInfo.name()),
                    name,
                    maybeDescription.orElse(null),
                    reference);

            // NotNull
            if (NonNullHelper.markAsNonNull(fieldType, annotationsForPojo)) {
                field.setNotNull(true);
            }

            // Array
            field.setArray(ArrayCreator.createArray(fieldType, fieldType).orElse(null));

            // TransformInfo
            field.setTransformInfo(FormatHelper.getFormat(fieldType, annotationsForPojo).orElse(null));

            // MappingInfo
            field.setMappingInfo(MappingHelper.getMapping(field, annotationsForPojo).orElse(null));

            // Default Value
            field.setDefaultValue(DefaultValueHelper.getDefaultValue(annotationsForPojo).orElse(null));

            return Optional.of(field);
        }
        return Optional.empty();
    }
    return Optional.empty();
}
 
Example 9
Source File: FieldDescriptor.java    From gizmo with Apache License 2.0 4 votes vote down vote up
private FieldDescriptor(FieldInfo fieldInfo) {
    this.name = fieldInfo.name();
    this.type = DescriptorUtils.typeToString(fieldInfo.type());
    this.declaringClass = fieldInfo.declaringClass().toString().replace('.', '/');
}
 
Example 10
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 11
Source File: Beans.java    From quarkus with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param producerField
 * @param declaringBean
 * @param beanDeployment
 * @param disposer
 * @return a new bean info
 */
static BeanInfo createProducerField(FieldInfo producerField, BeanInfo declaringBean, BeanDeployment beanDeployment,
        DisposerInfo disposer) {
    Set<AnnotationInstance> qualifiers = new HashSet<>();
    List<ScopeInfo> scopes = new ArrayList<>();
    Set<Type> types = Types.getProducerFieldTypeClosure(producerField, beanDeployment);
    Integer alternativePriority = null;
    boolean isAlternative = false;
    boolean isDefaultBean = false;
    List<StereotypeInfo> stereotypes = new ArrayList<>();
    String name = null;

    for (AnnotationInstance annotation : beanDeployment.getAnnotations(producerField)) {
        if (DotNames.NAMED.equals(annotation.name())) {
            AnnotationValue nameValue = annotation.value();
            if (nameValue != null) {
                name = nameValue.asString();
            } else {
                name = producerField.name();
            }
        }
        Collection<AnnotationInstance> qualifierCollection = beanDeployment.extractQualifiers(annotation);
        for (AnnotationInstance qualifierAnnotation : qualifierCollection) {
            // Qualifiers
            qualifiers.add(qualifierAnnotation);
        }
        if (!qualifierCollection.isEmpty()) {
            // we needn't process it further, the annotation was a qualifier (or multiple repeating ones)
            continue;
        }
        if (DotNames.ALTERNATIVE.equals(annotation.name())) {
            isAlternative = true;
            continue;
        }
        if (DotNames.ALTERNATIVE_PRIORITY.equals(annotation.name())) {
            isAlternative = true;
            alternativePriority = annotation.value().asInt();
            continue;
        }
        ScopeInfo scopeAnnotation = beanDeployment.getScope(annotation.name());
        if (scopeAnnotation != null) {
            scopes.add(scopeAnnotation);
            continue;
        }
        StereotypeInfo stereotype = beanDeployment.getStereotype(annotation.name());
        if (stereotype != null) {
            stereotypes.add(stereotype);
            continue;
        }
        if (DotNames.DEFAULT_BEAN.equals(annotation.name())) {
            isDefaultBean = true;
            continue;
        }
    }

    if (scopes.size() > 1) {
        throw multipleScopesFound("Producer field " + producerField, scopes);
    }
    ScopeInfo scope;
    if (scopes.isEmpty()) {
        scope = initStereotypeScope(stereotypes, producerField);
    } else {
        scope = scopes.get(0);
    }
    if (!isAlternative) {
        isAlternative = initStereotypeAlternative(stereotypes);
    }
    if (name == null) {
        name = initStereotypeName(stereotypes, producerField);
    }

    if (isAlternative) {
        if (alternativePriority == null) {
            alternativePriority = declaringBean.getAlternativePriority();
        }
        alternativePriority = initAlternativePriority(producerField, alternativePriority, stereotypes, beanDeployment);
        // after all attempts, priority is still null
        if (alternativePriority == null) {
            LOGGER.debugf(
                    "Ignoring producer field %s - declared as an @Alternative but not selected by @Priority, @AlternativePriority or quarkus.arc.selected-alternatives",
                    producerField);
            return null;

        }
    }

    BeanInfo bean = new BeanInfo(producerField, beanDeployment, scope, types, qualifiers, Collections.emptyList(),
            declaringBean, disposer, alternativePriority, stereotypes, name, isDefaultBean);
    return bean;
}
 
Example 12
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 13
Source File: BuildTimeEnabledProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private String toUniqueString(FieldInfo field) {
    return field.declaringClass().name().toString() + "." + field.name();
}
 
Example 14
Source File: SmallRyeMetricsProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
@Record(RUNTIME_INIT)
void registerMetricsFromProducers(
        SmallRyeMetricsRecorder recorder,
        ValidationPhaseBuildItem validationPhase,
        BeanArchiveIndexBuildItem beanArchiveIndex) {
    IndexView index = beanArchiveIndex.getIndex();
    for (io.quarkus.arc.processor.BeanInfo bean : validationPhase.getContext().beans().producers()) {
        ClassInfo implClazz = bean.getImplClazz();
        if (implClazz == null) {
            continue;
        }
        MetricType metricType = getMetricType(implClazz);
        if (metricType != null) {
            AnnotationTarget target = bean.getTarget().get();
            AnnotationInstance metricAnnotation = null;
            String memberName = null;
            if (bean.isProducerField()) {
                FieldInfo field = target.asField();
                metricAnnotation = field.annotation(METRIC);
                memberName = field.name();
            }
            if (bean.isProducerMethod()) {
                MethodInfo method = target.asMethod();
                metricAnnotation = method.annotation(METRIC);
                memberName = method.name();
            }
            if (metricAnnotation != null) {
                String nameValue = metricAnnotation.valueWithDefault(index, "name").asString();
                boolean absolute = metricAnnotation.valueWithDefault(index, "absolute").asBoolean();
                String metricSimpleName = !nameValue.isEmpty() ? nameValue : memberName;
                String declaringClassName = bean.getDeclaringBean().getImplClazz().name().toString();
                String metricsFinalName = absolute ? metricSimpleName
                        : MetricRegistry.name(declaringClassName, metricSimpleName);
                recorder.registerMetricFromProducer(
                        bean.getIdentifier(),
                        metricType,
                        metricsFinalName,
                        metricAnnotation.valueWithDefault(index, "tags").asStringArray(),
                        metricAnnotation.valueWithDefault(index, "description").asString(),
                        metricAnnotation.valueWithDefault(index, "displayName").asString(),
                        metricAnnotation.valueWithDefault(index, "unit").asString());
            }
        }
    }
}
 
Example 15
Source File: ReflectiveFieldBuildItem.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public ReflectiveFieldBuildItem(FieldInfo field) {
    this.name = field.name();
    this.declaringClass = field.declaringClass().name().toString();
}