/** * {@inheritDoc} */ public MethodDescription.InDefinedShape get(int index) { return index < constructors.size() ? new MethodDescription.ForLoadedConstructor(constructors.get(index)) : new MethodDescription.ForLoadedMethod(methods.get(index - constructors.size())); }
/** * {@inheritDoc} */ @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "The implicit field type casting is not understood by Findbugs") public MethodDescription.InDefinedShape getDeclaringMethod() { return new MethodDescription.ForLoadedConstructor(executable); }
/** * {@inheritDoc} */ public TypeVariableSource getTypeVariableSource() { GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration(); if (genericDeclaration instanceof Class) { return TypeDescription.ForLoadedType.of((Class<?>) genericDeclaration); } else if (genericDeclaration instanceof Method) { return new MethodDescription.ForLoadedMethod((Method) genericDeclaration); } else if (genericDeclaration instanceof Constructor) { return new MethodDescription.ForLoadedConstructor((Constructor<?>) genericDeclaration); } else { throw new IllegalStateException("Unknown declaration: " + genericDeclaration); } }
/** * {@inheritDoc} */ public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) { try { return new StackManipulation.Compound( TypeCreation.of(TypeDescription.ForLoadedType.of(ObjectInputStream.class)), Duplication.SINGLE, TypeCreation.of(TypeDescription.ForLoadedType.of(ByteArrayInputStream.class)), Duplication.SINGLE, new TextConstant(serialization), new TextConstant(CHARSET), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(String.class.getMethod("getBytes", String.class))), MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ByteArrayInputStream.class.getConstructor(byte[].class))), MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ObjectInputStream.class.getConstructor(InputStream.class))), MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(ObjectInputStream.class.getMethod("readObject"))) ).apply(methodVisitor, implementationContext); } catch (NoSuchMethodException exception) { throw new IllegalStateException("Could not locate Java API method", exception); } } }
/** * Binds the supplied annotation to the supplied parameter's argument. * * @param type The type of the annotation being bound. * @param constructor The constructor that defines the parameter. * @param index The index of the parameter. * @param <T> The annotation type. * @return A new builder for an advice that considers the supplied annotation type during binding. */ public <T extends Annotation> WithCustomMapping bind(Class<T> type, Constructor<?> constructor, int index) { if (index < 0) { throw new IllegalArgumentException("A parameter cannot be negative: " + index); } else if (constructor.getParameterTypes().length <= index) { throw new IllegalArgumentException(constructor + " does not declare a parameter with index " + index); } return bind(type, new MethodDescription.ForLoadedConstructor(constructor).getParameters().get(index)); }
/** * Implements the instrumented method with a dynamic method invocation which is linked at runtime using the * specified bootstrap constructor. * * @param constructor The bootstrap constructor that is used to link the instrumented method. * @param rawArgument The arguments that are handed to the bootstrap constructor. Any argument must be saved in the * constant pool, i.e. primitive types (represented as their wrapper types) with a size of * at least 32 bit, {@link java.lang.String} types, {@link java.lang.Class} types as well * as {@code MethodType} and {@code MethodHandle} instances. In order to avoid class loading, * it is also possible to supply unloaded types as {@link TypeDescription}, {@link JavaConstant} * instances. The latter do also allow for the representation of dynamic constants. * @return An implementation where a {@code this} reference, if available, and all arguments of the * instrumented method are passed to the bootstrapped method unless explicit parameters are specified. */ public static WithImplicitTarget bootstrap(Constructor<?> constructor, Object... rawArgument) { return bootstrap(new MethodDescription.ForLoadedConstructor(constructor), rawArgument); }
/** * Implements the instrumented method with a dynamic method invocation which is linked at runtime using the * specified bootstrap constructor. * * @param constructor The bootstrap constructor that is used to link the instrumented method. * @param rawArguments The arguments that are handed to the bootstrap constructor. Any argument must be saved in the * constant pool, i.e. primitive types (represented as their wrapper types) with a size of * at least 32 bit, {@link java.lang.String} types, {@link java.lang.Class} types as well * as {@code MethodType} and {@code MethodHandle} instances. In order to avoid class loading, * it is also possible to supply unloaded types as {@link TypeDescription}, {@link JavaConstant} * instances. The latter do also allow for the representation of dynamic constants. * @return An implementation where a {@code this} reference, if available, and all arguments of the * instrumented method are passed to the bootstrapped method unless explicit parameters are specified. */ public static WithImplicitTarget bootstrap(Constructor<?> constructor, List<?> rawArguments) { return bootstrap(new MethodDescription.ForLoadedConstructor(constructor), rawArguments); }
/** * <p> * Invokes the given constructor on the instance of the instrumented type. * </p> * <p> * <b>Important</b>: A constructor invocation can only be applied within another constructor to invoke the super constructor or an auxiliary * constructor. To construct a new instance, use {@link MethodCall#construct(Constructor)}. * </p> * * @param constructor The constructor to invoke. * @return A method call implementation that invokes the given constructor without providing any arguments. */ public static WithoutSpecifiedTarget invoke(Constructor<?> constructor) { return invoke(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * {@inheritDoc} */ public MethodDescription.InDefinedShape getEnclosingMethod() { Method enclosingMethod = type.getEnclosingMethod(); Constructor<?> enclosingConstructor = type.getEnclosingConstructor(); if (enclosingMethod != null) { return new MethodDescription.ForLoadedMethod(enclosingMethod); } else if (enclosingConstructor != null) { return new MethodDescription.ForLoadedConstructor(enclosingConstructor); } else { return MethodDescription.UNDEFINED; } }
/** * {@inheritDoc} */ public boolean represents(Constructor<?> constructor) { return equals(new ForLoadedConstructor(constructor)); }
/** * {@inheritDoc} */ public MethodDefinition.ImplementationDefinition<S> define(Constructor<?> constructor) { return define(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * {@inheritDoc} */ public InnerTypeDefinition<S> innerTypeOf(Constructor<?> constructor) { return innerTypeOf(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * Invokes the given constructor in order to create an instance. * * @param constructor The constructor to invoke. * @return A method call that invokes the given constructor without providing any arguments. */ public static MethodCall construct(Constructor<?> constructor) { return construct(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * Exactly matches a given constructor as a {@link MethodDescription} in its defined shape. * * @param constructor The constructor to match by its description * @param <T> The type of the matched object. * @return An element matcher that exactly matches the given constructor in its defined shape. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> is(Constructor<?> constructor) { return is(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * Represents a constant that is resolved by invoking a constructor. * * @param constructor The constructor to invoke to create the represented constant value. * @param rawArguments The constructor's constant arguments. * @return A dynamic constant that is resolved by the supplied constuctor. */ public static Dynamic ofInvocation(Constructor<?> constructor, List<?> rawArguments) { return ofInvocation(new MethodDescription.ForLoadedConstructor(constructor), rawArguments); }
/** * Creates a method handle representation of the given constructor. * * @param constructor The constructor ro represent. * @return A method handle representing the given constructor. */ public static MethodHandle of(Constructor<?> constructor) { return of(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * {@inheritDoc} */ public boolean represents(Constructor<?> constructor) { return this.constructor.equals(constructor) || equals(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * {@inheritDoc} */ public MethodDescription.InDefinedShape getDeclaringMethod() { return new MethodDescription.ForLoadedConstructor(constructor); }
/** * Returns a method type description of the given constructor. * * @param constructor The constructor to extract the method type from. * @return The method type of the given constructor. */ public static MethodType of(Constructor<?> constructor) { return of(new MethodDescription.ForLoadedConstructor(constructor)); }
/** * Binds the supplied bootstrap constructor for the resolution of a dynamic constant. * * @param name The name of the bootstrap constant that is provided to the bootstrap method or constructor. * @param constructor The bootstrap constructor to invoke. * @param rawArguments The arguments for the bootstrap constructor represented as primitive wrapper types, * {@link String}, {@link TypeDescription} or {@link JavaConstant} values or their loaded forms. * @return A dynamic constant that represents the bootstrapped constructor's result. */ public static Dynamic bootstrap(String name, Constructor<?> constructor, List<?> rawArguments) { return bootstrap(name, new MethodDescription.ForLoadedConstructor(constructor), rawArguments); }