本文整理汇总了Java中javassist.util.proxy.ProxyFactory类的典型用法代码示例。如果您正苦于以下问题:Java ProxyFactory类的具体用法?Java ProxyFactory怎么用?Java ProxyFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ProxyFactory类属于javassist.util.proxy包,在下文中一共展示了ProxyFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createStyledTextRenderer
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
private static /* org.eclipse.swt.custom.StyledTextRenderer */ Object createStyledTextRenderer(
StyledText styledText, ILineSpacingProvider lineSpacingProvider) throws Exception {
// get the org.eclipse.swt.custom.StyledTextRenderer instance of
// StyledText
/* org.eclipse.swt.custom.StyledTextRenderer */ Object originalRenderer = getRendererField(styledText)
.get(styledText);
// Create a Javassist proxy
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(originalRenderer.getClass());
StyledTextRenderer renderer = new StyledTextRenderer(styledText, originalRenderer.getClass());
renderer.setLineSpacingProvider(lineSpacingProvider);
factory.setHandler(renderer);
return factory.create(new Class[] { Device.class, StyledText.class },
new Object[] { styledText.getDisplay(), styledText });
}
示例2: getProxyFactory
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
public static Class getProxyFactory(
Class persistentClass,
Class[] interfaces) throws HibernateException {
// note: interfaces is assumed to already contain HibernateProxy.class
try {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
factory.setInterfaces( interfaces );
factory.setFilter( FINALIZE_FILTER );
return factory.createClass();
}
catch ( Throwable t ) {
LOG.error(LOG.javassistEnhancementFailed(persistentClass.getName()), t);
throw new HibernateException(LOG.javassistEnhancementFailed(persistentClass.getName()), t);
}
}
示例3: createProxyClass
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
static Class<?> createProxyClass(Class<?> persistentClass, Class<?>[] interfaces) throws HibernateException {
// note: interfaces is assumed to already contain HibernateProxy.class
try {
Set<Class<?>> allInterfaces = new HashSet<>();
if(interfaces != null) {
allInterfaces.addAll(Arrays.asList(interfaces));
}
allInterfaces.add(GroovyObject.class);
allInterfaces.add(EntityProxy.class);
ProxyFactory factory = createJavassistProxyFactory(persistentClass, allInterfaces.toArray(new Class<?>[allInterfaces.size()]));
Class<?> proxyClass = factory.createClass();
HibernateUtils.enhanceProxyClass(proxyClass);
return proxyClass;
}
catch (Throwable t) {
LogFactory.getLog(BasicLazyInitializer.class).error(
"Javassist Enhancement failed: " + persistentClass.getName(), t);
throw new HibernateException("Javassist Enhancement failed: " + persistentClass.getName(), t);
}
}
示例4: getProxyFactory
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
private static Class<?> getProxyFactory(Class<?> persistentClass, Class<?>[] interfaces) throws HibernateException {
// note: interfaces is assumed to already contain HibernateProxy.class
try {
Set<Class<?>> allInterfaces = new HashSet<Class<?>>();
if(interfaces != null) {
allInterfaces.addAll(Arrays.asList(interfaces));
}
allInterfaces.add(GroovyObject.class);
allInterfaces.add(EntityProxy.class);
ProxyFactory factory = createProxyFactory(persistentClass, allInterfaces.toArray(new Class<?>[allInterfaces.size()]));
Class<?> proxyClass = factory.createClass();
HibernateUtils.enhanceProxyClass(proxyClass);
return proxyClass;
}
catch (Throwable t) {
LogFactory.getLog(BasicLazyInitializer.class).error(
"Javassist Enhancement failed: " + persistentClass.getName(), t);
throw new HibernateException("Javassist Enhancement failed: " + persistentClass.getName(), t);
}
}
示例5: getProxyFactory
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
public static Class getProxyFactory(
Class persistentClass,
Class[] interfaces) throws HibernateException {
// note: interfaces is assumed to already contain HibernateProxy.class
try {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
factory.setInterfaces( interfaces );
factory.setFilter( FINALIZE_FILTER );
return factory.createClass();
}
catch ( Throwable t ) {
LogFactory.getLog( BasicLazyInitializer.class ).error(
"Javassist Enhancement failed: "
+ persistentClass.getName(), t
);
throw new HibernateException(
"Javassist Enhancement failed: "
+ persistentClass.getName(), t
);
}
}
示例6: createTestClass
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
/**
* We proxy the test class in order to be able to clear state after each
* test method invocation. It would be much better to be able to register a
* testng listener programmtically but I cannot find a way to do so.
*/
private Class<?> createTestClass(Class<?> actualTestClass) throws Exception {
final Class<?> testClassLoadedByMockedClassLoader = Class.forName(actualTestClass.getName(), false, mockLoader);
if (extendsPowerMockTestCase(actualTestClass)) {
return testClassLoadedByMockedClassLoader;
} else {
Class<?> proxyFactoryClass = Class.forName(ProxyFactory.class.getName(), false, mockLoader);
final Class<?> testNGMethodFilterByMockedClassLoader = Class.forName(TestNGMethodFilter.class.getName(), false, mockLoader);
Object f = proxyFactoryClass.newInstance();
Object filter = testNGMethodFilterByMockedClassLoader.newInstance();
Whitebox.invokeMethod(f, "setFilter", filter);
Whitebox.invokeMethod(f, "setSuperclass", testClassLoadedByMockedClassLoader);
Class<?> c = Whitebox.invokeMethod(f, "createClass");
return c;
}
}
示例7: of
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
/**
* 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);
}
}
示例8: getInstance
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
/**
* Get an instance of a rest proxy instance for the service class.
*
* @param serviceClass
* the service class.
* @return the proxy implementation that invokes the remote service.
* @throws Exception
*/
@SuppressWarnings("unchecked")
public <T> T getInstance(@NonNull final Class<T> serviceClass) throws Exception {
final ProxyFactory factory = new ProxyFactory();
if (serviceClass.isInterface()) {
factory.setInterfaces(new Class[] { serviceClass });
} else {
factory.setSuperclass(serviceClass);
}
factory.setFilter(new MethodFilter() {
@Override
public boolean isHandled(final Method method) {
return Modifier.isPublic(method.getModifiers());
}
});
final Class<?> klass = factory.createClass();
final Object instance = objenesis.getInstantiatorOf(klass).newInstance();
((ProxyObject) instance).setHandler(new RestMethodInvocationHandler(baseUri,
clientProvider, interfaceAnalyzer.analyze(serviceClass), responseToThrowableMapper,
builderFilter));
return (T) instance;
}
示例9: DefaultIdeaLanguageManager
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
/**
* Initializes a new instance of the {@link DefaultIdeaLanguageManager} class.
*/
@Inject
public DefaultIdeaLanguageManager(final ILanguageService languageService,
final ILanguageSource languageSource, final ILanguageDiscoveryService discoveryService,
final IIntelliJResourceService resourceService, final MetaborgSourceAnnotator metaborgSourceAnnotator,
final IFileElementTypeFactory fileElementTypeFactory, final IParserDefinitionFactory parserDefinitionFactory,
final Provider<SpoofaxSyntaxHighlighterFactory> syntaxHighlighterFactoryProvider,
final BuilderMenuBuilder builderMenuBuilder, final ActionUtils actionUtils) {
super(languageService, languageSource, discoveryService);
this.metaborgSourceAnnotator = metaborgSourceAnnotator;
this.fileElementTypeFactory = fileElementTypeFactory;
this.parserDefinitionFactory = parserDefinitionFactory;
this.syntaxHighlighterFactoryProvider = syntaxHighlighterFactoryProvider;
this.builderMenuBuilder = builderMenuBuilder;
this.actionUtils = actionUtils;
this.resourceService = resourceService;
this.proxyFactory = new ProxyFactory();
this.proxyFactory.setUseCache(false);
}
示例10: getNonProxyImplementationClassIfNecessary
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
public static Class<?> getNonProxyImplementationClassIfNecessary(Class<?> candidate) {
Class<?> response = candidate;
//TODO Although unusual, there are other proxy types in Hibernate than just javassist proxies (Broadleaf defaults to javassist).
//At some point, should try to account for those here in a performant way.
if (ProxyFactory.isProxyClass(candidate)) {
//TODO This is not ideal. While this works consistently, I don't think it's guaranteed that the proxy classname
//will always have this format in the future. We'll at least throw an exception if our formatting detection fails
//so that we're aware of it.
if (!candidate.getName().contains(JAVASSIST_PROXY_KEY_PHRASE)) {
throw new ProxyDetectionException(String.format("Cannot determine the original implementation class for " +
"the javassist proxy. Expected to find the keyphrase (%s) in the proxy classname (%s).",
JAVASSIST_PROXY_KEY_PHRASE, candidate.getName()));
}
String implName = candidate.getName().substring(0, candidate.getName().lastIndexOf(JAVASSIST_PROXY_KEY_PHRASE));
try {
response = Class.forName(implName);
} catch (ClassNotFoundException e) {
throw ExceptionHelper.refineException(e);
}
}
return response;
}
示例11: build
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
/**
* Creates a proxy object out of the specified root object and the specified interfaces
* and implementations.
*
* @return a proxy object that is still of type T but additionally implements all specified
* interfaces.
* @throws ProxyException if the proxy could not be created.
*/
public T build() throws ProxyException {
if (this.root == null) {
throw new IllegalArgumentException("root must not be null!");
}
if (this.interfacesToImplementations.isEmpty()) {
// nothing to proxy
return this.root;
}
try {
ProxyMethodHandler methodHandler = new ProxyMethodHandler(root,
interfacesToImplementations);
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(root.getClass());
proxyFactory.setInterfaces(interfacesToImplementations.keySet().toArray(new Class[]{}));
return (T) proxyFactory.create(new Class[0], new Object[0], methodHandler);
} catch (Exception e) {
throw new ProxyException(e);
}
}
示例12: createTheProxy
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private <T> Class<T> createTheProxy(Class<?> mainClass) {
ProxyFactory f = new ProxyFactory();
f.setSuperclass(mainClass);
f.setInterfaces(new Class[] {NoSqlProxy.class});
f.setFilter(new MethodFilter() {
public boolean isHandled(Method m) {
// ignore finalize()
if(m.getName().equals("finalize"))
return false;
else if(m.getName().equals("equals"))
return false;
else if(m.getName().equals("hashCode"))
return false;
return true;
}
});
Class<T> clazz = f.createClass();
testInstanceCreation(clazz);
return clazz;
}
示例13: nullSafe
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
public static <T> T nullSafe(Class<T> klass, final Object instance){
try {
if (Modifier.isFinal(klass.getModifiers())) return (T)instance;
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(klass);
Class clazz = factory.createClass();
MethodHandler handler = (self, overridden, forwarder, args) -> {
Object returnval = (instance != null) ? overridden.invoke(instance, args) : null;
return nullSafe(overridden.getReturnType(), returnval);
};
Object proxyInstance = clazz.newInstance();
((ProxyObject) proxyInstance).setHandler(handler);
return (T) proxyInstance;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例14: getProxyClass
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
private static PyObjectValueProxyClass getProxyClass(PyObject pyObject) throws Exception {
String proxyClassName = pyObject.getClass() + PROXY_CLASS_SUFFIX;
PyObjectValueProxyClass proxyClass = proxyClasses.get(proxyClassName);
if (proxyClass == null) {
writeLock.lock();
try {
proxyClass = proxyClasses.get(proxyClassName);
if (proxyClass == null) {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(pyObject.getClass());
factory.setInterfaces(new Class[]{PyObjectValue.class});
factory.setFilter(new PyObjectValueMethodFilter());
factory.setUseWriteReplace(false);
proxyClasses.putIfAbsent(proxyClassName, createProxyClass(factory.createClass(), pyObject));
proxyClass = proxyClasses.get(proxyClassName);
}
} finally {
writeLock.unlock();
}
}
return proxyClass;
}
示例15: newJavassistProxy
import javassist.util.proxy.ProxyFactory; //导入依赖的package包/类
static NonTrivialBean newJavassistProxy()
throws InstantiationException, IllegalAccessException {
ProxyFactory f = new ProxyFactory();
f.setSuperclass(NonTrivialBean.class);
Class<?> c = f.createClass();
MethodHandler mi = new MethodHandler() {
// delegate to source methods
public Object invoke(Object self, Method m, Method proceed,
Object[] args) throws Throwable {
return proceed.invoke(self, args);
}
};
NonTrivialBean b = (NonTrivialBean) c.newInstance();
((Proxy) b).setHandler(mi);
return b;
}