Java Code Examples for org.jboss.jandex.Type.Kind#PRIMITIVE

The following examples show how to use org.jboss.jandex.Type.Kind#PRIMITIVE . 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: Types.java    From quarkus with Apache License 2.0 6 votes vote down vote up
static Set<Type> getProducerFieldTypeClosure(FieldInfo producerField, BeanDeployment beanDeployment) {
    Set<Type> types;
    Type fieldType = producerField.type();
    if (fieldType.kind() == Kind.PRIMITIVE || fieldType.kind() == Kind.ARRAY) {
        types = new HashSet<>();
        types.add(fieldType);
        types.add(OBJECT_TYPE);
    } else {
        ClassInfo fieldClassInfo = getClassByName(beanDeployment.getIndex(), producerField.type());
        if (fieldClassInfo == null) {
            throw new IllegalArgumentException("Producer field type not found in index: " + producerField.type().name());
        }
        if (Kind.CLASS.equals(fieldType.kind())) {
            types = getTypeClosure(fieldClassInfo, producerField, Collections.emptyMap(), beanDeployment, null);
        } else if (Kind.PARAMETERIZED_TYPE.equals(fieldType.kind())) {
            types = getTypeClosure(fieldClassInfo, producerField,
                    buildResolvedMap(fieldType.asParameterizedType().arguments(), fieldClassInfo.typeParameters(),
                            Collections.emptyMap(), beanDeployment.getIndex()),
                    beanDeployment, null);
        } else {
            throw new IllegalArgumentException("Unsupported return type");
        }
    }
    return restrictBeanTypes(types, beanDeployment.getAnnotations(producerField));
}
 
Example 2
Source File: AbstractGenerator.java    From quarkus with Apache License 2.0 6 votes vote down vote up
protected String getPackageName(BeanInfo bean) {
    DotName providerTypeName;
    if (bean.isProducerMethod() || bean.isProducerField()) {
        providerTypeName = bean.getDeclaringBean().getProviderType().name();
    } else {
        if (bean.getProviderType().kind() == Kind.ARRAY || bean.getProviderType().kind() == Kind.PRIMITIVE) {
            providerTypeName = bean.getImplClazz().name();
        } else {
            providerTypeName = bean.getProviderType().name();
        }
    }
    String packageName = DotNames.packageName(providerTypeName);
    if (packageName.isEmpty() || packageName.startsWith("java.")) {
        // It is not possible to place a class in a JDK package or in default package
        packageName = DEFAULT_PACKAGE;
    }
    return packageName;
}
 
Example 3
Source File: ConfigBuildStep.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
@Record(RUNTIME_INIT)
void validateConfigProperties(ConfigRecorder recorder, List<ConfigPropertyBuildItem> configProperties,
        BeanContainerBuildItem beanContainer, BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
    // IMPL NOTE: we do depend on BeanContainerBuildItem to make sure that the BeanDeploymentValidator finished its processing

    // the non-primitive types need to be registered for reflection since Class.forName is used at runtime to load the class
    for (ConfigPropertyBuildItem item : configProperties) {
        Type requiredType = item.getPropertyType();
        String propertyType = requiredType.name().toString();
        if (requiredType.kind() != Kind.PRIMITIVE) {
            reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, propertyType));
        }
    }

    Map<String, Set<String>> propNamesToClasses = configProperties.stream().collect(
            groupingBy(ConfigPropertyBuildItem::getPropertyName,
                    mapping(c -> c.getPropertyType().name().toString(), toSet())));
    recorder.validateConfigProperties(propNamesToClasses);
}
 
Example 4
Source File: ConfigBuildStep.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private boolean isHandledByProducers(Type type) {
    if (type.kind() == Kind.ARRAY) {
        return false;
    }
    if (type.kind() == Kind.PRIMITIVE) {
        return true;
    }
    return DotNames.STRING.equals(type.name()) ||
            DotNames.OPTIONAL.equals(type.name()) ||
            SET_NAME.equals(type.name()) ||
            LIST_NAME.equals(type.name()) ||
            DotNames.LONG.equals(type.name()) ||
            DotNames.FLOAT.equals(type.name()) ||
            DotNames.INTEGER.equals(type.name()) ||
            DotNames.BOOLEAN.equals(type.name()) ||
            DotNames.DOUBLE.equals(type.name()) ||
            DotNames.SHORT.equals(type.name()) ||
            DotNames.BYTE.equals(type.name()) ||
            DotNames.CHARACTER.equals(type.name());
}
 
