Java Code Examples for org.jboss.jandex.AnnotationValue#kind()

The following examples show how to use org.jboss.jandex.AnnotationValue#kind() . 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: AnnotatedElement.java    From gizmo with Apache License 2.0 6 votes vote down vote up
default void addAnnotation(AnnotationInstance annotation) {
    AnnotationCreator ac = addAnnotation(annotation.name().toString());
    for (AnnotationValue member : annotation.values()) {
        if (member.kind() == AnnotationValue.Kind.NESTED) {
            throw new RuntimeException("Not Yet Implemented: Cannot generate annotation " + annotation);
        } else if (member.kind() == AnnotationValue.Kind.BOOLEAN) {
            ac.addValue(member.name(), member.asBoolean());
        } else if (member.kind() == AnnotationValue.Kind.BYTE) {
            ac.addValue(member.name(), member.asByte());
        } else if (member.kind() == AnnotationValue.Kind.SHORT) {
            ac.addValue(member.name(), member.asShort());
        } else if (member.kind() == AnnotationValue.Kind.INTEGER) {
            ac.addValue(member.name(), member.asInt());
        } else if (member.kind() == AnnotationValue.Kind.LONG) {
            ac.addValue(member.name(), member.asLong());
        } else if (member.kind() == AnnotationValue.Kind.FLOAT) {
            ac.addValue(member.name(), member.asFloat());
        } else if (member.kind() == AnnotationValue.Kind.DOUBLE) {
            ac.addValue(member.name(), member.asDouble());
        } else if (member.kind() == AnnotationValue.Kind.STRING) {
            ac.addValue(member.name(), member.asString());
        } else if (member.kind() == AnnotationValue.Kind.ARRAY) {
            ac.addValue(member.name(), member.value());
        }
    }
}
 
Example 2
Source File: ResteasyCommonProcessor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private boolean restJsonSupportNeeded(CombinedIndexBuildItem indexBuildItem, DotName mediaTypeAnnotation) {
    for (AnnotationInstance annotationInstance : indexBuildItem.getIndex().getAnnotations(mediaTypeAnnotation)) {
        final AnnotationValue annotationValue = annotationInstance.value();
        if (annotationValue == null) {
            continue;
        }

        List<String> mediaTypes = Collections.emptyList();
        if (annotationValue.kind() == Kind.ARRAY) {
            mediaTypes = Arrays.asList(annotationValue.asStringArray());
        } else if (annotationValue.kind() == Kind.STRING) {
            mediaTypes = Collections.singletonList(annotationValue.asString());
        }
        return mediaTypes.contains(MediaType.APPLICATION_JSON)
                || mediaTypes.contains(MediaType.APPLICATION_JSON_PATCH_JSON);
    }

    return false;
}
 
Example 3
Source File: JandexUtil.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a Double property value from the given annotation instance. If no value is found
 * this will return null.
 * 
 * @param annotation AnnotationInstance
 * @param propertyName String
 * @return BigDecimal value
 */
public static BigDecimal bigDecimalValue(AnnotationInstance annotation, String propertyName) {
    AnnotationValue value = annotation.value(propertyName);
    if (value == null) {
        return null;
    }
    if (value.kind() == AnnotationValue.Kind.DOUBLE) {
        return BigDecimal.valueOf(value.asDouble());
    }
    if (value.kind() == AnnotationValue.Kind.STRING) {
        return new BigDecimal(value.asString());
    }
    throw new RuntimeException(
            "Call to bigDecimalValue failed because the annotation property was not a double or a String.");
}
 
Example 4
Source File: AnnotationLiteralGenerator.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void generateStaticFieldsWithDefaultValues(ClassCreator annotationLiteral,
        List<MethodInfo> defaultOfClassType) {
    if (defaultOfClassType.isEmpty()) {
        return;
    }

    MethodCreator staticConstructor = annotationLiteral.getMethodCreator(Methods.CLINIT, void.class);
    staticConstructor.setModifiers(ACC_STATIC);

    for (MethodInfo method : defaultOfClassType) {
        Type returnType = method.returnType();
        String returnTypeName = returnType.name().toString();
        AnnotationValue defaultValue = method.defaultValue();

        FieldCreator fieldCreator = annotationLiteral.getFieldCreator(defaultValueStaticFieldName(method), returnTypeName);
        fieldCreator.setModifiers(ACC_PUBLIC | ACC_STATIC | ACC_FINAL);

        if (defaultValue.kind() == AnnotationValue.Kind.ARRAY) {
            Type[] clazzArray = defaultValue.asClassArray();
            ResultHandle array = staticConstructor.newArray(returnTypeName, clazzArray.length);
            for (int i = 0; i < clazzArray.length; ++i) {
                staticConstructor.writeArrayValue(array, staticConstructor.load(i),
                        staticConstructor.loadClass(clazzArray[i].name().toString()));
            }
            staticConstructor.writeStaticField(fieldCreator.getFieldDescriptor(), array);
        } else {
            staticConstructor.writeStaticField(fieldCreator.getFieldDescriptor(),
                    staticConstructor.loadClass(defaultValue.asClass().name().toString()));

        }
    }

    staticConstructor.returnValue(null);
}
 
