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

The following examples show how to use net.bytebuddy.description.type.TypeDescription#represents() . 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: JavaConstant.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves this {@link Dynamic} constant to resolve the returned instance to the supplied type. The type must be a subtype of the
 * bootstrap method's return type. Constructors cannot be resolved to a different type.
 *
 * @param typeDescription The type to resolve the bootstrapped value to.
 * @return This dynamic constant but resolved to the supplied type.
 */
public JavaConstant withType(TypeDescription typeDescription) {
    if (typeDescription.represents(void.class)) {
        throw new IllegalArgumentException("Constant value cannot represent void");
    } else if (value.getBootstrapMethod().getName().equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME)
            ? !this.typeDescription.isAssignableTo(typeDescription)
            : (!typeDescription.asBoxed().isInHierarchyWith(this.typeDescription.asBoxed()))) {
        throw new IllegalArgumentException(typeDescription + " is not compatible with bootstrapped type " + this.typeDescription);
    }
    Object[] bootstrapMethodArgument = new Object[value.getBootstrapMethodArgumentCount()];
    for (int index = 0; index < value.getBootstrapMethodArgumentCount(); index++) {
        bootstrapMethodArgument[index] = value.getBootstrapMethodArgument(index);
    }
    return new Dynamic(new ConstantDynamic(value.getName(),
            typeDescription.getDescriptor(),
            value.getBootstrapMethod(),
            bootstrapMethodArgument), typeDescription);
}
 
