/** * Converts the class to a <code>java.lang.Class</code> object. * Do not override this method any more at a subclass because * <code>toClass(CtClass)</code> never calls this method. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @deprecated Replaced by {@link #toClass(CtClass,ClassLoader,ProtectionDomain)}. * A subclass of <code>ClassPool</code> that has been * overriding this method should be modified. It should override * {@link #toClass(CtClass,ClassLoader,ProtectionDomain)}. */ public Class toClass(CtClass ct, ClassLoader loader) throws CannotCompileException { return toClass(ct, loader, null); }
/** * Converts the class to a <code>java.lang.Class</code> object. * Do not override this method any more at a subclass because * {@link #toClass(CtClass)} will never calls this method. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @deprecated Replaced by {@link #toClass(CtClass,Class,ClassLoader,ProtectionDomain)}. * A subclass of <code>ClassPool</code> that has been * overriding this method should be modified. It should override * {@link #toClass(CtClass,Class,ClassLoader,ProtectionDomain)}. */ public Class toClass(CtClass ct, ClassLoader loader) throws CannotCompileException { return toClass(ct, null, loader, null); }
throws CannotCompileException return toClass(ct, null, loader, domain);
/** * Converts this class to a <code>java.lang.Class</code> object. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @deprecated Replaced by {@link #toClass(ClassLoader,ProtectionDomain)} */ public final Class toClass(ClassLoader loader) throws CannotCompileException { return getClassPool().toClass(this, loader); }
/** * Converts this class to a <code>java.lang.Class</code> object. * Once this method is called, further modifications are not * allowed any more. * To load the class, this method uses the context class loader * of the current thread. If the program is running on some application * server, the context class loader might be inappropriate to load the * class. * * <p>This method is provided for convenience. If you need more * complex functionality, you should write your own class loader. * * <p>Note: this method calls <code>toClass()</code> * in <code>ClassPool</code>. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @see #toClass(java.lang.ClassLoader,ProtectionDomain) * @see ClassPool#toClass(CtClass) */ public Class toClass() throws CannotCompileException { return getClassPool().toClass(this); }
/** * Converts this class to a <code>java.lang.Class</code> object. * * <p><b>Warning:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @deprecated Replaced by {@link #toClass(ClassLoader,ProtectionDomain)} */ @Deprecated public final Class<?> toClass(ClassLoader loader) throws CannotCompileException { return getClassPool().toClass(this, null, loader, null); }
/** * Converts the given class to a <code>java.lang.Class</code> object. * Once this method is called, further modifications are not * allowed any more. * To load the class, this method uses the context class loader * of the current thread. It is obtained by calling * <code>getClassLoader()</code>. * * <p>This behavior can be changed by subclassing the pool and changing * the <code>getClassLoader()</code> method. * If the program is running on some application * server, the context class loader might be inappropriate to load the * class. * * <p>This method is provided for convenience. If you need more * complex functionality, you should write your own class loader. * * <p><b>Warining:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @see #toClass(CtClass, java.lang.ClassLoader, ProtectionDomain) * @see #getClassLoader() */ public Class toClass(CtClass clazz) throws CannotCompileException { // Some subclasses of ClassPool may override toClass(CtClass,ClassLoader). // So we should call that method instead of toClass(.., ProtectionDomain). return toClass(clazz, getClassLoader()); }
/** * Converts this class to a <code>java.lang.Class</code> object. * Once this method is called, further modifications are not * allowed any more. * * <p>This method is provided for convenience. If you need more * complex functionality, you should write your own class loader. * * <p>Note: this method calls <code>toClass()</code> * in <code>ClassPool</code>. * * <p><b>Warning:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @param lookup used when loading the class. It has to have * an access right to define a new class. * @see ClassPool#toClass(CtClass,java.lang.invoke.MethodHandles.Lookup) * @since 3.24 */ public Class<?> toClass(java.lang.invoke.MethodHandles.Lookup lookup) throws CannotCompileException { return getClassPool().toClass(this, lookup); }
/** * Converts this class to a <code>java.lang.Class</code> object. * Once this method is called, further modifications are not * allowed any more. * * <p>This method is provided for convenience. You should use * {@code toClass(Lookup)} for better compatibility with the * module system. * * <p>Note: this method calls <code>toClass()</code> * in <code>ClassPool</code>. * * <p><b>Warning:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * * @param neighbor A class belonging to the same package that this * class belongs to. It is used to load the class. * @see ClassPool#toClass(CtClass,Class) * @see #toClass(java.lang.invoke.MethodHandles.Lookup) * @since 3.24 */ public Class<?> toClass(Class<?> neighbor) throws CannotCompileException { return getClassPool().toClass(this, neighbor); }
return toClass(clazz, getClassLoader());
return getClassPool().toClass(this);
/** * Convert a javassist class to a java class * * @param ct * the javassist class * @param loader * the loader * @throws CannotCompileException * for any error */ public Class toClass(CtClass ct, ClassLoader loader, ProtectionDomain domain) throws CannotCompileException { // We need to pass up the classloader stored in this pool, as the // default implementation uses the Thread context cl. // In the case of JSP's in Tomcat, // org.apache.jasper.servlet.JasperLoader will be stored here, while // it's parent // org.jboss.web.tomcat.tc5.WebCtxLoader$ENCLoader is used as the Thread // context cl. The invocation class needs to // be generated in the JasperLoader classloader since in the case of // method invocations, the package name will be // the same as for the class generated from the jsp, i.e. // org.apache.jsp. For classes belonging to org.apache.jsp, // JasperLoader does NOT delegate to its parent if it cannot find them. lockInCache(ct); return super.toClass(ct, getClassLoader0(), domain); } }
private static Class loadClass(final CtClass cc, final Class relatedClass) { Class newClass; try { newClass = cc.getClassPool().toClass(cc, relatedClass.getClassLoader(), relatedClass.getProtectionDomain()); } catch (final Exception ex) { try { newClass = relatedClass.getClassLoader().loadClass(cc.getName()); } catch (final ClassNotFoundException e) { throw new UncheckedException(e); } } final ConcurrentMap<String, Class> map = getRelatedClassMap(relatedClass); map.put(newClass.getName(), newClass); return newClass; }
loader = cp.getClassLoader(); return cp.toClass(this, loader, domain);
/** * Convert a javassist class to a java class * * @param ct * the javassist class * @param loader * the loader * @throws CannotCompileException * for any error */ public Class<?> toClass(CtClass ct, ClassLoader loader, ProtectionDomain domain) throws CannotCompileException { // We need to pass up the classloader stored in this pool, as the // default implementation uses the Thread context cl. // In the case of JSP's in Tomcat, // org.apache.jasper.servlet.JasperLoader will be stored here, while // it's parent // org.jboss.web.tomcat.tc5.WebCtxLoader$ENCLoader is used as the Thread // context cl. The invocation class needs to // be generated in the JasperLoader classloader since in the case of // method invocations, the package name will be // the same as for the class generated from the jsp, i.e. // org.apache.jsp. For classes belonging to org.apache.jsp, // JasperLoader does NOT delegate to its parent if it cannot find them. lockInCache(ct); return super.toClass(ct, getClassLoader0(), domain); } }
loader = cp.getClassLoader(); return cp.toClass(this, null, loader, domain);
public ReflectorFactory newReflectorFactory() { implementationCounter++; try { pool = ClassPool.getDefault(); pool.insertClassPath(new ClassClassPath(this.getClass())); for (String name : servicesMap.keySetName()) { SService sService = servicesMap.getByName(name); build1((Class<? extends PublicInterface>) sService.getInterfaceClass(), sService); build2((Class<? extends PublicInterface>) sService.getInterfaceClass(), sService); } CtClass reflectorFactoryImpl = pool.makeClass("org.bimserver.reflector.ReflectorFactoryImpl" + implementationCounter); reflectorFactoryImpl.addInterface(pool.get(ReflectorFactory.class.getName())); createCreateReflectorMethod1(reflectorFactoryImpl); createCreateReflectorMethod2(reflectorFactoryImpl); Class<?> class1 = pool.toClass(reflectorFactoryImpl, getClass().getClassLoader(), getClass().getProtectionDomain()); return (ReflectorFactory) class1.newInstance(); } catch (Exception e) { LOGGER.error("", e); } return null; }
reflectorImplClass.addMethod(method); pool.toClass(reflectorImplClass, getClass().getClassLoader(), getClass().getProtectionDomain()); } catch (Exception e) { LOGGER.error("", e);
reflectorImplClass.addMethod(method); pool.toClass(reflectorImplClass, getClass().getClassLoader(), getClass().getProtectionDomain()); } catch (Exception e) { LOGGER.error("", e);
private Class<?> rewriteWithDifferentClassLoader() throws Exception { ClassLoader cl = new ClassLoader(){}; ClassPool pool = ClassPool.getDefault(); CtClass chckCl = pool.get(RoundhouseKick.class.getCanonicalName()); // change the class name to ChuckNorrisException ;) chckCl.setName(this.getClass().getCanonicalName()); return pool.toClass(chckCl, cl, Class.class.getProtectionDomain()); }