Java Code Examples for net.bytebuddy.description.type.TypeList#Generic

The following examples show how to use net.bytebuddy.description.type.TypeList#Generic . 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: HierarchyMatch.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isMatch(TypeDescription typeDescription) {
    List<String> parentTypes = new ArrayList<String>(Arrays.asList(this.parentTypes));

    TypeList.Generic implInterfaces = typeDescription.getInterfaces();
    for (TypeDescription.Generic implInterface : implInterfaces) {
        matchHierarchyClass(implInterface, parentTypes);
    }

    if (typeDescription.getSuperClass() != null) {
        matchHierarchyClass(typeDescription.getSuperClass(), parentTypes);
    }

    return parentTypes.size() == 0;

}
 
Example 2
Source File: MethodInheritanceAnnotationMatcher.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private boolean recursiveMatches(TypeDefinition typeDefinition, String methodName, ParameterList<?> parameters) {
    TypeList.Generic interfaces = typeDefinition.getInterfaces();
    for (TypeDescription.Generic implInterface : interfaces) {
        if (recursiveMatches(implInterface, methodName, parameters)) {
            return true;
        }
        MethodList<MethodDescription.InGenericShape> declaredMethods = implInterface.getDeclaredMethods();
        for (MethodDescription declaredMethod : declaredMethods) {
            if (Objects.equals(declaredMethod.getName(), methodName) && parameterEquals(parameters, declaredMethod.getParameters())) {
                return matcher.matches(declaredMethod.getDeclaredAnnotations());
            }
        }
    }
    return false;
}
 
Example 3
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation resolve(TypeDescription targetType,
                                 ByteCodeElement target,
                                 TypeList.Generic parameters,
                                 TypeDescription.Generic result,
                                 int freeOffset) {
    List<StackManipulation> stackManipulations = new ArrayList<StackManipulation>(parameters.size());
    for (int index = parameters.size() - 1; index >= 0; index--) {
        stackManipulations.add(Removal.of(parameters.get(index)));
    }
    return new StackManipulation.Compound(CompoundList.of(stackManipulations, DefaultValue.of(result.asErasure())));
}
 
Example 4
Source File: ParameterList.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic asTypeList() {
    List<TypeDescription.Generic> types = new ArrayList<TypeDescription.Generic>(size());
    for (ParameterDescription parameterDescription : this) {
        types.add(parameterDescription.getType());
    }
    return new TypeList.Generic.Explicit(types);
}
 
Example 5
Source File: GenericTypeAwareAssigner.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Boolean onParameterizedType(TypeDescription.Generic parameterizedType) {
    Queue<TypeDescription.Generic> candidates = new LinkedList<TypeDescription.Generic>(Collections.singleton(typeDescription));
    Set<TypeDescription> previous = new HashSet<TypeDescription>(Collections.singleton(typeDescription.asErasure()));
    do {
        TypeDescription.Generic candidate = candidates.remove();
        if (candidate.asErasure().equals(parameterizedType.asErasure())) {
            if (candidate.getSort().isNonGeneric()) {
                return true;
            } else /* if (candidate.getSort().isParameterized() */ {
                TypeList.Generic source = candidate.getTypeArguments(), target = parameterizedType.getTypeArguments();
                int size = target.size();
                if (source.size() != size) {
                    return false;
                }
                for (int index = 0; index < size; index++) {
                    if (!source.get(index).accept(new IsAssignableToVisitor(target.get(index), false))) {
                        return false;
                    }
                }
                TypeDescription.Generic ownerType = parameterizedType.getOwnerType();
                return ownerType == null || ownerType.accept(new IsAssignableToVisitor(parameterizedType.getOwnerType()));
            }
        } else if (polymorphic) {
            TypeDescription.Generic superClass = candidate.getSuperClass();
            if (superClass != null && previous.add(superClass.asErasure())) {
                candidates.add(superClass);
            }
            for (TypeDescription.Generic anInterface : candidate.getInterfaces()) {
                if (previous.add(anInterface.asErasure())) {
                    candidates.add(anInterface);
                }
            }
        }
    } while (!candidates.isEmpty());
    return false;
}
 
Example 6
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String getGenericSignature() {
    try {
        SignatureWriter signatureWriter = new SignatureWriter();
        boolean generic = false;
        for (TypeDescription.Generic typeVariable : getTypeVariables()) {
            signatureWriter.visitFormalTypeParameter(typeVariable.getSymbol());
            boolean classBound = true;
            for (TypeDescription.Generic upperBound : typeVariable.getUpperBounds()) {
                upperBound.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(classBound
                        ? signatureWriter.visitClassBound()
                        : signatureWriter.visitInterfaceBound()));
                classBound = false;
            }
            generic = true;
        }
        for (TypeDescription.Generic parameterType : getParameters().asTypeList()) {
            parameterType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(signatureWriter.visitParameterType()));
            generic = generic || !parameterType.getSort().isNonGeneric();
        }
        TypeDescription.Generic returnType = getReturnType();
        returnType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(signatureWriter.visitReturnType()));
        generic = generic || !returnType.getSort().isNonGeneric();
        TypeList.Generic exceptionTypes = getExceptionTypes();
        if (!exceptionTypes.filter(not(ofSort(TypeDefinition.Sort.NON_GENERIC))).isEmpty()) {
            for (TypeDescription.Generic exceptionType : exceptionTypes) {
                exceptionType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(signatureWriter.visitExceptionType()));
                generic = generic || !exceptionType.getSort().isNonGeneric();
            }
        }
        return generic
                ? signatureWriter.toString()
                : NON_GENERIC_SIGNATURE;
    } catch (GenericSignatureFormatError ignored) {
        return NON_GENERIC_SIGNATURE;
    }
}
 
