net.bytebuddy.implementation.bind.annotation.AllArguments Java Examples

The following examples show how to use net.bytebuddy.implementation.bind.annotation.AllArguments. 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: MapInAdjacentPropertiesHandler.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@RuntimeType
public static Object execute(@This final ElementFrame thisFrame, @Origin final Method method, @RuntimeType @AllArguments final Object[] args)
{
    final MapInAdjacentProperties ann = ((CachesReflection) thisFrame).getReflectionCache().getAnnotation(method, MapInAdjacentProperties.class);

    Element thisElement = thisFrame.getElement();
    if (!(thisElement instanceof Vertex))
        throw new WindupException("Element is not of supported type, must be Vertex, but was: " + thisElement.getClass().getCanonicalName());
    Vertex vertex = (Vertex) thisElement;

    String methodName = method.getName();
    if (methodName.startsWith("get"))
        return handleGetter(vertex, method, args, ann);

    if (methodName.startsWith("set"))
    {
        handleSetter(vertex, method, args, ann, thisFrame.getGraph());
        return null;
    }

    throw new WindupException("Only get* and set* method names are supported for @" + MapInAdjacentProperties.class.getSimpleName());
}
 
Example #2
Source File: BytebuddyInvocationHandler.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@RuntimeType
public Object byteBuddyInvoke(@This Object proxy, @Origin Method method, @AllArguments @RuntimeType Object[] args)
    throws Throwable {
    String name = method.getName();
    if ("equals".equals(name)) {
        Object another = args[0];
        return proxy == another ||
            (proxy.getClass().isInstance(another) && proxyInvoker.equals(BytebuddyProxy.parseInvoker(another)));
    } else if ("hashCode".equals(name)) {
        return proxyInvoker.hashCode();
    } else if ("toString".equals(name)) {
        return proxyInvoker.toString();
    }

    SofaRequest request = MessageBuilder.buildSofaRequest(method.getDeclaringClass(), method,
        method.getParameterTypes(), args);
    SofaResponse response = proxyInvoker.invoke(request);

    return response.getAppResponse();
}
 
Example #3
Source File: MapInAdjacentVerticesHandler.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@RuntimeType
public static Object execute(@This final ElementFrame thisFrame, @Origin final Method method, @RuntimeType @AllArguments final Object[] args)
{
    final MapInAdjacentVertices ann = ((CachesReflection) thisFrame).getReflectionCache().getAnnotation(method, MapInAdjacentVertices.class);

    Element thisElement = thisFrame.getElement();
    if (!(thisElement instanceof Vertex))
        throw new WindupException("Element is not of supported type, must be Vertex, but was: " + thisElement.getClass().getCanonicalName());
    Vertex vertex = (Vertex) thisElement;

    String methodName = method.getName();
    if (methodName.startsWith("get"))
    {
        return handleGetter(vertex, method, args, ann, thisFrame.getGraph());
    }
    else if (methodName.startsWith("set"))
    {
        handleSetter((VertexFrame)thisFrame, method, args, ann, thisFrame.getGraph());
        return null;
    }

    throw new WindupException("Only get* and set* method names are supported.");
}
 
Example #4
Source File: MapInPropertiesHandler.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@RuntimeType
public static Object execute(@This final ElementFrame thisFrame, @Origin final Method method, @RuntimeType @AllArguments final Object[] args)
{
    final MapInProperties ann = ((CachesReflection) thisFrame).getReflectionCache().getAnnotation(method, MapInProperties.class);

    Element thisElement = thisFrame.getElement();
    if (!(thisElement instanceof Vertex))
        throw new WindupException("Element is not of supported type, must be Vertex, but was: " + thisElement.getClass().getCanonicalName());
    Vertex vertex = (Vertex) thisElement;

    String methodName = method.getName();
    if (methodName.startsWith("get"))
        return handleGetter(vertex, method, args, ann);

    if (methodName.startsWith("set"))
        return handleSetter(vertex, method, args, ann);

    if (methodName.startsWith("put"))
        return handleAdder(vertex, method, args, ann);

    if (methodName.startsWith("putAll"))
        return handleAdder(vertex, method, args, ann);

    throw new WindupException("Only get*, set*, and put* method names are supported for @"
                + MapInProperties.class.getSimpleName() + ", found at: " + method.getName());
}
 