Example 5
Source File: SpringCacheUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static String singleName(AnnotationValue annotationValue, AnnotationTarget target) {
    if (annotationValue.kind() != AnnotationValue.Kind.ARRAY) { // shouldn't happen
        return null;
    }

    String[] strings = annotationValue.asStringArray();
    if (strings.length == 0) {
        return null;
    } else if (strings.length > 1) {
        throw new IllegalArgumentException(
                String.format("Quarkus currently only supports using a single cache name. Offending %s is %s",
                        target.kind() == AnnotationTarget.Kind.METHOD ? "method" : "class", target.toString()));
    }
    return strings[0];
}
 
Example 6
Source File: SpringDIProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static String determineName(AnnotationValue annotationValue) {
    if (annotationValue.kind() == AnnotationValue.Kind.ARRAY) {
        return annotationValue.asStringArray()[0];
    } else if (annotationValue.kind() == AnnotationValue.Kind.STRING) {
        return annotationValue.asString();
    }
    return null;
}
 
Example 7
Source File: AnnotationLiteralGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
static ResultHandle loadValue(String literalClassName,
        BytecodeCreator valueMethod, AnnotationValue value, ClassInfo annotationClass,
        MethodInfo method) {
    ResultHandle retValue;
    switch (value.kind()) {
        case BOOLEAN:
            retValue = valueMethod.load(value.asBoolean());
            break;
        case STRING:
            retValue = valueMethod.load(value.asString());
            break;
        case BYTE:
            retValue = valueMethod.load(value.asByte());
            break;
        case SHORT:
            retValue = valueMethod.load(value.asShort());
            break;
        case LONG:
            retValue = valueMethod.load(value.asLong());
            break;
        case INTEGER:
            retValue = valueMethod.load(value.asInt());
            break;
        case FLOAT:
            retValue = valueMethod.load(value.asFloat());
            break;
        case DOUBLE:
            retValue = valueMethod.load(value.asDouble());
            break;
        case CHARACTER:
            retValue = valueMethod.load(value.asChar());
            break;
        case CLASS:
            if (value.equals(method.defaultValue())) {
                retValue = valueMethod.readStaticField(
                        FieldDescriptor.of(literalClassName, defaultValueStaticFieldName(method),
                                method.returnType().name().toString()));
            } else {
                retValue = valueMethod.loadClass(value.asClass().toString());
            }
            break;
        case ARRAY:
            retValue = arrayValue(literalClassName, value, valueMethod, method, annotationClass);
            break;
        case ENUM:
            retValue = valueMethod
                    .readStaticField(FieldDescriptor.of(value.asEnumType().toString(), value.asEnum(),
                            value.asEnumType().toString()));
            break;
        case NESTED:
        default:
            throw new UnsupportedOperationException("Unsupported value: " + value);
    }
    return retValue;
}
 
Example 8
Source File: BytecodeRecorderImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
DeferredParameter loadValue(AnnotationValue value, ClassInfo annotationClass,
        MethodInfo method) {
    //note that this is a special case, in general DeferredParameter should be added to the main parameter list
    //however in this case we know it is a constant
    return new DeferredParameter() {

        @Override
        ResultHandle doLoad(MethodContext context, MethodCreator valueMethod, ResultHandle array) {

            ResultHandle retValue;
            switch (value.kind()) {
                case BOOLEAN:
                    retValue = valueMethod.load(value.asBoolean());
                    break;
                case STRING:
                    retValue = valueMethod.load(value.asString());
                    break;
                case BYTE:
                    retValue = valueMethod.load(value.asByte());
                    break;
                case SHORT:
                    retValue = valueMethod.load(value.asShort());
                    break;
                case LONG:
                    retValue = valueMethod.load(value.asLong());
                    break;
                case INTEGER:
                    retValue = valueMethod.load(value.asInt());
                    break;
                case FLOAT:
                    retValue = valueMethod.load(value.asFloat());
                    break;
                case DOUBLE:
                    retValue = valueMethod.load(value.asDouble());
                    break;
                case CHARACTER:
                    retValue = valueMethod.load(value.asChar());
                    break;
                case CLASS:
                    retValue = valueMethod.loadClass(value.asClass().toString());
                    break;
                case ARRAY:
                    retValue = arrayValue(value, valueMethod, method, annotationClass);
                    break;
                case ENUM:
                    retValue = valueMethod
                            .readStaticField(FieldDescriptor.of(value.asEnumType().toString(), value.asEnum(),
                                    value.asEnumType().toString()));
                    break;
                case NESTED:
                default:
                    throw new UnsupportedOperationException("Unsupported value: " + value);
            }
            return retValue;
        }
    };
}