/** * Creates a public abstract method. The created method must be * added to a class with <code>CtClass.addMethod()</code>. * * @param declaring the class to which the created method is added. * @param returnType the type of the returned value * @param mname the method name * @param parameters a list of the parameter types * * @see CtClass#addMethod(CtMethod) */ public CtMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass declaring) { this(null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); String desc = Descriptor.ofMethod(returnType, parameters); methodInfo = new MethodInfo(cp, mname, desc); setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT); }
private String getMethodString(CtMethod method) { try { return Modifier.toString(method.getModifiers()) + " " + method.getReturnType().getName() + " " + method.getName() + Descriptor.toString(method.getSignature()) + ";"; } catch (NotFoundException e) { throw new RuntimeException(e); } }
/** * Obtains the name of the annotation type. * * @return the type name */ public String getTypeName() { return Descriptor.toClassName(pool.getUtf8Info(typeIndex)); }
private static String javaName(String name) { return Descriptor.toJavaName(Descriptor.toJvmName(name)); } }
/** * Returns the <code>CtClass</code> objects representing the parameter * types specified by the given descriptor. * * @param desc descriptor * @param cp the class pool used for obtaining * a <code>CtClass</code> object. */ public static CtClass[] getParameterTypes(String desc, ClassPool cp) throws NotFoundException { if (desc.charAt(0) != '(') return null; else { int num = numOfParameters(desc); CtClass[] args = new CtClass[num]; int n = 0; int i = 1; do { i = toCtClass(cp, desc, i, args, n++); } while (i > 0); return args; } }
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;
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
/** * Returns the method name followed by parameter types * such as <code>javassist.CtMethod.setBody(String)</code>. * * @since 3.5 */ public String getLongName() { return getDeclaringClass().getName() + "." + getName() + Descriptor.toString(getSignature()); }
private CtMethod addEmptySetIsNew(CtClass cc, CtClass argumentType) throws CannotCompileException { String desc = "(" + Descriptor.of(argumentType) + ")V"; ConstPool cp = cc.getClassFile().getConstPool(); MethodInfo minfo = new MethodInfo(cp, "$$_setIsNew", desc); minfo.setAccessFlags(AccessFlag.PUBLIC); CtMethod method = CtMethod.make(minfo, cc); method.setBody("{}"); cc.addMethod(method); return method; }
private CtMethod addUnmarkDirty(CtClass cc, CtField dirtyField) throws CannotCompileException { FieldInfo dirtyFieldInfo = dirtyField.getFieldInfo2(); String desc = "()" + Descriptor.of("void"); ConstPool cp = dirtyFieldInfo.getConstPool(); MethodInfo minfo = new MethodInfo(cp, "$$_unmarkDirty", desc); minfo.setAccessFlags(AccessFlag.PUBLIC); String dirtyFieldName = dirtyFieldInfo.getName(); StringBuilder sb = new StringBuilder(); sb.append("{\n"); sb.append("\t$0.").append(dirtyFieldName).append(" = 0;\n"); sb.append('}'); CtMethod method = CtMethod.make(minfo, cc); method.setBody(sb.toString()); cc.addMethod(method); return method; }
private String fixTypes2(ArrayList scc, HashSet lowersSet, ClassPool cp) throws NotFoundException { Iterator it = lowersSet.iterator(); if (lowersSet.size() == 0) return null; // only NullType else if (lowersSet.size() == 1) return (String)it.next(); else { CtClass cc = cp.get((String)it.next()); while (it.hasNext()) cc = commonSuperClassEx(cc, cp.get((String)it.next())); if (cc.getSuperclass() == null || isObjectArray(cc)) cc = fixByUppers(scc, cp, new HashSet(), cc); if (cc.isArray()) return Descriptor.toJvmName(cc); else return cc.getName(); } }
/** * Adds a new <code>CONSTANT_Class_info</code> structure. * * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure * for storing the class name. * * @return the index of the added entry. */ public int addClassInfo(CtClass c) { if (c == THIS) return thisClassInfo; else if (!c.isArray()) return addClassInfo(c.getName()); else { // an array type is recorded in the hashtable with // the key "[L<classname>;" instead of "<classname>". // // note: toJvmName(toJvmName(c)) is equal to toJvmName(c). return addClassInfo(Descriptor.toJvmName(c)); } }
.toJvmName(thisclassname)); if (jvmNewThisName != null) thisclassname = Descriptor.toJavaName(jvmNewThisName); constPool.renameClass(classnames); for (int i = 0; i < n; ++i) { MethodInfo minfo = (MethodInfo)list.get(i); String desc = minfo.getDescriptor(); minfo.setDescriptor(Descriptor.rename(desc, classnames)); AttributeInfo.renameClass(minfo.getAttributes(), classnames); for (int i = 0; i < n; ++i) { FieldInfo finfo = (FieldInfo)list.get(i); String desc = finfo.getDescriptor(); finfo.setDescriptor(Descriptor.rename(desc, classnames)); AttributeInfo.renameClass(finfo.getAttributes(), classnames);
/** * Returns the constructor name followed by parameter types * such as <code>javassist.CtConstructor(CtClass[],CtClass)</code>. * * @since 3.5 */ public String getLongName() { return getDeclaringClass().getName() + (isConstructor() ? Descriptor.toString(getSignature()) : ("." + MethodInfo.nameClinit + "()")); }
CannotCompileException thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos; String desc; if (opcode == Opcode.NEWARRAY) { index = iterator.byteAt(currentPos + 1); // atype CtPrimitiveType cpt = (CtPrimitiveType)getPrimitiveType(index); desc = "[" + cpt.getDescriptor(); index = iterator.u16bitAt(pos + 1); desc = constPool.getClassInfo(index); if (desc.startsWith("[")) desc = "[" + desc; index = iterator.u16bitAt(currentPos + 1); desc = constPool.getClassInfo(index); dim = iterator.byteAt(currentPos + 3); codeLength = 4; throw new RuntimeException("bad opcode: " + opcode); retType = Descriptor.toCtClass(desc, thisClass.getClassPool());
thisClass.getClassFile(); // to call checkModify(). ConstPool constPool = getConstPool(); int pos = currentPos; int index = iterator.u16bitAt(pos + 1); CodeAttribute ca = iterator.get(); try { CtClass[] params; CtClass retType; CtClass fieldType = Descriptor.toCtClass(constPool.getFieldrefType(index), thisClass.getClassPool()); boolean read = isReader(); if (read) { jc.recordParams(constPool.getFieldrefClassName(index), params, true, paramVar, withinStatic());
throws CannotCompileException declaringClass.checkModify(); ConstPool cp = methodInfo.getConstPool(); CodeAttribute ca = methodInfo.getCodeAttribute(); if (ca == null) throw new CannotCompileException("no method body"); String desc = Descriptor.of(type); va.addEntry(0, ca.getCodeLength(), cp.addUtf8Info(name), cp.addUtf8Info(desc), maxLocals); ca.setMaxLocals(maxLocals + Descriptor.dataSize(desc));
/** * Creates a constructor with no constructor body. * The created constructor * must be added to a class with <code>CtClass.addConstructor()</code>. * * <p>The created constructor does not include a constructor body, * which must be specified with <code>setBody()</code>. * * @param declaring the class to which the created method is added. * @param parameters a list of the parameter types * * @see CtClass#addConstructor(CtConstructor) * @see CtConstructor#setBody(String) * @see CtConstructor#setBody(CtConstructor,ClassMap) */ public CtConstructor(CtClass[] parameters, CtClass declaring) { this((MethodInfo)null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); String desc = Descriptor.ofConstructor(parameters); methodInfo = new MethodInfo(cp, "<init>", desc); setModifiers(Modifier.PUBLIC); }
/** * Returns the type of array components. If the created array is * a two-dimensional array of <tt>int</tt>, * the type returned by this method is * not <tt>int[]</tt> but <tt>int</tt>. */ public CtClass getComponentType() throws NotFoundException { if (opcode == Opcode.NEWARRAY) { int atype = iterator.byteAt(currentPos + 1); return getPrimitiveType(atype); } else if (opcode == Opcode.ANEWARRAY || opcode == Opcode.MULTIANEWARRAY) { int index = iterator.u16bitAt(currentPos + 1); String desc = getConstPool().getClassInfo(index); int dim = Descriptor.arrayDimension(desc); desc = Descriptor.toArrayComponent(desc, dim); return Descriptor.toCtClass(desc, thisClass.getClassPool()); } else throw new RuntimeException("bad opcode: " + opcode); }