public CtClass getSuperclass() throws NotFoundException { return pool.get(javaLangObject); }
private static Type lookupType(String name) { try { return new Type(ClassPool.getDefault().get(name)); } catch (NotFoundException e) { throw new RuntimeException(e); } }
defaultPool = new ClassPool(null); defaultPool.appendSystemPath();
public static void cleanJavassistClassPath() { for (ClassClassPath ccp : ccps) { ClassPool.getDefault().removeClassPath(ccp); } ccps.clear(); } }
synchronized void classNameChanged(String oldname, CtClass clazz) { CtClass c = (CtClass)getCached(oldname); if (c == clazz) // must check this equation. removeCached(oldname); // see getAndRename(). String newName = clazz.getName(); checkNotFrozen(newName); cacheCtClass(newName, clazz, false); }
public static void addJavassistClassPath(Class<?> clazz) { ClassClassPath ccp = new ClassClassPath(clazz); ccps.add(ccp); ClassPool.getDefault().insertClassPath(ccp); }
/** * Constructs a web server. * * @param port port number */ public AppletServer(int port) throws IOException, NotFoundException, CannotCompileException { this(ClassPool.getDefault(), new StubGenerator(), port); }
/** * @param useCache false if the cached CtClass must be ignored. * @return null if the class could not be found. */ protected synchronized CtClass get0(String classname, boolean useCache) throws NotFoundException { CtClass clazz = null; if (useCache) { clazz = getCached(classname); if (clazz != null) return clazz; } if (!childFirstLookup && parent != null) { clazz = parent.get0(classname, useCache); if (clazz != null) return clazz; } clazz = createCtClass(classname, useCache); if (clazz != null) { // clazz.getName() != classname if classname is "[L<name>;". if (useCache) cacheCtClass(clazz.getName(), clazz, false); return clazz; } if (childFirstLookup && parent != null) clazz = parent.get0(classname, useCache); return clazz; }
private CtClass searchImports(String orgName) throws CompileError { if (orgName.indexOf('.') < 0) { Iterator it = classPool.getImportedPackages(); while (it.hasNext()) { String pac = (String)it.next(); String fqName = pac + '.' + orgName; try { return classPool.get(fqName); } catch (NotFoundException e) { try { if (pac.endsWith("." + orgName)) return classPool.get(pac); } catch (NotFoundException e2) {} } } } getInvalidNames().put(orgName, INVALID); throw new CompileError("no such class: " + orgName); }
/** * Creates a new annotation. * If there already exists a class/interface with the same name, * the new interface overwrites that previous one. * * @param name a fully-qualified interface name. * Or null if the annotation has no super interface. * @throws RuntimeException if the existing interface is frozen. * @since 3.19 */ public CtClass makeAnnotation(String name) throws RuntimeException { try { CtClass cc = makeInterface(name, get("java.lang.annotation.Annotation")); cc.setModifiers(cc.getModifiers() | Modifier.ANNOTATION); return cc; } catch (NotFoundException e) { // should never happen. throw new RuntimeException(e.getMessage(), 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 com.github.jobop.anylog.common.javassist.bytecode.annotation.AnnotationImpl.make( clazz.getClassLoader(), clazz, cp, anno); } catch (Throwable e3) { throw new ClassNotFoundException(anno.getTypeName()); } } } }
/** * 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. * @param ifNotFrozen throws a RuntimeException if this parameter is true * and there is a frozen class with the same name. * @see javassist.ByteArrayClassPath */ public CtClass makeClass(InputStream classfile, boolean ifNotFrozen) throws IOException, RuntimeException { compress(); classfile = new BufferedInputStream(classfile); CtClass clazz = new CtClassType(classfile, this); clazz.checkModify(); String classname = clazz.getName(); if (ifNotFrozen) checkNotFrozen(classname); cacheCtClass(classname, clazz, true); return clazz; }
pool.recordCflow(name, declaringClass.getName(), fname); try { CtClass type = pool.get("javassist.runtime.Cflow"); CtField field = new CtField(type, fname, cc); field.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
throws IOException, RuntimeException compress(); classfile = new BufferedInputStream(classfile); CtClass clazz = new CtClassType(classfile, this); clazz.checkModify(); String classname = clazz.getName(); CtClass found = checkNotExists(classname); if (found != null) return found; else { cacheCtClass(classname, clazz, true); return clazz;
/** * Creates a new public nested class. * This method is called by {@link CtClassType#makeNestedClass()}. * * @param classname a fully-qualified class name. * @return the nested class. */ synchronized CtClass makeNestedClass(String classname) { checkNotFrozen(classname); CtClass clazz = new CtNewNestedClass(classname, this, false, null); cacheCtClass(classname, clazz, true); return clazz; }
public void setName(String name) throws RuntimeException { String oldname = getName(); if (name.equals(oldname)) return; // check this in advance although classNameChanged() below does. classPool.checkNotFrozen(name); ClassFile cf = getClassFile2(); super.setName(name); cf.setName(name); nameReplaced(); classPool.classNameChanged(oldname, this); }
/** * Removes this <code>CtClass</code> object from the * <code>ClassPool</code>. * After this method is called, any method cannot be called on the * removed <code>CtClass</code> object. * * <p>If <code>get()</code> in <code>ClassPool</code> is called * with the name of the removed method, * the <code>ClassPool</code> will read the class file again * and constructs another <code>CtClass</code> object representing * the same class. */ public void detach() { ClassPool cp = getClassPool(); CtClass obj = cp.removeCached(getName()); if (obj != this) cp.cacheCtClass(getName(), obj, false); }
/** * Lock a class into the cache * * @param c * the class */ public void lockInCache(CtClass c) { super.cacheCtClass(c.getName(), c, false); }
/** * Creates a root class pool. If <code>useDefaultPath</code> is * true, <code>appendSystemPath()</code> is called. Otherwise, * this constructor is equivalent to the constructor taking no * parameter. * * @param useDefaultPath true if the system search path is * appended. */ public ClassPool(boolean useDefaultPath) { this(null); if (useDefaultPath) appendSystemPath(); }