Java Code Examples for net.bytebuddy.description.type.TypeDescription#STRING

The following examples show how to use net.bytebuddy.description.type.TypeDescription#STRING . 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: FixedValue.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Returns a fixed value from any intercepted method. The fixed value is stored in the constant pool if this is possible.
 * Java is capable of storing any primitive value, {@link String} values and {@link Class} references in the constant pool.
 * Since Java 7, {@code MethodHandle} as well as {@code MethodType} references are also supported. Alternatively, the fixed
 * value is stored in a static field.
 * </p>
 * <p>
 * When a value is stored in the class's constant pool, its identity is lost. If an object's identity is important, the
 * {@link FixedValue#reference(Object)} method should be used instead.
 * </p>
 * <p>
 * <b>Important</b>: When supplying a method handle or a method type, all types that are implied must be visible to the instrumented
 * type or an {@link IllegalAccessException} will be thrown at runtime.
 * </p>
 *
 * @param fixedValue The fixed value to return from the method.
 * @return An implementation for the given {@code value}.
 */
public static AssignerConfigurable value(Object fixedValue) {
    Class<?> type = fixedValue.getClass();
    if (type == String.class) {
        return new ForPoolValue(new TextConstant((String) fixedValue), TypeDescription.STRING);
    } else if (type == Class.class) {
        return new ForPoolValue(ClassConstant.of(TypeDescription.ForLoadedType.of((Class<?>) fixedValue)), TypeDescription.CLASS);
    } else if (type == Boolean.class) {
        return new ForPoolValue(IntegerConstant.forValue((Boolean) fixedValue), boolean.class);
    } else if (type == Byte.class) {
        return new ForPoolValue(IntegerConstant.forValue((Byte) fixedValue), byte.class);
    } else if (type == Short.class) {
        return new ForPoolValue(IntegerConstant.forValue((Short) fixedValue), short.class);
    } else if (type == Character.class) {
        return new ForPoolValue(IntegerConstant.forValue((Character) fixedValue), char.class);
    } else if (type == Integer.class) {
        return new ForPoolValue(IntegerConstant.forValue((Integer) fixedValue), int.class);
    } else if (type == Long.class) {
        return new ForPoolValue(LongConstant.forValue((Long) fixedValue), long.class);
    } else if (type == Float.class) {
        return new ForPoolValue(FloatConstant.forValue((Float) fixedValue), float.class);
    } else if (type == Double.class) {
        return new ForPoolValue(DoubleConstant.forValue((Double) fixedValue), double.class);
    } else if (JavaType.METHOD_HANDLE.getTypeStub().isAssignableFrom(type)) {
        return new ForPoolValue(new JavaConstantValue(JavaConstant.MethodHandle.ofLoaded(fixedValue)), type);
    } else if (JavaType.METHOD_TYPE.getTypeStub().represents(type)) {
        return new ForPoolValue(new JavaConstantValue(JavaConstant.MethodType.ofLoaded(fixedValue)), type);
    } else {
        return reference(fixedValue);
    }
}
 
Example 2
Source File: ElementMatchersTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testHasSignature() throws Exception {
    MethodDescription.SignatureToken signatureToken = new MethodDescription.SignatureToken("toString", TypeDescription.STRING, Collections.<TypeDescription>emptyList());
    assertThat(ElementMatchers.hasSignature(signatureToken)
            .matches(new MethodDescription.ForLoadedMethod(Object.class.getDeclaredMethod("toString"))), is(true));
    assertThat(ElementMatchers.hasSignature(signatureToken)
            .matches(new MethodDescription.ForLoadedMethod(Object.class.getDeclaredMethod("hashCode"))), is(false));
}
 
Example 3
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Resolved resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) {
    return new Resolved.Simple(new TextConstant(value), TypeDescription.STRING);
}
 
Example 4
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ParameterBinding<?> bind(AnnotationDescription.Loadable<S> annotation,
                                MethodDescription source,
                                ParameterDescription target,
                                Implementation.Target implementationTarget,
                                Assigner assigner,
                                Assigner.Typing typing) {
    Object value = bind(annotation, source, target);
    if (value == null) {
        return new ParameterBinding.Anonymous(DefaultValue.of(target.getType()));
    }
    StackManipulation stackManipulation;
    TypeDescription suppliedType;
    if (value instanceof Boolean) {
        stackManipulation = IntegerConstant.forValue((Boolean) value);
        suppliedType = TypeDescription.ForLoadedType.of(boolean.class);
    } else if (value instanceof Byte) {
        stackManipulation = IntegerConstant.forValue((Byte) value);
        suppliedType = TypeDescription.ForLoadedType.of(byte.class);
    } else if (value instanceof Short) {
        stackManipulation = IntegerConstant.forValue((Short) value);
        suppliedType = TypeDescription.ForLoadedType.of(short.class);
    } else if (value instanceof Character) {
        stackManipulation = IntegerConstant.forValue((Character) value);
        suppliedType = TypeDescription.ForLoadedType.of(char.class);
    } else if (value instanceof Integer) {
        stackManipulation = IntegerConstant.forValue((Integer) value);
        suppliedType = TypeDescription.ForLoadedType.of(int.class);
    } else if (value instanceof Long) {
        stackManipulation = LongConstant.forValue((Long) value);
        suppliedType = TypeDescription.ForLoadedType.of(long.class);
    } else if (value instanceof Float) {
        stackManipulation = FloatConstant.forValue((Float) value);
        suppliedType = TypeDescription.ForLoadedType.of(float.class);
    } else if (value instanceof Double) {
        stackManipulation = DoubleConstant.forValue((Double) value);
        suppliedType = TypeDescription.ForLoadedType.of(double.class);
    } else if (value instanceof String) {
        stackManipulation = new TextConstant((String) value);
        suppliedType = TypeDescription.STRING;
    } else if (value instanceof Class) {
        stackManipulation = ClassConstant.of(TypeDescription.ForLoadedType.of((Class<?>) value));
        suppliedType = TypeDescription.CLASS;
    } else if (value instanceof TypeDescription) {
        stackManipulation = ClassConstant.of((TypeDescription) value);
        suppliedType = TypeDescription.CLASS;
    } else if (JavaType.METHOD_HANDLE.isInstance(value)) {
        stackManipulation = new JavaConstantValue(JavaConstant.MethodHandle.ofLoaded(value));
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (value instanceof JavaConstant.MethodHandle) {
        stackManipulation = new JavaConstantValue((JavaConstant.MethodHandle) value);
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (JavaType.METHOD_TYPE.isInstance(value)) {
        stackManipulation = new JavaConstantValue(JavaConstant.MethodType.ofLoaded(value));
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (value instanceof JavaConstant.MethodType) {
        stackManipulation = new JavaConstantValue((JavaConstant.MethodType) value);
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else {
        throw new IllegalStateException("Not able to save in class's constant pool: " + value);
    }
    return new ParameterBinding.Anonymous(new StackManipulation.Compound(
            stackManipulation,
            assigner.assign(suppliedType.asGenericType(), target.getType(), typing)
    ));
}