Java Code Examples for net.bytebuddy.description.type.TypeDescription#isPrimitive()

The following examples show how to use net.bytebuddy.description.type.TypeDescription#isPrimitive() . 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: InvokeDynamic.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Passes {@code null} values of the given types to the bootstrapped method.
 *
 * @param typeDescription The type that the {@code null} values should represent.
 * @return This invoke dynamic implementation where the bootstrapped method is passed the specified arguments.
 */
public InvokeDynamic withNullValue(TypeDescription... typeDescription) {
    List<InvocationProvider.ArgumentProvider> argumentProviders = new ArrayList<InvocationProvider.ArgumentProvider>(typeDescription.length);
    for (TypeDescription aTypeDescription : typeDescription) {
        if (aTypeDescription.isPrimitive()) {
            throw new IllegalArgumentException("Cannot assign null to primitive type: " + aTypeDescription);
        }
        argumentProviders.add(new InvocationProvider.ArgumentProvider.ForNullValue(aTypeDescription));
    }
    return new InvokeDynamic(bootstrap,
            arguments,
            invocationProvider.appendArguments(argumentProviders),
            terminationHandler,
            assigner,
            typing);
}
 
Example 2
Source File: AsmUtils.java    From Diorite with MIT License 5 votes vote down vote up
public static int getReturnCode(TypeDescription fieldType)
{
    if (fieldType.isPrimitive())
    {
        if (BOOLEAN_P.equals(fieldType) || BYTE_P.equals(fieldType) || CHAR_P.equals(fieldType) ||
            SHORT_P.equals(fieldType) || INT_P.equals(fieldType))
        {
            return IRETURN;
        }
        if (LONG_P.equals(fieldType))
        {
            return LRETURN;
        }
        if (FLOAT_P.equals(fieldType))
        {
            return FRETURN;
        }
        if (DOUBLE_P.equals(fieldType))
        {
            return DRETURN;
        }
        else
        {
            throw new IllegalStateException("Unknown store method");
        }
    }
    else
    {
        return ARETURN;
    }
}
 
Example 3
Source File: AsmUtils.java    From Diorite with MIT License 5 votes vote down vote up
public static int getStoreCode(TypeDescription fieldType)
{
    if (fieldType.isPrimitive())
    {
        if (BOOLEAN_P.equals(fieldType) || BYTE_P.equals(fieldType) || CHAR_P.equals(fieldType) ||
            SHORT_P.equals(fieldType) || INT_P.equals(fieldType))
        {
            return ISTORE;
        }
        if (LONG_P.equals(fieldType))
        {
            return LSTORE;
        }
        if (FLOAT_P.equals(fieldType))
        {
            return FSTORE;
        }
        if (DOUBLE_P.equals(fieldType))
        {
            return DSTORE;
        }
        else
        {
            throw new IllegalStateException("Unknown store method");
        }
    }
    else
    {
        return ASTORE;
    }
}
 
Example 4
Source File: AsmUtils.java    From Diorite with MIT License 5 votes vote down vote up
public static int getLoadCode(TypeDescription fieldType)
{
    if (fieldType.isPrimitive())
    {
        if (BOOLEAN_P.equals(fieldType) || BYTE_P.equals(fieldType) || CHAR_P.equals(fieldType) ||
            SHORT_P.equals(fieldType) || INT_P.equals(fieldType))
        {
            return ILOAD;
        }
        if (LONG_P.equals(fieldType))
        {
            return LLOAD;
        }
        if (FLOAT_P.equals(fieldType))
        {
            return FLOAD;
        }
        if (DOUBLE_P.equals(fieldType))
        {
            return DLOAD;
        }
        else
        {
            throw new IllegalStateException("Unknown load method");
        }
    }
    else
    {
        return ALOAD;
    }
}
 
Example 5
Source File: JavaConstant.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link Class} constant for a primitive type.
 *
 * @param typeDescription The primitive type to represent.
 * @return A dynamically resolved primitive type constant.
 */