Example #5
Source File: ProxyConfiguration.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Intercepts a method call to a proxy.
 *
 * @param instance The proxied instance.
 * @param method The invoked method.
 * @param arguments The method arguments.
 * @param stubValue The intercepted method's default value.
 * @param interceptor The proxy object's interceptor instance.
 *
 * @return The intercepted method's return value.
 *
 * @throws Throwable If the intercepted method raises an exception.
 */
@RuntimeType
public static Object intercept(
		@This final Object instance,
		@Origin final Method method,
		@AllArguments final Object[] arguments,
		@StubValue final Object stubValue,
		@FieldValue(INTERCEPTOR_FIELD_NAME) Interceptor interceptor
) throws Throwable {
	if ( interceptor == null ) {
		if ( method.getName().equals( "getHibernateLazyInitializer" ) ) {
			return instance;
		}
		else {
			return stubValue;
		}
	}
	else {
		return interceptor.intercept( instance, method, arguments );
	}
}
 
Example #6
Source File: ConstructorInterTemplate.java    From skywalking with Apache License 2.0 6 votes vote down vote up
/**
 * Intercept the target constructor.
 *
 * @param obj          target class instance.
 * @param allArguments all constructor arguments
 */
@RuntimeType
public static void intercept(@This Object obj, @AllArguments Object[] allArguments) {
    try {
        prepare();

        EnhancedInstance targetObject = (EnhancedInstance) obj;

        if (INTERCEPTOR == null) {
            return;
        }
        INTERCEPTOR.onConstruct(targetObject, allArguments);
    } catch (Throwable t) {
        LOGGER.error("ConstructorInter failure.", t);
    }
}
 
Example #7
Source File: GenericImmutableProxyForwarder.java    From reflection-util with Apache License 2.0 6 votes vote down vote up
@RuntimeType
public static Object forward(@Origin Method method,
							 @FieldValue(ImmutableProxy.DELEGATE_FIELD_NAME) Object delegate,
							 @AllArguments Object[] args) throws InvocationTargetException, IllegalAccessException {
	Object value = method.invoke(delegate, args);
	if (ImmutableProxy.isImmutable(value)) {
		return value;
	}
	if (!shouldProxyReturnValue(method)) {
		return value;
	}
	if (value instanceof Collection) {
		return createImmutableCollection(value, method);
	} else if (value instanceof Map) {
		return createImmutableMap(value, method);
	} else {
		return ImmutableProxy.create(value);
	}
}
 
Example #8
Source File: AutoInvoker.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@RuntimeType
public Object invoke(@Origin Method method, @AllArguments @RuntimeType Object[] args) throws Throwable {
    Class<?> returnType = method.getReturnType();

    if (isSyncInvoke(returnType)) {
        return doInvoke(method.getName(), args, returnType, true);
    }

    InvokeFuture<Object> inf = (InvokeFuture<Object>) doInvoke(method.getName(), args, returnType, false);

    if (returnType.isAssignableFrom(inf.getClass())) {
        return inf;
    }

    final CompletableFuture<Object> cf = newFuture((Class<CompletableFuture>) returnType);
    inf.whenComplete((result, throwable) -> {
        if (throwable == null) {
            cf.complete(result);
        } else {
            cf.completeExceptionally(throwable);
        }
    });

    return cf;
}
 
Example #9
Source File: CucumberInterceptor.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@RuntimeType
public static Object intercept(@FieldValue("executor") DecoratorExecutor executor, @FieldValue("bean") Object bean, @Origin Method method, @AllArguments Object[] args) throws Exception {
	
       final FeatureResolver resolver = FeatureResolver.newFeatureResolver(bean.getClass()).withTestMethod(method)
               .withDefaultCleanupPhase(CleanupPhase.NONE).build();

       Object result = null;
       final TestInvocationImpl invocation = new TestInvocationImpl(bean, method, resolver);
       executor.processBefore(invocation);
       try {
           result = method.invoke(bean, args);
       } catch (final Exception e) {
       	Exception cause = (Exception)e.getCause();
           invocation.setTestException(cause);
           executor.processAfter(invocation);
           throw cause;
       }
       executor.processAfter(invocation);

       return result;
   }
 
