@SneakyThrows public Class<I> getTargetClass() { if (targetClass == null) { targetClass = ctClass.toClass(ClassUtils.getDefaultClassLoader(), null); } return targetClass; }
public Class weaveAndLoad(String name, ClassLoader cl) { try { CtClass w = javassistLoadClass(name); if( w == null ) return null; return w.toClass(cl, null); } catch( CannotCompileException e ) { throw new RuntimeException(e); } }
public static void enableStreamingSupport() throws Exception { CtClass ctClass = pool.makeClass("com.jsoniter.IterImpl"); ctClass.setSuperclass(pool.get(IterImplForStreaming.class.getName())); ctClass.toClass(); } }
return cls.toClass(ClassHelper.getCallerClassLoader(getClass()), JavassistCompiler.class.getProtectionDomain());
return cls.toClass(ClassHelper.getCallerClassLoader(getClass()), JavassistCompiler.class.getProtectionDomain());
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; }
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); } } }
@Override public Class<?> loadClass() { try { Class<?> loadClass = clazz.toClass(GeccoClassLoader.get(), null); log.debug("load class : " + clazz.getName()); return loadClass; } catch (CannotCompileException e) { e.printStackTrace(); log.error(clazz.getName() + " cannot compile," + e.getMessage()); return null; } finally { // clazz.detach(); } }
@Override public <T> ObjectConstructor<T> createConstructor(Class<T> type) throws NoSuchMethodException { String constructorClassName = type.getName() + "_ReflectConstructor"; try { return (ObjectConstructor<T>) type.getClassLoader().loadClass(constructorClassName).getConstructor().newInstance(); } catch (ClassNotFoundException ignored) { try { if (Modifier.isPrivate(type.getDeclaredConstructor().getModifiers())) { logger.warn("Constructor for '{}' exists but is private, falling back on reflection", type); return backupFactory.createConstructor(type); } CtClass constructorClass = pool.makeClass(type.getName() + "_ReflectConstructor"); constructorClass.setInterfaces(new CtClass[]{objectConstructorInterface}); CtMethod method = CtNewMethod.make("public Object construct() { return new " + type.getName() + "();}", constructorClass); constructorClass.addMethod(method); return (ObjectConstructor<T>) (constructorClass.toClass(type.getClassLoader(), type.getProtectionDomain()).getConstructor().newInstance()); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | CannotCompileException e) { logger.error("Error instantiating constructor object for '{}', falling back on reflection", type, e); return backupFactory.createConstructor(type); } catch (NoSuchMethodException e) { return null; } } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { logger.error("Error instantiating constructor object for '{}', falling back on reflection", type, e); return backupFactory.createConstructor(type); } }
@SuppressWarnings({"deprecation"}) protected static Object makeDummyObject (String className) { try { ClassLoader isolation = new ClassLoader() {}; ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(Dummy.class)); CtClass clazz = cp.get(Dummy.class.getName()); clazz.setName(className); return clazz.toClass(isolation).newInstance(); } catch ( Exception e ) { e.printStackTrace(); return new byte[0]; } }
public static Encoder gen(Class clazz, String cacheKey, CodegenResult source) throws Exception { source.flushBuffer(); CtClass ctClass = pool.makeClass(cacheKey); ctClass.setInterfaces(new CtClass[]{pool.get(Encoder.class.getName())}); String staticCode = source.toString(); CtMethod staticMethod = CtNewMethod.make(staticCode, ctClass); ctClass.addMethod(staticMethod); String wrapperCode = source.generateWrapperCode(clazz); if ("true".equals(System.getenv("JSONITER_DEBUG"))) { System.out.println(">>> " + cacheKey); System.out.println(wrapperCode); System.out.println(staticCode); } CtMethod interfaceMethod = CtNewMethod.make(wrapperCode, ctClass); ctClass.addMethod(interfaceMethod); return (Encoder) ctClass.toClass().newInstance(); } }
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); try { return anno.toAnnotationType(cl2, cp); } catch (ClassNotFoundException e2){ try { Class clazz = cp.get(anno.getTypeName()).toClass(); return javassist.bytecode.annotation.AnnotationImpl.make( clazz.getClassLoader(), clazz, cp, anno); } catch (Throwable e3) { throw new ClassNotFoundException(anno.getTypeName()); } } } }
ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
return mCtc.toClass(loader, pd); } catch (RuntimeException e) { throw e;
return mCtc.toClass(loader, pd); } catch (RuntimeException e) { throw e;
private static Object toAnnoType(Annotation anno, ClassPool cp) throws ClassNotFoundException { try { ClassLoader cl = cp.getClassLoader(); return anno.toAnnotationType(cl, cp); } catch (ClassNotFoundException e) { ClassLoader cl2 = cp.getClass().getClassLoader(); try { return anno.toAnnotationType(cl2, cp); } catch (ClassNotFoundException e2){ try { Class<?> clazz = cp.get(anno.getTypeName()).toClass(); return javassist.bytecode.annotation.AnnotationImpl.make( clazz.getClassLoader(), clazz, cp, anno); } catch (Throwable e3) { throw new ClassNotFoundException(anno.getTypeName()); } } } }
scClass.addConstructor(happyConst); Class myClass = scClass.toClass(ScorecardModel.class.getClassLoader(), null); Constructor<ScorecardModel> co = myClass.getConstructor(ScorecardModel.class); ScorecardModel jitted_scm = co.newInstance(scm);
return cl.toClass();