/** * Creates a new class (or interface) from the given class file. * If there already exists a class with the same name, the new class * overwrites that previous class. * * <p>This method is used for creating a <code>CtClass</code> object * directly from a class file. The qualified class name is obtained * from the class file; you do not have to explicitly give the name. * * @param classfile class file. * @throws RuntimeException if there is a frozen class with the * the same name. * @since 3.20 */ public CtClass makeClass(ClassFile classfile) throws RuntimeException { return makeClass(classfile, true); }
/** * Creates a new public class. * If there already exists a class with the same name, the new class * overwrites that previous class. * * <p>If no constructor is explicitly added to the created new * class, Javassist generates constructors and adds it when * the class file is generated. It generates a new constructor * for each constructor of the super class. The new constructor * takes the same set of parameters and invokes the * corresponding constructor of the super class. All the received * parameters are passed to it. * * @param classname a fully-qualified class name. * @throws RuntimeException if the existing class is frozen. */ public CtClass makeClass(String classname) throws RuntimeException { return makeClass(classname, null); }
/** * Creates a new class (or interface) from the given class file. * If there already exists a class with the same name, the new class * overwrites that previous class. * * <p>This method is used for creating a <code>CtClass</code> object * directly from a class file. The qualified class name is obtained * from the class file; you do not have to explicitly give the name. * * @param classfile class file. * @throws RuntimeException if there is a frozen class with the * the same name. * @see #makeClassIfNew(InputStream) * @see javassist.ByteArrayClassPath */ public CtClass makeClass(InputStream classfile) throws IOException, RuntimeException { return makeClass(classfile, true); }
/** * Creates a new public class. * If there already exists a class with the same name, the new class * overwrites that previous class. * * <p>If no constructor is explicitly added to the created new * class, Javassist generates constructors and adds it when * the class file is generated. It generates a new constructor * for each constructor of the super class. The new constructor * takes the same set of parameters and invokes the * corresponding constructor of the super class. All the received * parameters are passed to it. * * @param classname a fully-qualified class name. * @throws RuntimeException if the existing class is frozen. */ public CtClass makeClass(String classname) throws RuntimeException { return makeClass(classname, null); }
/** * Creates a new class (or interface) from the given class file. * If there already exists a class with the same name, the new class * overwrites that previous class. * * <p>This method is used for creating a <code>CtClass</code> object * directly from a class file. The qualified class name is obtained * from the class file; you do not have to explicitly give the name. * * @param classfile class file. * @throws RuntimeException if there is a frozen class with the * the same name. * @since 3.20 */ public CtClass makeClass(ClassFile classfile) throws RuntimeException { return makeClass(classfile, true); }
/** * Creates a new class (or interface) from the given class file. * If there already exists a class with the same name, the new class * overwrites that previous class. * * <p>This method is used for creating a <code>CtClass</code> object * directly from a class file. The qualified class name is obtained * from the class file; you do not have to explicitly give the name. * * @param classfile class file. * @throws RuntimeException if there is a frozen class with the * the same name. * @see #makeClassIfNew(InputStream) * @see javassist.ByteArrayClassPath */ public CtClass makeClass(InputStream classfile) throws IOException, RuntimeException { return makeClass(classfile, true); }
protected CtClass loadCtClassFromClass(Class<?> aClass) { String resourceName = aClass.getName().replace( '.', '/' ) + ".class"; InputStream resourceAsStream = aClass.getClassLoader().getResourceAsStream( resourceName ); if ( resourceAsStream == null ) { throw new UncheckedIOException( new FileNotFoundException ( "Not found: " + resourceName ) ); } try { return classPool.makeClass( resourceAsStream ); } catch (IOException e) { throw new EnhancementException( "Could not prepare Javassist ClassPool", e ); } finally { try { resourceAsStream.close(); } catch (IOException ioe) { log.debugf( "An error occurs closing InputStream for class [%s]", aClass.getName() ); } } }
public static void enableStreamingSupport() throws Exception { CtClass ctClass = pool.makeClass("com.jsoniter.IterImpl"); ctClass.setSuperclass(pool.get(IterImplForStreaming.class.getName())); ctClass.toClass(); } }
private static TemplateRenderer tryToCompile(String source, Map<String, String> expressions, Map<String, String> constants, Class<?> modelType) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException { ClassPool cp = ClassPool.getDefault(); CtClass sup = cp.get(Object.class.getCanonicalName()); CtClass cls = cp.makeClass("RapidoidTemplate" + ID_GEN.incrementAndGet(), sup); cls.addInterface(cp.get(TemplateRenderer.class.getCanonicalName())); cls.addConstructor(CtNewConstructor.defaultConstructor(cls)); addExpressions(expressions, cls); addConstants(constants, cls); CtClass[] params = {cp.get(RenderCtx.class.getCanonicalName())}; CtClass clsVoid = cp.get(void.class.getCanonicalName()); cls.addMethod(CtNewMethod.make(Modifier.PUBLIC, clsVoid, "render", params, new CtClass[0], source, cls)); return (TemplateRenderer) cls.toClass().newInstance(); }
@Override public ResourceConfig register(final Object component) { final Object object = requireNonNull(component); Class<?> clazz = object.getClass(); // If a class gets passed through as an object, cast to Class and register directly if (component instanceof Class<?>) { return super.register((Class<?>) component); } else if (Providers.isProvider(clazz) || Binder.class.isAssignableFrom(clazz)) { // If jersey supports this component's class (including hk2 Binders), register directly return super.register(object); } else { // Else register a binder that binds the instance to its class type try { // Need to create a new subclass dynamically here because hk2/jersey // doesn't add new bindings for the same class ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.makeClass(SpecificBinder.class.getName() + UUID.randomUUID()); cc.setSuperclass(pool.get(SpecificBinder.class.getName())); Object binderProxy = cc.toClass().getConstructor(Object.class, Class.class).newInstance(object, clazz); super.register(binderProxy); return super.register(clazz); } catch (Exception e) { throw new RuntimeException(e); } } }
public static Decoder gen(String cacheKey, String source) throws Exception { Decoder decoder; CtClass ctClass = pool.makeClass(cacheKey); ctClass.setInterfaces(new CtClass[]{pool.get(Decoder.class.getName())}); CtMethod staticMethod = CtNewMethod.make(source, ctClass); ctClass.addMethod(staticMethod); CtMethod interfaceMethod = CtNewMethod.make("" + "public Object decode(com.jsoniter.JsonIterator iter) {" + "return decode_(iter);" + "}", ctClass); ctClass.addMethod(interfaceMethod); decoder = (Decoder) ctClass.toClass().newInstance(); return decoder; }
@SneakyThrows public Proxy(Class<I> superClass, String... classPathString) { if (superClass == null) { throw new NullPointerException("superClass can not be null"); } this.superClass = superClass; ClassPool classPool = new ClassPool(true); ClassPath classPath = new ClassClassPath(this.getClass()); classPool.insertClassPath(classPath); if (classPathString != null) { for (String path : classPathString) { classPool.insertClassPath(path); } } className = superClass.getSimpleName() + "FastBeanCopier" + counter.getAndAdd(1); classFullName = superClass.getPackage() + "." + className; ctClass = classPool.makeClass(classFullName); if (superClass != Object.class) { if (superClass.isInterface()) { ctClass.setInterfaces(new CtClass[]{classPool.get(superClass.getName())}); } else { ctClass.setSuperclass(classPool.get(superClass.getName())); } } addConstructor("public " + className + "(){}"); }
} catch (ClassNotFoundException e) { ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5();
} catch (ClassNotFoundException e) { ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5();
/** * * @param iface The interface the new class should implement * @param oldlassName The class to be extended * @param newClassName the name of the new class to be created */ public ClassCustomizer(Class<?> iface, String oldlassName, String newClassName) { try { ClassPool pool = ClassPool.getDefault(); ctClass = pool.makeClass(newClassName); classFile = ctClass.getClassFile(); classFile.setSuperclass(oldlassName); classFile.setName(newClassName); classFile.setInterfaces(new String[] { iface.getName() }); } catch (Exception e) { throw new RuntimeException(e); } }
? ClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) { mCtc.setSuperclass(ctcs);
? ClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) { mCtc.setSuperclass(ctcs);
+ " must be public, non-native, and non-abstract."); CtClass proxy = classPool.makeClass(orgclass.getName(), orgclass.getSuperclass());