Example #10
Source File: ConcordionInterceptor.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@RuntimeType
public static Object intercept(@FieldValue("executor") DecoratorExecutor executor, @FieldValue("bean") Object bean, @Origin Method method, @AllArguments Object[] args) throws Exception {
	
       final FeatureResolver resolver = FeatureResolver.newFeatureResolver(bean.getClass()).withTestMethod(method)
               .withDefaultCleanupPhase(CleanupPhase.NONE).build();

       Object result = null;
       final TestInvocationImpl invocation = new TestInvocationImpl(bean, method, resolver);
       executor.processBefore(invocation);
       try {
           result = method.invoke(bean, args);
       } catch (final Exception e) {
       	Exception cause = (Exception)e.getCause();
           invocation.setTestException(cause);
           executor.processAfter(invocation);
           throw cause;
       }
       executor.processAfter(invocation);

       return result;
   }
 
Example #11
Source File: ConstructorInter.java    From skywalking with Apache License 2.0 5 votes vote down vote up
/**
 * Intercept the target constructor.
 *
 * @param obj          target class instance.
 * @param allArguments all constructor arguments
 */
@RuntimeType
public void intercept(@This Object obj, @AllArguments Object[] allArguments) {
    try {
        EnhancedInstance targetObject = (EnhancedInstance) obj;

        interceptor.onConstruct(targetObject, allArguments);
    } catch (Throwable t) {
        logger.error("ConstructorInter failure.", t);
    }

}
 
Example #12
Source File: TemplateModelProxyHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Processes a method invocation on a Byte buddy proxy instance and returns
 * the result. This method will be invoked on an invocation handler when a
 * method is invoked on a proxy instance that it is associated with.
 *
 * @param target
 *            the proxy instance
 * @param method
 *            the {@code Method} instance corresponding to the proxied
 *            method invoked on the proxy instance.
 *
 * @param args
 *            an array of objects containing the values of the arguments
 *            passed in the method invocation on the proxy instance.
 * @return the value to return from the method invocation on the proxy
 *         instance.
 */
@RuntimeType
@SuppressWarnings("static-method")
public Object intercept(@This Object target, @Origin Method method,
        @AllArguments Object[] args) {
    String propertyName = ReflectTools.getPropertyName(method);

    BeanModelType<?> modelType = getModelTypeForProxy(target);

    if (!modelType.hasProperty(propertyName)) {
        throw new InvalidTemplateModelException(
                modelType.getProxyType().getName()
                        + " has no property named " + propertyName
                        + " (or it has been excluded)");
    }

    ModelType propertyType = modelType.getPropertyType(propertyName);
    ElementPropertyMap modelMap = ElementPropertyMap
            .getModel(getStateNodeForProxy(target));

    if (ReflectTools.isGetter(method)) {
        return handleGetter(modelMap, propertyName, propertyType);
    } else if (ReflectTools.isSetter(method)) {
        Object value = args[0];
        handleSetter(modelMap, propertyName, propertyType, value);
        return null;
    }

    throw new InvalidTemplateModelException(
            getUnsupportedMethodMessage(method, args));
}
 
Example #13
Source File: RMapInterceptor.java    From redisson with Apache License 2.0 5 votes vote down vote up
@RuntimeType
public static Object intercept(
        @Origin Method method,
        @AllArguments Object[] args,
        @FieldValue("liveObjectLiveMap") RMap<?, ?> map
) throws Exception {
    return method.invoke(map, args);
}
 
Example #14
Source File: RObjectInterceptor.java    From redisson with Apache License 2.0 5 votes vote down vote up
@RuntimeType
public static Object intercept(
        @Origin Method method,
        @AllArguments Object[] args,
        @FieldValue("liveObjectLiveMap") RMap<?, ?> map
) throws Exception {
    throw new UnsupportedOperationException("Please use RLiveObjectService instance for this type of functions");
}
 