Example 5
Source File: AsmUtil.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a return bytecode instruction suitable for the given return Jandex Type. This will return
 * specialised return instructions <tt>IRETURN, LRETURN, FRETURN, DRETURN, RETURN</tt> for primitives/void,
 * and <tt>ARETURN</tt> otherwise;
 * 
 * @param typeDescriptor the return Jandex Type.
 * @return the correct bytecode return instruction for that return type descriptor.
 */
public static int getReturnInstruction(Type jandexType) {
    if (jandexType.kind() == Kind.PRIMITIVE) {
        switch (jandexType.asPrimitiveType().primitive()) {
            case BOOLEAN:
            case BYTE:
            case SHORT:
            case INT:
            case CHAR:
                return Opcodes.IRETURN;
            case DOUBLE:
                return Opcodes.DRETURN;
            case FLOAT:
                return Opcodes.FRETURN;
            case LONG:
                return Opcodes.LRETURN;
            default:
                throw new IllegalArgumentException("Unknown primitive type: " + jandexType);
        }
    } else if (jandexType.kind() == Kind.VOID) {
        return Opcodes.RETURN;
    }
    return Opcodes.ARETURN;
}
 
Example 6
Source File: AsmUtil.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the bytecode instruction to load the given Jandex Type. This returns the specialised
 * bytecodes <tt>ILOAD, DLOAD, FLOAD and LLOAD</tt> for primitives, or <tt>ALOAD</tt> otherwise.
 * 
 * @param jandexType The Jandex Type whose load instruction to return.
 * @return The bytecode instruction to load the given Jandex Type.
 */
public static int getLoadOpcode(Type jandexType) {
    if (jandexType.kind() == Kind.PRIMITIVE) {
        switch (jandexType.asPrimitiveType().primitive()) {
            case BOOLEAN:
            case BYTE:
            case SHORT:
            case INT:
            case CHAR:
                return Opcodes.ILOAD;
            case DOUBLE:
                return Opcodes.DLOAD;
            case FLOAT:
                return Opcodes.FLOAD;
            case LONG:
                return Opcodes.LLOAD;
            default:
                throw new IllegalArgumentException("Unknown primitive type: " + jandexType);
        }
    }
    return Opcodes.ALOAD;
}
 
Example 7
Source File: Types.java    From quarkus with Apache License 2.0 5 votes vote down vote up
static Set<Type> getProducerMethodTypeClosure(MethodInfo producerMethod, BeanDeployment beanDeployment) {
    Set<Type> types;
    Type returnType = producerMethod.returnType();
    if (returnType.kind() == Kind.PRIMITIVE || returnType.kind() == Kind.ARRAY) {
        types = new HashSet<>();
        types.add(returnType);
        types.add(OBJECT_TYPE);
        return types;
    } else {
        ClassInfo returnTypeClassInfo = getClassByName(beanDeployment.getIndex(), returnType);
        if (returnTypeClassInfo == null) {
            throw new IllegalArgumentException(
                    "Producer method return type not found in index: " + producerMethod.returnType().name());
        }
        if (Kind.CLASS.equals(returnType.kind())) {
            types = getTypeClosure(returnTypeClassInfo, producerMethod, Collections.emptyMap(), beanDeployment, null);
        } else if (Kind.PARAMETERIZED_TYPE.equals(returnType.kind())) {
            types = getTypeClosure(returnTypeClassInfo, producerMethod,
                    buildResolvedMap(returnType.asParameterizedType().arguments(), returnTypeClassInfo.typeParameters(),
                            Collections.emptyMap(), beanDeployment.getIndex()),
                    beanDeployment, null);
        } else {
            throw new IllegalArgumentException("Unsupported return type");
        }
    }
    return restrictBeanTypes(types, beanDeployment.getAnnotations(producerMethod));
}
 
Example 8
Source File: AsmUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the right boxing method for the given Jandex Type if it is a primitive.
 * 
 * @param mv The MethodVisitor on which to visit the boxing instructions
 * @param jandexType The Jandex Type to box if it is a primitive.
 */
public static void boxIfRequired(MethodVisitor mv, Type jandexType) {
    if (jandexType.kind() == Kind.PRIMITIVE) {
        switch (jandexType.asPrimitiveType().primitive()) {
            case BOOLEAN:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
                break;
            case BYTE:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
                break;
            case CHAR:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;",
                        false);
                break;
            case DOUBLE:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
                break;
            case FLOAT:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
                break;
            case INT:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
                break;
            case LONG:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
                break;
            case SHORT:
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
                break;
            default:
                throw new IllegalArgumentException("Unknown primitive type: " + jandexType);
        }
    }
}
 
