/** * Returns the source file containing the expression. * * @return null if this information is not available. */ public String getFileName() { ClassFile cf = thisClass.getClassFile2(); if (cf == null) return null; else return cf.getSourceFile(); }
/** * Returns a class file for this class. * * <p>This method is not available if <code>isFrozen()</code> * is true. */ public ClassFile getClassFile() { checkModify(); return getClassFile2(); }
/** * Returns the major version of the class file * targeted by this compilation. */ public int getMajorVersion() { ClassFile cf = thisClass.getClassFile2(); if (cf == null) return ClassFile.MAJOR_VERSION; // JDK 1.3 else return cf.getMajorVersion(); }
private String getSuperclassName() { ClassFile cf = declaringClass.getClassFile2(); return cf.getSuperclass(); }
/** * Returns the major version of the class file * targeted by this compilation. */ public int getMajorVersion() { ClassFile cf = thisClass.getClassFile2(); if (cf == null) return ClassFile.MAJOR_VERSION; // JDK 1.3 return cf.getMajorVersion(); }
/** * Returns a class file for this class. * * <p>This method is not available if <code>isFrozen()</code> * is true. */ public ClassFile getClassFile() { checkModify(); return getClassFile2(); }
/** * Returns the source file containing the expression. * * @return null if this information is not available. */ public String getFileName() { ClassFile cf = thisClass.getClassFile2(); if (cf == null) return null; return cf.getSourceFile(); }
private String getSuperclassName() { ClassFile cf = declaringClass.getClassFile2(); return cf.getSuperclass(); }
/** * Constructs a compiler. * * @param thisClass the class that a compiled method/field * belongs to. */ public Javac(CtClass thisClass) { this(new Bytecode(thisClass.getClassFile2().getConstPool(), 0, 0), thisClass); }
private CtField(String typeDesc, String name, CtClass clazz) throws CannotCompileException { super(clazz); ClassFile cf = clazz.getClassFile2(); if (cf == null) throw new CannotCompileException("bad declaring class: " + clazz.getName()); fieldInfo = new FieldInfo(cf.getConstPool(), name, typeDesc); }
/** * Constructs a compiler. * * @param thisClass the class that a compiled method/field * belongs to. */ public Javac(CtClass thisClass) { this(new Bytecode(thisClass.getClassFile2().getConstPool(), 0, 0), thisClass); }
private CtField(String typeDesc, String name, CtClass clazz) throws CannotCompileException { super(clazz); ClassFile cf = clazz.getClassFile2(); if (cf == null) throw new CannotCompileException("bad declaring class: " + clazz.getName()); fieldInfo = new FieldInfo(cf.getConstPool(), name, typeDesc); }
/** * Creates a method from a <code>MethodInfo</code> object. * * @param declaring the class declaring the method. * @throws CannotCompileException if the the <code>MethodInfo</code> * object and the declaring class have different * <code>ConstPool</code> objects * @since 3.6 */ public static CtMethod make(MethodInfo minfo, CtClass declaring) throws CannotCompileException { if (declaring.getClassFile2().getConstPool() != minfo.getConstPool()) throw new CannotCompileException("bad declaring class"); return new CtMethod(minfo, declaring); }
/** * 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); }
/** * 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); }
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; } } }
public static CtConstructor wrapped(CtClass[] parameterTypes, CtClass[] exceptionTypes, int howToCallSuper, CtMethod body, ConstParameter constParam, CtClass declaring) throws CannotCompileException { try { CtConstructor cons = new CtConstructor(parameterTypes, declaring); cons.setExceptionTypes(exceptionTypes); Bytecode code = makeBody(declaring, declaring.getClassFile2(), howToCallSuper, body, parameterTypes, constParam); cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); // a stack map table is not needed. return cons; } catch (NotFoundException e) { throw new CannotCompileException(e); } }
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 static CtMethod wrapped(CtClass returnType, String mname, CtClass[] parameterTypes, CtClass[] exceptionTypes, CtMethod body, ConstParameter constParam, CtClass declaring) throws CannotCompileException { CtMethod mt = new CtMethod(returnType, mname, parameterTypes, declaring); mt.setModifiers(body.getModifiers()); try { mt.setExceptionTypes(exceptionTypes); } catch (NotFoundException e) { throw new CannotCompileException(e); } Bytecode code = makeBody(declaring, declaring.getClassFile2(), body, parameterTypes, returnType, constParam); MethodInfo minfo = mt.getMethodInfo2(); minfo.setCodeAttribute(code.toCodeAttribute()); // a stack map has been already created. return mt; }
/** * Creates a default (public) constructor. * * <p>The created constructor takes no parameter. It calls * <code>super()</code>. */ public static CtConstructor defaultConstructor(CtClass declaring) throws CannotCompileException { CtConstructor cons = new CtConstructor((CtClass[])null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); Bytecode code = new Bytecode(cp, 1, 1); code.addAload(0); try { code.addInvokespecial(declaring.getSuperclass(), "<init>", "()V"); } catch (NotFoundException e) { throw new CannotCompileException(e); } code.add(Bytecode.RETURN); // no need to construct a stack map table. cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); return cons; }