Search in sources :

Example 1 with InnerClassesAttribute

use of org.hotswap.agent.javassist.bytecode.InnerClassesAttribute in project HotswapAgent by HotswapProjects.

the class FieldInitLink method updateInnerEntry.

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) {
        // If the class is a static inner class, its modifier
        // does not contain the static bit.  Its inner class attribute
        // contains the static bit.
        int mod = newMod & ~Modifier.STATIC;
        int i = ica.find(name);
        if (i >= 0) {
            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);
                    } catch (NotFoundException e) {
                        throw new RuntimeException("cannot find the declaring class: " + outName);
                    }
                return;
            }
        }
    }
    if (Modifier.isStatic(newMod))
        throw new RuntimeException("cannot change " + Descriptor.toJavaName(name) + " into a static class");
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) InnerClassesAttribute(org.hotswap.agent.javassist.bytecode.InnerClassesAttribute)

Example 2 with InnerClassesAttribute

use of org.hotswap.agent.javassist.bytecode.InnerClassesAttribute in project HotswapAgent by HotswapProjects.

the class FieldInitLink method makeNestedClass.

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;
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) InnerClassesAttribute(org.hotswap.agent.javassist.bytecode.InnerClassesAttribute)

Example 3 with InnerClassesAttribute

use of org.hotswap.agent.javassist.bytecode.InnerClassesAttribute in project HotswapAgent by HotswapProjects.

the class FieldInitLink method getNestedClasses.

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()]);
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) ArrayList(java.util.ArrayList) InnerClassesAttribute(org.hotswap.agent.javassist.bytecode.InnerClassesAttribute)

Example 4 with InnerClassesAttribute

use of org.hotswap.agent.javassist.bytecode.InnerClassesAttribute in project HotswapAgent by HotswapProjects.

the class FieldInitLink method getDeclaringClass.

public CtClass getDeclaringClass() throws NotFoundException {
    ClassFile cf = getClassFile2();
    InnerClassesAttribute ica = (InnerClassesAttribute) cf.getAttribute(InnerClassesAttribute.tag);
    if (ica == null)
        return null;
    String name = getName();
    int n = ica.tableLength();
    for (int i = 0; i < n; ++i) if (name.equals(ica.innerClass(i))) {
        String outName = ica.outerClass(i);
        if (outName != null)
            return classPool.get(outName);
        else {
            // maybe anonymous or local class.
            EnclosingMethodAttribute ema = (EnclosingMethodAttribute) cf.getAttribute(EnclosingMethodAttribute.tag);
            if (ema != null)
                return classPool.get(ema.className());
        }
    }
    return null;
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) InnerClassesAttribute(org.hotswap.agent.javassist.bytecode.InnerClassesAttribute) EnclosingMethodAttribute(org.hotswap.agent.javassist.bytecode.EnclosingMethodAttribute)

Aggregations

ClassFile (org.hotswap.agent.javassist.bytecode.ClassFile)4 InnerClassesAttribute (org.hotswap.agent.javassist.bytecode.InnerClassesAttribute)4 ArrayList (java.util.ArrayList)1 EnclosingMethodAttribute (org.hotswap.agent.javassist.bytecode.EnclosingMethodAttribute)1