Example #15
Source File: FieldAccessorInterceptor.java    From redisson with Apache License 2.0 5 votes vote down vote up
@RuntimeType
public static Object intercept(
        @Origin Method method,
        @AllArguments Object[] args,
        @This Object me,
        @FieldValue("liveObjectLiveMap") RMap<?, ?> map
) throws Exception {
    if (args.length >= 1 && String.class.isAssignableFrom(args[0].getClass())) {
        String name = ((String) args[0]).substring(0, 1).toUpperCase() + ((String) args[0]).substring(1);
        if ("get".equals(method.getName()) && args.length == 1) {
            try {
                return me.getClass().getMethod("get" + name).invoke(me);
            } catch (NoSuchMethodException noSuchMethodException) {
                throw new NoSuchFieldException((String) args[0]);
            }
        } else if ("set".equals(method.getName()) && args.length == 2) {
            Method m = ClassUtils.searchForMethod(me.getClass(), "set" + name, new Class[]{args[1].getClass()});
            if (m != null) {
                return m.invoke(me, args[1]);
            } else {
                throw new NoSuchFieldException((String) args[0]);
            }
        }
    }
    throw new NoSuchMethodException(method.getName() + " has wrong signature");

}
 
Example #16
Source File: RExpirableInterceptor.java    From redisson with Apache License 2.0 5 votes vote down vote up
@RuntimeType
public static Object intercept(
        @Origin Method method,
        @AllArguments Object[] args,
        @FieldValue("liveObjectLiveMap") RMap<?, ?> map
) throws Exception {
    Class<?>[] cls = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
        cls[i] = args[i].getClass();
    }
    return ClassUtils.searchForMethod(RExpirable.class, method.getName(), cls).invoke(map, args);
}
 
Example #17
Source File: ByteBuddyMethodInterceptor.java    From JGiven with Apache License 2.0 5 votes vote down vote up
@RuntimeType
@BindingPriority( BindingPriority.DEFAULT * 3 )
public Object interceptSuper( @SuperCall final Callable<?> zuper, @This final Object receiver, @Origin Method method,
        @AllArguments final Object[] parameters,
        @FieldProxy( INTERCEPTOR_FIELD_NAME ) StepInterceptorGetterSetter stepInterceptorGetter )
        throws Throwable{

    StepInterceptor interceptor = (StepInterceptor) stepInterceptorGetter.getValue();

    if( interceptor == null ) {
        return zuper.call();
    }

    return interceptor.intercept( receiver, method, parameters, zuper::call );
}
 
Example #18
Source File: ByteBuddyMethodInterceptor.java    From JGiven with Apache License 2.0 5 votes vote down vote up
@RuntimeType
@BindingPriority( BindingPriority.DEFAULT * 2 )
public Object interceptDefault( @DefaultCall final Callable<?> zuper, @This final Object receiver, @Origin Method method,
        @AllArguments final Object[] parameters,
        @FieldProxy( INTERCEPTOR_FIELD_NAME ) StepInterceptorGetterSetter stepInterceptorGetter )
        throws Throwable{

    StepInterceptor interceptor = (StepInterceptor) stepInterceptorGetter.getValue();

    if( interceptor == null ) {
        return zuper.call();
    }

    return interceptor.intercept( receiver, method, parameters, zuper::call );
}
 
Example #19
Source File: ByteBuddyMethodInterceptor.java    From JGiven with Apache License 2.0 5 votes vote down vote up
@RuntimeType
public Object intercept( @This final Object receiver, @Origin final Method method,
        @AllArguments final Object[] parameters,
        @FieldProxy( INTERCEPTOR_FIELD_NAME ) StepInterceptorGetterSetter stepInterceptorGetter )
        throws Throwable{
    // this intercepted method does not have a non-abstract super method
    StepInterceptor interceptor = (StepInterceptor) stepInterceptorGetter.getValue();

    if( interceptor == null ) {
        return null;
    }

    return interceptor.intercept( receiver, method, parameters, () -> null );
}
 
Example #20
Source File: ClassInjectorUsingReflectionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@RuntimeType
public static Object intercept(@Super(proxyType = TargetType.class) Object zuper,
                               @AllArguments Object[] args,
                               @Origin Method method) throws Throwable {
    args[0] = BAR;
    return method.invoke(zuper, args);
}
 