public static JavaConstant ofPrimitiveType(TypeDescription typeDescription) {
    if (!typeDescription.isPrimitive()) {
        throw new IllegalArgumentException("Not a primitive type: " + typeDescription);
    }
    return new Dynamic(new ConstantDynamic(typeDescription.getDescriptor(),
            TypeDescription.CLASS.getDescriptor(),
            new Handle(Opcodes.H_INVOKESTATIC,
                    CONSTANT_BOOTSTRAPS,
                    "primitiveClass",
                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class;",
                    false)), TypeDescription.CLASS);
}
 
Example 6
Source File: InstanceCheck.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance check.
 *
 * @param typeDescription The type to apply the instance check against.
 * @return An appropriate stack manipulation.
 */
public static StackManipulation of(TypeDescription typeDescription) {
    if (typeDescription.isPrimitive()) {
        throw new IllegalArgumentException("Cannot check an instance against a primitive type: " + typeDescription);
    }
    return new InstanceCheck(typeDescription);
}
 
Example 7
Source File: TypeCreation.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a type creation for the given type.
 *
 * @param typeDescription The type to be create.
 * @return A stack manipulation that represents the creation of the given type.
 */
public static StackManipulation of(TypeDescription typeDescription) {
    if (typeDescription.isArray() || typeDescription.isPrimitive() || typeDescription.isAbstract()) {
        throw new IllegalArgumentException(typeDescription + " is not instantiable");
    }
    return new TypeCreation(typeDescription);
}
 
Example 8
Source File: Super.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves a type locator based upon an annotation value.
 *
 * @param typeDescription The annotation's value.
 * @return The appropriate type locator.
 */
protected static TypeLocator of(TypeDescription typeDescription) {
    if (typeDescription.represents(void.class)) {
        return ForParameterType.INSTANCE;
    } else if (typeDescription.represents(TargetType.class)) {
        return ForInstrumentedType.INSTANCE;
    } else if (typeDescription.isPrimitive() || typeDescription.isArray()) {
        throw new IllegalStateException("Cannot assign proxy to " + typeDescription);
    } else {
        return new ForType(typeDescription);
    }
}
 