Example 7
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    if (TypeDescription.AbstractBase.RAW_TYPES) {
        return new TypeList.Generic.Empty();
    }
    return TypeList.Generic.ForLoadedTypes.OfTypeVariables.of(method);
}
 
Example 8
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitFieldInsn(int opcode, String owner, String internalName, String descriptor) {
    TypePool.Resolution resolution = typePool.describe(owner.replace('/', '.'));
    if (resolution.isResolved()) {
        FieldList<FieldDescription.InDefinedShape> candidates = resolution.resolve().getDeclaredFields().filter(strict
                ? ElementMatchers.<FieldDescription>named(internalName).and(hasDescriptor(descriptor))
                : ElementMatchers.<FieldDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
        if (!candidates.isEmpty()) {
            Replacement.Binding binding = replacement.bind(instrumentedType,
                    instrumentedMethod,
                    candidates.getOnly(),
                    opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC);
            if (binding.isBound()) {
                TypeList.Generic parameters;
                TypeDescription.Generic result;
                switch (opcode) {
                    case Opcodes.PUTFIELD:
                        parameters = new TypeList.Generic.Explicit(candidates.getOnly().getDeclaringType(), candidates.getOnly().getType());
                        result = TypeDescription.Generic.VOID;
                        break;
                    case Opcodes.PUTSTATIC:
                        parameters = new TypeList.Generic.Explicit(candidates.getOnly().getType());
                        result = TypeDescription.Generic.VOID;
                        break;
                    case Opcodes.GETFIELD:
                        parameters = new TypeList.Generic.Explicit(candidates.getOnly().getDeclaringType());
                        result = candidates.getOnly().getType();
                        break;
                    case Opcodes.GETSTATIC:
                        parameters = new TypeList.Generic.Empty();
                        result = candidates.getOnly().getType();
                        break;
                    default:
                        throw new IllegalStateException("Unexpected opcode: " + opcode);
                }
                stackSizeBuffer = Math.max(stackSizeBuffer, binding.make(parameters, result, getFreeOffset())
                        .apply(new LocalVariableTracingMethodVisitor(mv), implementationContext)
                        .getMaximalSize() - result.getStackSize().getSize());
                return;
            }
        } else if (strict) {
            throw new IllegalStateException("Could not resolve " + owner.replace('/', '.')
                    + "." + internalName + descriptor + " using " + typePool);
        }
    } else if (strict) {
        throw new IllegalStateException("Could not resolve " + owner.replace('/', '.') + " using " + typePool);
    }
    super.visitFieldInsn(opcode, owner, internalName, descriptor);
}
 
Example 9
Source File: ParameterList.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic asTypeList() {
    return new TypeList.Generic.Empty();
}
 
Example 10
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation make(TypeList.Generic parameters, TypeDescription.Generic result, int freeOffset) {
    throw new IllegalStateException("Cannot resolve unresolved binding");
}
 
Example 11
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getExceptionTypes() {
    return new TypeList.Generic.OfConstructorExceptionTypes(constructor);
}
 
Example 12
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    return new TypeList.Generic.Empty();
}
 
Example 13
Source File: Transformer.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    return new TypeList.Generic.ForDetachedTypes.OfTypeVariables(this, token.getTypeVariableTokens(), new AttachmentVisitor());
}
 
Example 14
Source File: Implementation.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    return new TypeList.Generic.Empty();
}
 
Example 15
Source File: MethodRebaseResolver.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getExceptionTypes() {
    return methodDescription.getExceptionTypes().asRawTypes();
}
 
Example 16
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    return TypeList.Generic.ForDetachedTypes.attachVariables(this, typeVariables);
}
 
Example 17
Source File: MethodDescription.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    return TypeList.Generic.ForLoadedTypes.OfTypeVariables.of(constructor);
}
 
Example 18
Source File: MethodRebaseResolver.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TypeList.Generic getTypeVariables() {
    return new TypeList.Generic.Empty();
}
 
Example 19
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Resolves the method to substitute with.
 *
 * @param targetType The target type on which a member is accessed.
 * @param target     The target field, method or constructor that is substituted,
 * @param parameters All parameters that serve as input to this access.
 * @param result     The result that is expected from the interaction or {@code void} if no result is expected.
 * @return The field to substitute with.
 */
MethodDescription resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result);
 
Example 20
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Resolves the field to substitute with.
 *
 * @param targetType The target type on which a member is accessed.
 * @param target     The target field, method or constructor that is substituted,
 * @param parameters All parameters that serve as input to this access.
 * @param result     The result that is expected from the interaction or {@code void} if no result is expected.
 * @return The field to substitute with.
 */
FieldDescription resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result);