javassist.util.proxy.MethodFilter Java Examples

The following examples show how to use javassist.util.proxy.MethodFilter. 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: Partial.java    From FreeBuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a partial instance of abstract type {@code cls}, passing {@code args} into its
 * constructor.
 *
 * <p>The returned object will throw an {@link UnsupportedOperationException} from any
 * unimplemented methods.
 */
public static <T> T of(Class<T> cls, Object... args) {
  checkIsValidPartial(cls);
  try {
    Constructor<?> constructor = cls.getDeclaredConstructors()[0];
    ProxyFactory factory = new ProxyFactory();
    factory.setSuperclass(cls);
    factory.setFilter(new MethodFilter() {
      @Override public boolean isHandled(Method m) {
        return Modifier.isAbstract(m.getModifiers());
      }
    });
    @SuppressWarnings("unchecked")
    T partial = (T) factory.create(
        constructor.getParameterTypes(), args, new ThrowingMethodHandler());
    return partial;
  } catch (Exception e) {
    throw new RuntimeException("Failed to instantiate " + cls, e);
  }
}
 
Example #2
Source File: TrivialClassCreationBenchmark.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a benchmark for a trivial class creation using javassist proxies.
 *
 * @return The created instance, in order to avoid JIT removal.
 */
@Benchmark
public Class<?> benchmarkJavassist() {
    ProxyFactory proxyFactory = new ProxyFactory() {
        protected ClassLoader getClassLoader() {
            return newClassLoader();
        }
    };
    proxyFactory.setUseCache(false);
    proxyFactory.setUseWriteReplace(false);
    proxyFactory.setSuperclass(baseClass);
    proxyFactory.setFilter(new MethodFilter() {
        public boolean isHandled(Method method) {
            return false;
        }
    });
    return proxyFactory.createClass();
}
 
Example #3
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using javassist proxies.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws java.lang.Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkJavassist() throws Exception {
    ProxyFactory proxyFactory = new ProxyFactory() {
        protected ClassLoader getClassLoader() {
            return newClassLoader();
        }
    };
    proxyFactory.setUseCache(false);
    proxyFactory.setUseWriteReplace(false);
    proxyFactory.setSuperclass(baseClass);
    proxyFactory.setFilter(new MethodFilter() {
        public boolean isHandled(Method method) {
            return method.getDeclaringClass() == baseClass;
        }
    });
    @SuppressWarnings("unchecked")
    Object instance = proxyFactory.createClass().getDeclaredConstructor().newInstance();
    ((javassist.util.proxy.Proxy) instance).setHandler(new MethodHandler() {
        public Object invoke(Object self,
                             Method thisMethod,
                             Method proceed,
                             Object[] args) throws Throwable {
            return proceed.invoke(self, args);
        }
    });
    return (ExampleClass) instance;
}
 
Example #4
Source File: ReflectionUtils.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
public static MethodFilter getPackageFilter(final String packageName) {
    return new MethodFilter() {
        public boolean isHandled(final Method method) {
            return method.getDeclaringClass().getName().contains(packageName) &&
                    method.getAnnotation(Publish.class) != null;
        }
    };
}
 
Example #5
Source File: ClassByImplementationBenchmark.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * Performs a benchmark of an interface implementation using javassist proxies.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws java.lang.Exception If the reflective invocation causes an exception.
 */
@Benchmark
public ExampleInterface benchmarkJavassist() throws Exception {
    ProxyFactory proxyFactory = new ProxyFactory() {
        protected ClassLoader getClassLoader() {
            return newClassLoader();
        }
    };
    proxyFactory.setUseCache(false);
    proxyFactory.setUseWriteReplace(false);
    proxyFactory.setSuperclass(Object.class);
    proxyFactory.setInterfaces(new Class<?>[]{baseClass});
    proxyFactory.setFilter(new MethodFilter() {
        public boolean isHandled(Method method) {
            return true;
        }
    });
    @SuppressWarnings("unchecked")
    Object instance = proxyFactory.createClass().getDeclaredConstructor().newInstance();
    ((javassist.util.proxy.Proxy) instance).setHandler(new MethodHandler() {
        public Object invoke(Object self,
                             Method thisMethod,
                             Method proceed,
                             Object[] args) throws Throwable {
            Class<?> returnType = thisMethod.getReturnType();
            if (returnType.isPrimitive()) {
                if (returnType == boolean.class) {
                    return defaultBooleanValue;
                } else if (returnType == byte.class) {
                    return defaultByteValue;
                } else if (returnType == short.class) {
                    return defaultShortValue;
                } else if (returnType == char.class) {
                    return defaultCharValue;
                } else if (returnType == int.class) {
                    return defaultIntValue;
                } else if (returnType == long.class) {
                    return defaultLongValue;
                } else if (returnType == float.class) {
                    return defaultFloatValue;
                } else {
                    return defaultDoubleValue;
                }
            } else {
                return defaultReferenceValue;
            }
        }
    });
    return (ExampleInterface) instance;
}