Example 2
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if this method is a bootstrap method given the supplied arguments. This method does not implement a full check but assumes that
 * {@link MethodDescription.AbstractBase#isBootstrap(TypeDescription)} is invoked, as well.
 *
 * @param types The types of the explicit arguments that are supplied to the bootstrap method.
 * @return {@code true} if this method is a bootstrap method for the supplied arguments.
 */
private boolean isBootstrap(List<? extends TypeDefinition> types) {
    TypeList targets = getParameters().asTypeList().asErasures();
    if (targets.size() < 4) {
        return types.isEmpty() || targets.get(targets.size() - 1).represents(Object[].class);
    } else {
        Iterator<TypeDescription> iterator = targets.subList(3, targets.size()).iterator();
        for (TypeDefinition type : types) {
            if (!iterator.hasNext()) {
                return false;
            }
            TypeDescription target = iterator.next();
            if (!iterator.hasNext() && target.represents(Object[].class)) {
                return true;
            } else if (!type.asErasure().isAssignableTo(target)) {
                return false;
            }
        }
        if (iterator.hasNext()) {
            return iterator.next().represents(Object[].class) && !iterator.hasNext();
        } else {
            return true;
        }
    }
}
 
Example 3
Source File: AnnotationAppender.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Performs the writing of a given annotation value to an annotation visitor.
 *
 * @param annotationVisitor The annotation visitor the write process is to be applied on.
 * @param valueType         The type of the annotation value.
 * @param name              The name of the annotation type.
 * @param value             The annotation's value.
 */
public static void apply(AnnotationVisitor annotationVisitor, TypeDescription valueType, String name, Object value) {
    if (valueType.isArray()) { // The Android emulator reads annotation arrays as annotation types. Therefore, this check needs to come first.
        AnnotationVisitor arrayVisitor = annotationVisitor.visitArray(name);
        int length = Array.getLength(value);
        TypeDescription componentType = valueType.getComponentType();
        for (int index = 0; index < length; index++) {
            apply(arrayVisitor, componentType, NO_NAME, Array.get(value, index));
        }
        arrayVisitor.visitEnd();
    } else if (valueType.isAnnotation()) {
        handle(annotationVisitor.visitAnnotation(name, valueType.getDescriptor()), (AnnotationDescription) value, AnnotationValueFilter.Default.APPEND_DEFAULTS);
    } else if (valueType.isEnum()) {
        annotationVisitor.visitEnum(name, valueType.getDescriptor(), ((EnumerationDescription) value).getValue());
    } else if (valueType.represents(Class.class)) {
        annotationVisitor.visit(name, Type.getType(((TypeDescription) value).getDescriptor()));
    } else {
        annotationVisitor.visit(name, value);
    }
}
 
Example 4
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Resolution resolve(TypeDescription targetType,
                          ByteCodeElement target,
                          TypeList.Generic parameters,
                          TypeDescription.Generic current,
                          Map<Integer, Integer> offsets,
                          int freeOffset) {
    return targetType.represents(void.class)
            ? this
            : new Simple(new StackManipulation.Compound(Removal.of(targetType), stackManipulation), resultType);
}
 
Example 5
Source File: AnnotationValue.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the supplied type description's component tag.
 *
 * @param typeDescription The type to resolve.
 * @return The character that describes the component tag as an {@code int} to ease concatenation.
 */
public int toComponentTag(TypeDescription typeDescription) {
    if (typeDescription.represents(boolean.class)) {
        return 'Z';
    } else if (typeDescription.represents(byte.class)) {
        return 'B';
    } else if (typeDescription.represents(short.class)) {
        return 'S';
    } else if (typeDescription.represents(char.class)) {
        return 'C';
    } else if (typeDescription.represents(int.class)) {
        return 'I';
    } else if (typeDescription.represents(long.class)) {
        return 'J';
    } else if (typeDescription.represents(float.class)) {
        return 'F';
    } else if (typeDescription.represents(double.class)) {
        return 'D';
    } else if (typeDescription.represents(String.class)) {
        return 's';
    } else if (typeDescription.represents(Class.class)) {
        return 'c';
    } else if (typeDescription.isEnum()) {
        return 'e';
    } else if (typeDescription.isAnnotation()) {
        return '@';
    } else if (typeDescription.isArray()) {
        return '[';
    } else {
        throw new IllegalArgumentException("Not an annotation component: " + typeDescription);
    }
}
 
Example 6
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean isDefaultValue(AnnotationValue<?, ?> annotationValue) {
    if (!isDefaultValue()) {
        return false;
    }
    TypeDescription returnType = getReturnType().asErasure();
    Object value = annotationValue.resolve();
    return (returnType.represents(boolean.class) && value instanceof Boolean)
            || (returnType.represents(byte.class) && value instanceof Byte)
            || (returnType.represents(char.class) && value instanceof Character)
            || (returnType.represents(short.class) && value instanceof Short)
            || (returnType.represents(int.class) && value instanceof Integer)
            || (returnType.represents(long.class) && value instanceof Long)
            || (returnType.represents(float.class) && value instanceof Float)
            || (returnType.represents(double.class) && value instanceof Double)
            || (returnType.represents(String.class) && value instanceof String)
            || (returnType.isAssignableTo(Enum.class) && value instanceof EnumerationDescription && isEnumerationType(returnType, (EnumerationDescription) value))
            || (returnType.isAssignableTo(Annotation.class) && value instanceof AnnotationDescription && isAnnotationType(returnType, (AnnotationDescription) value))
            || (returnType.represents(Class.class) && value instanceof TypeDescription)
            || (returnType.represents(boolean[].class) && value instanceof boolean[])
            || (returnType.represents(byte[].class) && value instanceof byte[])
            || (returnType.represents(char[].class) && value instanceof char[])
            || (returnType.represents(short[].class) && value instanceof short[])
            || (returnType.represents(int[].class) && value instanceof int[])
            || (returnType.represents(long[].class) && value instanceof long[])
            || (returnType.represents(float[].class) && value instanceof float[])
            || (returnType.represents(double[].class) && value instanceof double[])
            || (returnType.represents(String[].class) && value instanceof String[])
            || (returnType.isAssignableTo(Enum[].class) && value instanceof EnumerationDescription[] && isEnumerationType(returnType.getComponentType(), (EnumerationDescription[]) value))
            || (returnType.isAssignableTo(Annotation[].class) && value instanceof AnnotationDescription[] && isAnnotationType(returnType.getComponentType(), (AnnotationDescription[]) value))
            || (returnType.represents(Class[].class) && value instanceof TypeDescription[]);
}
 
Example 7
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 8
Source File: TargetType.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves the given type description to the supplied target type if it represents the {@link TargetType} placeholder.
 * Array types are resolved to their component type and rebuilt as an array of the actual target type, if necessary.
 *
 * @param typeDescription The type description that might represent the {@link TargetType} placeholder.
 * @param targetType      The actual target type.
 * @return A description of the resolved type.
 */
public static TypeDescription resolve(TypeDescription typeDescription, TypeDescription targetType) {
    int arity = 0;
    TypeDescription componentType = typeDescription;
    while (componentType.isArray()) {
        componentType = componentType.getComponentType();
        arity++;
    }
    return componentType.represents(TargetType.class)
            ? TypeDescription.ArrayProjection.of(targetType, arity)
            : typeDescription;
}
 
Example 9
Source File: ToStringMethod.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves an appropriate value resolver for a given type.
 *
 * @param typeDescription The type for which to resolve a value resolver.
 * @return An appropriate stack manipulation.
 */
protected static StackManipulation of(TypeDescription typeDescription) {
    if (typeDescription.represents(boolean.class)) {
        return BOOLEAN;
    } else if (typeDescription.represents(char.class)) {
        return CHARACTER;
    } else if (typeDescription.represents(byte.class)
            || typeDescription.represents(short.class)
            || typeDescription.represents(int.class)) {
        return INTEGER;
    } else if (typeDescription.represents(long.class)) {
        return LONG;
    } else if (typeDescription.represents(float.class)) {
        return FLOAT;
    } else if (typeDescription.represents(double.class)) {
        return DOUBLE;
    } else if (typeDescription.represents(String.class)) {
        return STRING;
    } else if (typeDescription.isAssignableTo(CharSequence.class)) {
        return CHARACTER_SEQUENCE;
    } else if (typeDescription.represents(boolean[].class)) {
        return BOOLEAN_ARRAY;
    } else if (typeDescription.represents(byte[].class)) {
        return BYTE_ARRAY;
    } else if (typeDescription.represents(short[].class)) {
        return SHORT_ARRAY;
    } else if (typeDescription.represents(char[].class)) {
        return CHARACTER_ARRAY;
    } else if (typeDescription.represents(int[].class)) {
        return INTEGER_ARRAY;
    } else if (typeDescription.represents(long[].class)) {
        return LONG_ARRAY;
    } else if (typeDescription.represents(float[].class)) {
        return FLOAT_ARRAY;
    } else if (typeDescription.represents(double[].class)) {
        return DOUBLE_ARRAY;
    } else if (typeDescription.isArray()) {
        return typeDescription.getComponentType().isArray()
                ? NESTED_ARRAY
                : REFERENCE_ARRAY;
    } else {
        return OBJECT;
    }
}
 
Example 10
Source File: PluginEngineDefaultTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public boolean matches(TypeDescription target) {
    return target.represents(Sample.class);
}
 
Example 11
Source File: PluginEngineDefaultTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public boolean matches(TypeDescription target) {
    return target.represents(Sample.class);
}
 
Example 12
Source File: PluginEngineDefaultTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public boolean matches(TypeDescription target) {
    return target.represents(Sample.class);
}