public CtClass makeNestedClass(String name, boolean isStatic) { if (!isStatic) throw new RuntimeException( "sorry, only nested static class is supported"); checkModify(); CtClass c = classPool.makeNestedClass(getName() + "$" + name); ClassFile cf = getClassFile2(); ClassFile cf2 = c.getClassFile2(); InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute( InnerClassesAttribute.tag); if (ica == null) { ica = new InnerClassesAttribute(cf.getConstPool()); cf.addAttribute(ica); } ica.append(c.getName(), this.getName(), name, (cf2.getAccessFlags() & ~AccessFlag.SUPER) | AccessFlag.STATIC); cf2.addAttribute(ica.copy(cf2.getConstPool(), null)); return c; }
public CtClass[] getNestedClasses() throws NotFoundException { ClassFile cf = getClassFile2(); InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag); if (ica == null) return new CtClass[0]; String thisName = cf.getName() + "$"; int n = ica.tableLength(); ArrayList list = new ArrayList(n); for (int i = 0; i < n; i++) { String name = ica.innerClass(i); if (name != null) if (name.startsWith(thisName)) { // if it is an immediate nested class if (name.lastIndexOf('$') < thisName.length()) list.add(classPool.get(name)); } } return (CtClass[])list.toArray(new CtClass[list.size()]); }
CtNewClass(String name, ClassPool cp, boolean isInterface, CtClass superclass) { super(name, cp); wasChanged = true; String superName; if (isInterface || superclass == null) superName = null; else superName = superclass.getName(); classfile = new ClassFile(isInterface, name, superName); if (isInterface && superclass != null) classfile.setInterfaces(new String[] { superclass.getName() }); setModifiers(Modifier.setPublic(getModifiers())); hasConstructor = isInterface; }
private void testExistingMethod(MethodInfo newMinfo) throws DuplicateMemberException { String name = newMinfo.getName(); String descriptor = newMinfo.getDescriptor(); ListIterator it = methods.listIterator(0); while (it.hasNext()) if (isDuplicated(newMinfo, name, descriptor, (MethodInfo)it.next(), it)) throw new DuplicateMemberException("duplicate method: " + name + " in " + this.getName()); }
public void setGenericSignature(String sig) { ClassFile cf = getClassFile(); SignatureAttribute sa = new SignatureAttribute(cf.getConstPool(), sig); cf.addAttribute(sa); }
ClassPool cp = ClassPool.getDefault(); cp.insertClassPath(new ClassClassPath(ConfigVariationsTestSuiteBuilder.class)); CtClass cl = cp.makeClass(pkg + "." + clsName); cl.setSuperclass(cp.get(cls.getName())); ClassFile ccFile = cl.getClassFile(); ConstPool constpool = ccFile.getConstPool(); mtd.getMethodInfo().addAttribute(attr); cl.addMethod(mtd); return cl.toClass();
} catch (ClassNotFoundException e) { ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName))); Class<?> type = parameterTypes[i]; Annotation[] annotations = parameterAnnotations[i]; AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(Constraint.class)) { javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation( classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName())); Method[] members = annotation.annotationType().getMethods(); for (Method member : members) { if (null != value) { MemberValue memberValue = createMemberValue( classFile.getConstPool(), pool.get(member.getReturnType().getName()), value); ja.addMemberValue(member.getName(), memberValue); CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName)); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
private static CtMethod delegator0(CtMethod delegate, CtClass declaring) throws CannotCompileException, NotFoundException MethodInfo deleInfo = delegate.getMethodInfo2(); String methodName = deleInfo.getName(); String desc = deleInfo.getDescriptor(); ConstPool cp = declaring.getClassFile2().getConstPool(); MethodInfo minfo = new MethodInfo(cp, methodName, desc); minfo.setAccessFlags(deleInfo.getAccessFlags()); boolean isStatic = Modifier.isStatic(delegate.getModifiers()); CtClass deleClass = delegate.getDeclaringClass(); CtClass[] params = delegate.getParameterTypes(); int s;
ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bout); ClassFile classFile = clazz.getClassFile(); out.writeUTF(javaName); CtMethod[] methods = clazz.getDeclaredMethods(); int classMods = clazz.getModifiers(); if ((classMods & Modifier.INTERFACE) != 0) if (methods.length > 0) String[] interfaces = classFile.getInterfaces(); for (int i = 0; i < interfaces.length; i++) interfaces[i] = javaName(interfaces[i]); out.writeUTF(field.getName()); out.writeInt(mods); out.writeUTF(field.getFieldInfo2().getDescriptor()); if (classFile.getStaticInitializer() != null) { out.writeUTF("<clinit>"); out.writeInt(Modifier.STATIC); out.writeInt(mods); out.writeUTF(constructor.getMethodInfo2() .getDescriptor().replace('/', '.'));
public boolean subtypeOf(CtClass clazz) throws NotFoundException { int i; String cname = clazz.getName(); if (this == clazz || getName().equals(cname)) return true; ClassFile file = getClassFile2(); String supername = file.getSuperclass(); if (supername != null && supername.equals(cname)) return true; String[] ifs = file.getInterfaces(); int num = ifs.length; for (i = 0; i < num; ++i) if (ifs[i].equals(cname)) return true; if (supername != null && classPool.get(supername).subtypeOf(clazz)) return true; for (i = 0; i < num; ++i) if (classPool.get(ifs[i]).subtypeOf(clazz)) return true; return false; }
private static String addBodyMethod(CtClassType clazz, ClassFile classfile, CtMethod src) throws BadBytecode, CannotCompileException { Hashtable bodies = clazz.getHiddenMethods(); String bodyname = (String)bodies.get(src); if (bodyname == null) { do { bodyname = addedWrappedMethod + clazz.getUniqueNumber(); } while (classfile.getMethod(bodyname) != null); ClassMap map = new ClassMap(); map.put(src.getDeclaringClass().getName(), clazz.getName()); MethodInfo body = new MethodInfo(classfile.getConstPool(), bodyname, src.getMethodInfo2(), map); int acc = body.getAccessFlags(); body.setAccessFlags(AccessFlag.setPrivate(acc)); body.addAttribute(new SyntheticAttribute(classfile.getConstPool())); // a stack map is copied. rebuilding it is not needed. classfile.addMethod(body); bodies.put(src, bodyname); CtMember.Cache cache = clazz.hasMemberCache(); if (cache != null) cache.addMethod(new CtMethod(body, clazz)); } return bodyname; }
throws CompileError String fieldName = finfo.getName(); String key = fieldName + ":getter"; Object res = accessors.get(key); return (MethodInfo)res; // already exists. ClassFile cf = clazz.getClassFile(); // turn on the modified flag. String accName = findAccessorName(cf); try { ConstPool cp = cf.getConstPool(); ClassPool pool = clazz.getClassPool(); String fieldType = finfo.getDescriptor(); String accDesc; if (is_static) accDesc = "()" + fieldType; else accDesc = "(" + Descriptor.of(clazz) + ")" + fieldType; code.addReturn(Descriptor.toCtClass(fieldType, pool)); minfo.setCodeAttribute(code.toCodeAttribute()); cf.addMethod(minfo); accessors.put(key, minfo); return minfo;
public CtBehavior getEnclosingBehavior() throws NotFoundException { ClassFile cf = getClassFile2(); EnclosingMethodAttribute ema = (EnclosingMethodAttribute)cf.getAttribute( EnclosingMethodAttribute.tag); if (ema == null) return null; else { CtClass enc = classPool.get(ema.className()); String name = ema.methodName(); if (MethodInfo.nameInit.equals(name)) return enc.getConstructor(ema.methodDescriptor()); else if(MethodInfo.nameClinit.equals(name)) return enc.getClassInitializer(); else return enc.getMethod(name, ema.methodDescriptor()); } }
= AccessFlag.toModifier(cf.getAccessFlags() & ~AccessFlag.SYNCHRONIZED); out.println("major: " + cf.major + ", minor: " + cf.minor + " modifiers: " + Integer.toHexString(cf.getAccessFlags())); out.println(Modifier.toString(mod) + " class " + cf.getName() + " extends " + cf.getSuperclass()); String[] infs = cf.getInterfaces(); if (infs != null && infs.length > 0) { out.print(" implements "); list = cf.getFields(); n = list.size(); for (int i = 0; i < n; ++i) { FieldInfo finfo = (FieldInfo)list.get(i); int acc = finfo.getAccessFlags(); out.println(Modifier.toString(AccessFlag.toModifier(acc)) + " " + finfo.getName() + "\t" + finfo.getDescriptor()); printAttributes(finfo.getAttributes(), out, 'f'); list = cf.getMethods(); n = list.size(); for (int i = 0; i < n; ++i) { MethodInfo minfo = (MethodInfo)list.get(i); int acc = minfo.getAccessFlags(); out.println(Modifier.toString(AccessFlag.toModifier(acc)) + " " + minfo.getName() + "\t" + minfo.getDescriptor());
return consDesc; // already exists. consDesc = Descriptor.appendParameter(lastParamType, desc); ClassFile cf = clazz.getClassFile(); // turn on the modified flag. try { ConstPool cp = cf.getConstPool(); ClassPool pool = clazz.getClassPool(); MethodInfo minfo = new MethodInfo(cp, MethodInfo.nameInit, consDesc); minfo.setAccessFlags(0); minfo.addAttribute(new SyntheticAttribute(cp)); ExceptionsAttribute ea = orig.getExceptionsAttribute(); if (ea != null) minfo.addAttribute(ea.copy(cp, null)); CtClass[] params = Descriptor.getParameterTypes(desc, pool); Bytecode code = new Bytecode(cp); code.addAload(0); minfo.setCodeAttribute(code.toCodeAttribute()); cf.addMethod(minfo);
private static void updateInnerEntry(int mod, String name, CtClass clazz, boolean outer) { ClassFile cf = clazz.getClassFile2(); InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute( InnerClassesAttribute.tag); if (ica == null) return; int n = ica.tableLength(); for (int i = 0; i < n; i++) if (name.equals(ica.innerClass(i))) { int acc = ica.accessFlags(i) & AccessFlag.STATIC; ica.setAccessFlags(i, mod | acc); String outName = ica.outerClass(i); if (outName != null && outer) try { CtClass parent = clazz.getClassPool().get(outName); updateInnerEntry(mod, name, parent, false); } catch (NotFoundException e) { throw new RuntimeException("cannot find the declaring class: " + outName); } break; } } }
private static void updateInnerEntry(int newMod, String name, CtClass clazz, boolean outer) { ClassFile cf = clazz.getClassFile2(); InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag); if (ica != null) { int isStatic = ica.accessFlags(i) & AccessFlag.STATIC; if (isStatic != 0 || !Modifier.isStatic(newMod)) { clazz.checkModify(); ica.setAccessFlags(i, AccessFlag.of(mod) | isStatic); String outName = ica.outerClass(i); if (outName != null && outer) try { CtClass parent = clazz.getClassPool().get(outName); updateInnerEntry(mod, name, parent, false); throw new RuntimeException("cannot change " + Descriptor.toJavaName(name) + " into a static class");
public CtClass[] getInterfaces() throws NotFoundException { String[] ifs = getClassFile2().getInterfaces(); int num = ifs.length; CtClass[] ifc = new CtClass[num]; for (int i = 0; i < num; ++i) ifc[i] = classPool.get(ifs[i]); return ifc; }
public CtClass getSuperclass() throws NotFoundException { String supername = getClassFile2().getSuperclass(); if (supername == null) return null; else return classPool.get(supername); }