public void addConstructor(CtConstructor c) throws CannotCompileException { hasConstructor = true; super.addConstructor(c); }
public void addField(CtField f, String init) throws CannotCompileException { addField(f, CtField.Initializer.byExpr(init)); }
public void setSuperclass(CtClass clazz) throws CannotCompileException { checkModify(); if (isInterface()) addInterface(clazz); else getClassFile2().setSuperclass(clazz.getName()); }
public CtConstructor makeClassInitializer() throws CannotCompileException { CtConstructor clinit = getClassInitializer(); if (clinit != null) return clinit; checkModify(); ClassFile cf = getClassFile2(); Bytecode code = new Bytecode(cf.getConstPool(), 0, 0); modifyClassConstructor(cf, code, 0, 0); return getClassInitializer(); }
public void replaceClassName(String oldname, String newname) throws RuntimeException { String thisname = getName(); if (thisname.equals(oldname)) setName(newname); else { super.replaceClassName(oldname, newname); getClassFile2().renameClass(oldname, newname); nameReplaced(); } }
if (isModified()) { checkPruned("toBytecode"); ClassFile cf = getClassFile2(); if (gcConstPool) { cf.compact(); modifyClassConstructor(cf); modifyConstructors(cf); if (debugDump != null) dumpClassFile(cf); classPool.writeClassfile(getName(), out);
buffer.append("pruned "); buffer.append(Modifier.toString(getModifiers())); buffer.append(" class "); buffer.append(getName()); CtClass ext = getSuperclass(); if (ext != null) { String name = ext.getName(); CtClass[] intf = getInterfaces(); if (intf.length > 0) buffer.append(" implements "); CtMember.Cache memCache = getMembers(); exToString(buffer, " fields=", memCache.fieldHead(), memCache.lastField()); exToString(buffer, " constructors=", memCache.consHead(), memCache.lastCons()); exToString(buffer, " methods=", memCache.methodHead(), memCache.lastMethod());
public void removeMethod(CtMethod m) throws NotFoundException { checkModify(); MethodInfo mi = m.getMethodInfo2(); ClassFile cf = getClassFile2(); if (cf.getMethods().remove(mi)) { getMembers().remove(m); gcConstPool = true; } else throw new NotFoundException(m.toString()); }
public void addMethod(CtMethod m) throws CannotCompileException { checkModify(); if (m.getDeclaringClass() != this) throw new CannotCompileException("bad declaring class"); int mod = m.getModifiers(); if ((getModifiers() & Modifier.INTERFACE) != 0) { if (Modifier.isProtected(mod) || Modifier.isPrivate(mod)) throw new CannotCompileException( "an interface method must be public: " + m.toString()); m.setModifiers(mod | Modifier.PUBLIC); } getMembers().addMethod(m); getClassFile2().addMethod(m.getMethodInfo2()); if ((mod & Modifier.ABSTRACT) != 0) setModifiers(getModifiers() | Modifier.ABSTRACT); }
public void addInterface(CtClass anInterface) { checkModify(); if (anInterface != null) getClassFile2().addInterface(anInterface.getName()); }
public void setModifiers(int mod) { ClassFile cf = getClassFile2(); if (Modifier.isStatic(mod)) { int flags = cf.getInnerAccessFlags(); if (flags != -1 && (flags & AccessFlag.STATIC) != 0) mod = mod & ~Modifier.STATIC; else throw new RuntimeException("cannot change " + getName() + " into a static class"); } checkModify(); cf.setAccessFlags(AccessFlag.of(mod)); }
/** * Creates a CtClass object representing the specified class. * It first examines whether or not the corresponding class * file exists. If yes, it creates a CtClass object. * * @return null if the class file could not be found. */ protected CtClass createCtClass(String classname, boolean useCache) { // accept "[L<class name>;" as a class name. if (classname.charAt(0) == '[') classname = Descriptor.toClassName(classname); if (classname.endsWith("[]")) { String base = classname.substring(0, classname.indexOf('[')); if ((!useCache || getCached(base) == null) && find(base) == null) return null; else return new CtArray(classname, this); } else if (find(classname) == null) return null; else return new CtClassType(classname, this); }
public Object getAnnotation(Class clz) throws ClassNotFoundException { ClassFile cf = getClassFile2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); AnnotationsAttribute ainfo2 = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag); return getAnnotationType(clz, getClassPool(), ainfo, ainfo2); }
public boolean hasAnnotation(String annotationName) { ClassFile cf = getClassFile2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); AnnotationsAttribute ainfo2 = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag); return hasAnnotationType(annotationName, getClassPool(), ainfo, ainfo2); }
public Object[] getAnnotations() throws ClassNotFoundException { return getAnnotations(false); }
protected void extendToString(StringBuffer buffer) { if (hasConstructor) buffer.append("hasConstructor "); super.extendToString(buffer); }
public void defrost() { checkPruned("defrost"); wasFrozen = false; }
/** * @see javassist.CtClass#prune() * @see javassist.CtClass#stopPruning(boolean) */ public void prune() { if (wasPruned) return; wasPruned = wasFrozen = true; getClassFile2().prune(); }
if (isModified()) { checkPruned("toBytecode"); ClassFile cf = getClassFile2(); if (gcConstPool) { cf.compact(); modifyClassConstructor(cf); modifyConstructors(cf); if (debugDump != null) dumpClassFile(cf); classPool.writeClassfile(getName(), out);
buffer.append("pruned "); buffer.append(Modifier.toString(getModifiers())); buffer.append(" class "); buffer.append(getName()); CtClass ext = getSuperclass(); if (ext != null) { String name = ext.getName(); CtClass[] intf = getInterfaces(); if (intf.length > 0) buffer.append(" implements "); CtMember.Cache memCache = getMembers(); exToString(buffer, " fields=", memCache.fieldHead(), memCache.lastField()); exToString(buffer, " constructors=", memCache.consHead(), memCache.lastCons()); exToString(buffer, " methods=", memCache.methodHead(), memCache.lastMethod());