Example #21
Source File: SetInPropertiesHandler.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@RuntimeType
public static Object execute(@This final ElementFrame thisFrame, @Origin final Method method, @RuntimeType @AllArguments final Object[] args)
{
    final SetInProperties ann = ((CachesReflection) thisFrame).getReflectionCache().getAnnotation(method, SetInProperties.class);

    Element thisElement = thisFrame.getElement();
    if (!(thisElement instanceof Vertex))
        throw new WindupException("Element is not of supported type, must be Vertex, but was: " + thisElement.getClass().getCanonicalName());
    Vertex vertex = (Vertex)thisElement;

    String methodName = method.getName();
    if (methodName.startsWith("get"))
        return handleGetter(vertex, method, args, ann);

    else if (methodName.startsWith("set"))
        handleSetter(vertex, method, args, ann);

    else if (methodName.startsWith("addAll"))
        handleAddAll(vertex, method, args, ann);

    else if (methodName.startsWith("add"))
        handleAdder(vertex, method, args, ann);

    else
        throw new WindupException("Only get*, set*, add*, and addAll* method names are supported for @"
                + SetInProperties.class.getSimpleName() + ", found at: " + method.getName());

    return thisFrame;
}
 
Example #22
Source File: ByteBuddyProxyTest.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@RuntimeType
public Object intercept(@Origin Method method, @AllArguments Object[] params) {
    String methodName = method.getName();
    if (methodName.equals("findById")) {
        return Mono.just(Account.newBuilder().setId(1).setNick("demo").build());
    } else if (methodName.equals("findByStatus") || methodName.equals("findByIdStream")) {
        return Flux.just(Account.newBuilder().setId(1).setNick("demo").build());
    }
    return null;
}
 
Example #23
Source File: RobustElementWrapper.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
/**
 * This is the method that intercepts component container methods in "enhanced" model objects.
 * 
 * @param obj "enhanced" object upon which the method was invoked
 * @param method {@link Method} object for the invoked method
 * @param args method invocation arguments
 * @return {@code anything} (the result of invoking the intercepted method)
 * @throws Exception {@code anything} (exception thrown by the intercepted method)
 */
@RuntimeType
@BindingPriority(Integer.MAX_VALUE)
public Object intercept(@This final Object obj, @Origin final Method method,
                @AllArguments final Object[] args) throws Exception { //NOSONAR
    try {
        return invoke(method, args);
    } catch (StaleElementReferenceException sere) {
        refreshReference(sere);
        return invoke(method, args);
    }
}
 
Example #24
Source File: MethodInvokeBenchmark.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
@RuntimeType
public Object invoke(@SuperCall Callable<Object> superMethod, @AllArguments @RuntimeType Object[] args) throws Throwable {
    return superMethod.call();
}
 
Example #25
Source File: OrchestratorInterceptor.java    From yanwte2 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
@RuntimeType
public Object intercept(@AllArguments Object[] allArguments) {
    Object arg = allArguments[0];
    return function.apply(arg);
}
 
Example #26
Source File: MethodDelegationAllArgumentsTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public static Object intercept(@AllArguments(includeSelf = true) Object[] args) {
    assertThat(args.length, is(3));
    assertThat(args[1], is((Object) QUX));
    assertThat(args[2], is((Object) BAZ));
    return args[0];
}
 
Example #27
Source File: MethodDelegationAllArgumentsTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public static String qux(@AllArguments(AllArguments.Assignment.SLACK) String[] args) {
    assertThat(args.length, is(1));
    return QUX + args[0];
}
 
Example #28
Source File: MethodDelegationAllArgumentsTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public static String qux(@AllArguments String[] args) {
    assertThat(args.length, is(2));
    return QUX + args[0] + args[1];
}
 
Example #29
Source File: MethodDelegationAllArgumentsTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public static String qux(@AllArguments Object args) {
    return QUX + ((Object[]) args)[0] + ((Object[]) args)[1];
}
 
Example #30
Source File: MethodDelegationAllArgumentsTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public static String qux(@AllArguments int[] args) {
    return QUX + args[0] + args[1];
}