Example 9
Source File: AsmUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the right unboxing method for the given Jandex Type if it is a primitive.
 * 
 * @param mv The MethodVisitor on which to visit the unboxing instructions
 * @param jandexType The Jandex Type to unbox if it is a primitive.
 */
public static void unboxIfRequired(MethodVisitor mv, Type jandexType) {
    if (jandexType.kind() == Kind.PRIMITIVE) {
        switch (jandexType.asPrimitiveType().primitive()) {
            case BOOLEAN:
                unbox(mv, "java/lang/Boolean", "booleanValue", "Z");
                break;
            case BYTE:
                unbox(mv, "java/lang/Byte", "byteValue", "B");
                break;
            case CHAR:
                unbox(mv, "java/lang/Character", "charValue", "C");
                break;
            case DOUBLE:
                unbox(mv, "java/lang/Double", "doubleValue", "D");
                break;
            case FLOAT:
                unbox(mv, "java/lang/Float", "floatValue", "F");
                break;
            case INT:
                unbox(mv, "java/lang/Integer", "intValue", "I");
                break;
            case LONG:
                unbox(mv, "java/lang/Long", "longValue", "J");
                break;
            case SHORT:
                unbox(mv, "java/lang/Short", "shortValue", "S");
                break;
            default:
                throw new IllegalArgumentException("Unknown primitive type: " + jandexType);
        }
    }
}
 
Example 10
Source File: AsmUtil.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the number of underlying bytecode parameters taken by the given Jandex parameter Type.
 * This will be 2 for doubles and longs, 1 otherwise.
 * 
 * @param paramType the Jandex parameter Type
 * @return the number of underlying bytecode parameters required.
 */
public static int getParameterSize(Type paramType) {
    if (paramType.kind() == Kind.PRIMITIVE) {
        switch (paramType.asPrimitiveType().primitive()) {
            case DOUBLE:
            case LONG:
                return 2;
        }
    }
    return 1;
}
 
Example 11
Source File: Types.java    From quarkus with Apache License 2.0 4 votes vote down vote up
static Type box(Type type) {
    if (type.kind() == Kind.PRIMITIVE) {
        return box(type.asPrimitiveType().primitive());
    }
    return type;
}
 
Example 12
Source File: Types.java    From quarkus with Apache License 2.0 4 votes vote down vote up
static Type box(Type type) {
    if (type.kind() == Kind.PRIMITIVE) {
        return box(type.asPrimitiveType().primitive());
    }
    return type;
}
 
Example 13
Source File: PanacheRepositoryEnhancer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void generateJvmBridge(MethodInfo method) {
    // get a bounds-erased descriptor
    String descriptor = AsmUtil.getDescriptor(method, name -> null);
    // make sure we need a bridge
    if (!userMethods.contains(method.name() + "/" + descriptor)) {
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE,
                method.name(),
                descriptor,
                null,
                null);
        List<org.jboss.jandex.Type> parameters = method.parameters();
        for (int i = 0; i < parameters.size(); i++) {
            mv.visitParameter(method.parameterName(i), 0 /* modifiers */);
        }
        mv.visitCode();
        // this
        mv.visitIntInsn(Opcodes.ALOAD, 0);
        // each param
        for (int i = 0; i < parameters.size(); i++) {
            org.jboss.jandex.Type paramType = parameters.get(i);
            if (paramType.kind() == Kind.PRIMITIVE)
                throw new IllegalStateException("BUG: Don't know how to generate JVM bridge method for " + method
                        + ": has primitive parameters");
            mv.visitIntInsn(Opcodes.ALOAD, i + 1);
            if (paramType.kind() == Kind.TYPE_VARIABLE) {
                String typeParamName = paramType.asTypeVariable().identifier();
                switch (typeParamName) {
                    case "Entity":
                        mv.visitTypeInsn(Opcodes.CHECKCAST, entityBinaryType);
                        break;
                    case "Id":
                        mv.visitTypeInsn(Opcodes.CHECKCAST, idBinaryType);
                        break;
                }
            }
        }

        String targetDescriptor = AsmUtil.getDescriptor(method, name -> typeArguments.get(name));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                daoBinaryName,
                method.name(),
                targetDescriptor, false);
        String targetReturnTypeDescriptor = targetDescriptor.substring(targetDescriptor.indexOf(')') + 1);
        mv.visitInsn(AsmUtil.getReturnInstruction(targetReturnTypeDescriptor));
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

}