Example 9
Source File: FieldReaderAppender.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod) {
	TypeDescription dispatcherType = persistentFieldAsDefined.getType().isPrimitive()
			? persistentFieldAsDefined.getType().asErasure()
			: TypeDescription.OBJECT;
	// if ( this.$$_hibernate_getInterceptor() != null )
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	Label skip = new Label();
	methodVisitor.visitJumpInsn( Opcodes.IFNULL, skip );
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// this.$$_hibernate_getInterceptor();
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	// .readXXX( self, fieldName, field );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentFieldAsDefined.getName() );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEINTERFACE,
			Type.getInternalName( PersistentAttributeInterceptor.class ),
			"read" + EnhancerImpl.capitalize( dispatcherType.getSimpleName() ),
			Type.getMethodDescriptor(
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( Object.class ),
					Type.getType( String.class ),
					Type.getType( dispatcherType.getDescriptor() )
			),
			true
	);
	// field = (cast) XXX
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// end if
	methodVisitor.visitLabel( skip );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	// return field
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	if ( !persistentField.getType().isPrimitive()
			&& !persistentField.getType().asErasure().getInternalName().equals( persistentFieldAsDefined.getType().asErasure().getInternalName() ) ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentField.getType().asErasure().getInternalName() );
	}
	methodVisitor.visitInsn( Type.getType( persistentFieldAsDefined.getType().asErasure().getDescriptor() ).getOpcode( Opcodes.IRETURN ) );
	return new Size( 4 + persistentFieldAsDefined.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example 10
Source File: FieldWriterAppender.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod) {
	TypeDescription dispatcherType = persistentFieldAsDefined.getType().isPrimitive()
			? persistentFieldAsDefined.getType().asErasure()
			: TypeDescription.OBJECT;
	// if ( this.$$_hibernate_getInterceptor() != null )
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	Label noInterceptor = new Label();
	methodVisitor.visitJumpInsn( Opcodes.IFNULL, noInterceptor );
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// this.$$_hibernate_getInterceptor();
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	// .writeXXX( self, fieldName, field, arg1 );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentFieldAsDefined.getName() );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	methodVisitor.visitVarInsn( Type.getType( dispatcherType.getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEINTERFACE,
			Type.getInternalName( PersistentAttributeInterceptor.class ),
			"write" + EnhancerImpl.capitalize( dispatcherType.getSimpleName() ),
			Type.getMethodDescriptor(
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( Object.class ),
					Type.getType( String.class ),
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( dispatcherType.getDescriptor() )
			),
			true
	);
	// arg1 = (cast) XXX
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// return
	methodVisitor.visitInsn( Opcodes.RETURN );
	// else
	methodVisitor.visitLabel( noInterceptor );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// arg1 = (cast) XXX
	methodVisitor.visitVarInsn( Type.getType( dispatcherType.getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// return
	methodVisitor.visitInsn( Opcodes.RETURN );
	return new Size( 4 + 2 * persistentFieldAsDefined.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example 11
Source File: AsmUtils.java    From Diorite with MIT License 4 votes vote down vote up
/**
 * If given type is primitive type {@link TypeDescription#isPrimitive()} then it will return
 * wrapper type for it. Like: boolean.class {@literal ->} Boolean.class
 * If given type isn't primitive, then it will return given type.
 *
 * @param type
 *         type to get wrapper of it.
 *
 * @return non-primitive type.
 */
public static TypeDescription getWrapperClass(TypeDescription type)
{
    if (! type.isPrimitive())
    {
        return type;
    }
    if (type.equals(BOOLEAN_P))
    {
        return BOOLEAN;
    }
    if (type.equals(BYTE_P))
    {
        return BYTE;
    }
    if (type.equals(SHORT_P))
    {
        return SHORT;
    }
    if (type.equals(CHAR_P))
    {
        return CHARACTER;
    }
    if (type.equals(INT_P))
    {
        return INTEGER;
    }
    if (type.equals(LONG_P))
    {
        return LONG;
    }
    if (type.equals(FLOAT_P))
    {
        return FLOAT;
    }
    if (type.equals(DOUBLE_P))
    {
        return DOUBLE;
    }
    if (type.equals(VOID_P))
    {
        return VOID;
    }
    throw new Error("Unknown primitive type?"); // not possible?
}
 
Example 12
Source File: MethodDelegation.java    From byte-buddy with Apache License 2.0 3 votes vote down vote up
/**
 * Delegates any intercepted method to invoke a {@code static} method that is declared by the supplied type. To be considered
 * a valid delegation target, the target method must be visible and accessible to the instrumented type. This is the case if
 * the target type is either public or in the same package as the instrumented type and if the target method is either public
 * or non-private and in the same package as the instrumented type. Private methods can only be used as a delegation target if
 * the delegation is targeting the instrumented type.
 *
 * @param typeDescription The target type for the delegation.
 * @return A method delegation that redirects method calls to a static method of the supplied type.
 */
public MethodDelegation to(TypeDescription typeDescription) {
    if (typeDescription.isArray()) {
        throw new IllegalArgumentException("Cannot delegate to array " + typeDescription);
    } else if (typeDescription.isPrimitive()) {
        throw new IllegalArgumentException("Cannot delegate to primitive " + typeDescription);
    }
    return new MethodDelegation(ImplementationDelegate.ForStaticMethod.of(typeDescription.getDeclaredMethods().filter(isStatic().and(matcher)),
            TargetMethodAnnotationDrivenBinder.of(parameterBinders)), parameterBinders, ambiguityResolver, bindingResolver);
}