Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.gh.bmd.jrt.core;
 
 
 
 
 import static com.gh.bmd.jrt.core.RoutineBuilders.getAnnotatedMethod;
 import static com.gh.bmd.jrt.core.RoutineBuilders.getTargetMethodInfo;
 import static com.gh.bmd.jrt.core.RoutineBuilders.invokeRoutine;
 import static com.gh.bmd.jrt.util.Reflection.NO_ARGS;

Class implementing a builder of routines wrapping an object instance.

Created by davide-maestroni on 9/21/14.

 
         implements ObjectRoutineBuilder {
 
             new ProxyConfiguration.Configurable<ObjectRoutineBuilder>() {
 
                 @Nonnull
                 public ObjectRoutineBuilder setConfiguration(
                         @Nonnull final ProxyConfiguration configuration) {
 
                     return DefaultObjectRoutineBuilder.this.setConfiguration(configuration);
                 }
             };
 
             new InvocationConfiguration.Configurable<ObjectRoutineBuilder>() {
 
                 @Nonnull
                 public ObjectRoutineBuilder setConfiguration(
                         @Nonnull final InvocationConfiguration configuration) {
 
                     return DefaultObjectRoutineBuilder.this.setConfiguration(configuration);
                 }
             };

    
Constructor.

Parameters:
target the target object instance.
 
     DefaultObjectRoutineBuilder(@Nonnull final Object target) {
 
         super(target);
     }
 
     @Nonnull
     public <INPUT, OUTPUT> Routine<INPUT, OUTPUT> aliasMethod(@Nonnull final String name) {
 
         final Method method = getAnnotatedMethod(namegetTargetClass());
 
         if (method == null) {
 
             throw new IllegalArgumentException(
                     "no annotated method with alias '" + name + "' has been found");
         }
 
         return method(method);
     }
 
     @Nonnull
     @Override
     @SuppressWarnings("unchecked")
        final InvocationConfiguration config = getInvocationConfiguration();
                                                                         config);
    }
    @Nonnull
    @Override
    @SuppressWarnings("unchecked")
    public ProxyConfiguration.Builder<? extends ObjectRoutineBuilderproxies() {
        final ProxyConfiguration config = getProxyConfiguration();
        return new ProxyConfiguration.Builder<ObjectRoutineBuilder>(config);
    }
    @Nonnull
    @Override
    public ObjectRoutineBuilder setConfiguration(@Nonnull final ProxyConfiguration configuration) {
        super.setConfiguration(configuration);
        return this;
    }
    @Nonnull
    @Override
            @Nonnull final InvocationConfiguration configuration) {
        super.setConfiguration(configuration);
        return this;
    }
    @Nonnull
    public <TYPE> TYPE buildProxy(@Nonnull final Class<TYPE> itf) {
        if (!itf.isInterface()) {
            throw new IllegalArgumentException(
                    "the specified class is not an interface: " + itf.getName());
        }
        final Object proxy = Proxy.newProxyInstance(itf.getClassLoader(), new Class[]{itf},
                                                    new ProxyInvocationHandler());
        return itf.cast(proxy);
    }
    @Nonnull
    public <TYPE> TYPE buildProxy(@Nonnull final ClassToken<TYPE> itf) {
        return itf.cast(buildProxy(itf.getRawClass()));
    }

    
Invocation handler adapting a different interface to the target object instance.
    private class ProxyInvocationHandler implements InvocationHandler {
        private final InvocationConfiguration mInvocationConfiguration;
        private final ProxyConfiguration mProxyConfiguration;

        
Constructor.
        private ProxyInvocationHandler() {
             = getProxyConfiguration();
        }
        public Object invoke(final Object proxyfinal Method methodfinal Object[] argsthrows
                Throwable {
            final WeakReference<?> targetReference = getTargetReference();
            if (targetReference == null) {
                throw new IllegalStateException("the target reference must not be null");
            }
            final Object target = targetReference.get();
            if (target == null) {
                throw new IllegalStateException("the target object has been destroyed");
            }
            final MethodInfo methodInfo = getTargetMethodInfo(methodgetTargetClass());
            final InputMode inputMode = methodInfo.inputMode;
            final OutputMode outputMode = methodInfo.outputMode;
            final Routine<ObjectObjectroutine =
                    buildRoutine(methodmethodInfo.methodinputModeoutputMode);
            return invokeRoutine(routinemethod, (args != null) ? args : inputMode,
                                 outputMode);
        }
        @Nonnull
        private Routine<ObjectObjectbuildRoutine(@Nonnull final Method method,
                @Nonnull final Method targetMethod, @Nullable final InputMode inputMode,
                @Nullable final OutputMode outputMode) {
            String shareGroup = .getShareGroupOr(null);
            final InvocationConfiguration configuration = ;
            final InvocationConfiguration.Builder<InvocationConfigurationbuilder =
                    configuration.builderFrom();
            final Priority priorityAnnotation = method.getAnnotation(Priority.class);
            if (priorityAnnotation != null) {
                builder.withPriority(priorityAnnotation.value());
            }
            final ShareGroup shareGroupAnnotation = method.getAnnotation(ShareGroup.class);
            if (shareGroupAnnotation != null) {
                shareGroup = shareGroupAnnotation.value();
            }
            final Timeout timeoutAnnotation = method.getAnnotation(Timeout.class);
            if (timeoutAnnotation != null) {
                builder.withExecutionTimeout(timeoutAnnotation.value(), timeoutAnnotation.unit());
            }
            final TimeoutAction actionAnnotation = method.getAnnotation(TimeoutAction.class);
            if (actionAnnotation != null) {
                builder.withExecutionTimeoutAction(actionAnnotation.value());
            }
            return getRoutine(builder.set(), shareGrouptargetMethodinputModeoutputMode);
        }
    }
New to GrepCode? Check out our FAQ X