Java Code Examples for net.bytebuddy.pool.TypePool#Resolution

The following examples show how to use net.bytebuddy.pool.TypePool#Resolution . 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: TypeReferenceAdjustment.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitEnd() {
    for (String observedType : observedTypes) {
        if (visitedInnerTypes.add(observedType)) {
            TypePool.Resolution resolution = typePool.describe(observedType.replace('/', '.'));
            if (resolution.isResolved()) {
                TypeDescription typeDescription = resolution.resolve();
                if (!filter.matches(typeDescription)) {
                    while (typeDescription != null && typeDescription.isNestedClass()) {
                        super.visitInnerClass(typeDescription.getInternalName(),
                                typeDescription.isMemberType()
                                        ? typeDescription.getDeclaringType().getInternalName()
                                        : null,
                                typeDescription.isAnonymousType()
                                        ? null
                                        : typeDescription.getSimpleName(),
                                typeDescription.getModifiers());
                        try {
                            do {
                                typeDescription = typeDescription.getEnclosingType();
                            } while (typeDescription != null && !visitedInnerTypes.add(typeDescription.getInternalName()));
                        } catch (RuntimeException exception) {
                            if (strict) {
                                throw exception;
                            } else {
                                break;
                            }
                        }
                    }
                }
            } else if (strict) {
                throw new IllegalStateException("Could not locate type for: " + observedType.replace('/', '.'));
            }
        }
    }
    super.visitEnd();
}
 
Example 2
Source File: Plugin.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Callable<Dispatcher.Materializable> call() throws Exception {
    listener.onDiscovery(typeName);
    TypePool.Resolution resolution = typePool.describe(typeName);
    if (resolution.isResolved()) {
        TypeDescription typeDescription = resolution.resolve();
        try {
            if (!ignoredTypeMatcher.matches(typeDescription)) {
                for (WithPreprocessor preprocessor : preprocessors) {
                    preprocessor.onPreprocess(typeDescription, classFileLocator);
                }
                return new Resolved(typeDescription);
            } else {
                return new Ignored(typeDescription);
            }
        } catch (Throwable throwable) {
            listener.onComplete(typeDescription);
            if (throwable instanceof Exception) {
                throw (Exception) throwable;
            } else if (throwable instanceof Error) {
                throw (Error) throwable;
            } else {
                throw new IllegalStateException(throwable);
            }
        }
    } else {
        return new Unresolved();
    }
}
 
Example 3
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 4
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMethodInsn(int opcode, String owner, String internalName, String descriptor, boolean isInterface) {
    TypePool.Resolution resolution = typePool.describe(owner.replace('/', '.'));
    if (resolution.isResolved()) {
        MethodList<?> candidates;
        if (opcode == Opcodes.INVOKESPECIAL && internalName.equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME)) {
            candidates = resolution.resolve().getDeclaredMethods().filter(strict
                    ? ElementMatchers.<MethodDescription>isConstructor().and(hasDescriptor(descriptor))
                    : ElementMatchers.<MethodDescription>failSafe(isConstructor().and(hasDescriptor(descriptor))));
        } else if (opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKESPECIAL) {
            candidates = resolution.resolve().getDeclaredMethods().filter(strict
                    ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor))
                    : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
        } else if (virtualPrivateCalls) {
            candidates = resolution.resolve().getDeclaredMethods().filter(strict
                    ? ElementMatchers.<MethodDescription>isPrivate().and(not(isStatic())).and(named(internalName).and(hasDescriptor(descriptor)))
                    : ElementMatchers.<MethodDescription>failSafe(isPrivate().<MethodDescription>and(not(isStatic())).and(named(internalName).and(hasDescriptor(descriptor)))));
            if (candidates.isEmpty()) {
                candidates = methodGraphCompiler.compile(resolution.resolve(), instrumentedType).listNodes().asMethodList().filter(strict
                        ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor))
                        : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
            }
        } else {
            candidates = methodGraphCompiler.compile(resolution.resolve(), instrumentedType).listNodes().asMethodList().filter(strict
                    ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor))
                    : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
        }
        if (!candidates.isEmpty()) {
            Replacement.Binding binding = replacement.bind(instrumentedType,
                    instrumentedMethod,
                    resolution.resolve(),
                    candidates.getOnly(),
                    Replacement.InvocationType.of(opcode, candidates.getOnly()));
            if (binding.isBound()) {
                stackSizeBuffer = Math.max(stackSizeBuffer, binding.make(
                        candidates.getOnly().isStatic() || candidates.getOnly().isConstructor()
                                ? candidates.getOnly().getParameters().asTypeList()
                                : new TypeList.Generic.Explicit(CompoundList.of(resolution.resolve(), candidates.getOnly().getParameters().asTypeList())),
                        candidates.getOnly().isConstructor()
                                ? candidates.getOnly().getDeclaringType().asGenericType()
                                : candidates.getOnly().getReturnType(),
                        getFreeOffset())
                        .apply(new LocalVariableTracingMethodVisitor(mv), implementationContext).getMaximalSize() - (candidates.getOnly().isConstructor()
                        ? StackSize.SINGLE
                        : candidates.getOnly().getReturnType().getStackSize()).getSize());
                if (candidates.getOnly().isConstructor()) {
                    stackSizeBuffer = Math.max(stackSizeBuffer, new StackManipulation.Compound(
                            Duplication.SINGLE.flipOver(TypeDescription.OBJECT),
                            Removal.SINGLE,
                            Removal.SINGLE,
                            Duplication.SINGLE.flipOver(TypeDescription.OBJECT),
                            Removal.SINGLE,
                            Removal.SINGLE
                    ).apply(mv, implementationContext).getMaximalSize() + StackSize.SINGLE.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.visitMethodInsn(opcode, owner, internalName, descriptor, isInterface);
}