Search in sources :

Example 16 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class InnerClassesInfo method buildInnerClassesAttribute.

/*
     * Make this class a member nested class of the given class or move it to top-level
     *
     * @param enclosingClass the new outer class of this class, or null to make it a toplevel class.
     * @param innerName the simple name of this member class.
     */
/*
    public void setEnclosingClass(ClassInfo enclosingClass, String innerName) throws AppInfoException {
        // implement setOuterClass(), if needed. But this is not a simple task.
        // need to
        // - remove references to old outerClass from InnerClasses of this and all old outer classes
        // - create InnerClasses attribute if none exists
        // - add new entries to InnerClasses of this and all new outer classes.
        // - update class hierarchy infos and fullyKnown flags
        // - handle setEnclosingClass(null)
    }
    */
/*
     * Make this class a local nested class of the given method.
     * @param methodInfo the enclosing method of this class.
     */
/*
    public void setEnclosingMethod(MethodInfo methodInfo) {
    }
    */
/*
     * Add or replace InnerClass entries of this class with the info of the given classes.
     * If no InnerClasses attribute exists, it is created. Enclosing classes are added if needed.
     *
     * @param nestedClasses a list of nested classes to add to the InnerClasses attribute.
     */
/*
    public void addInnerClassRefs(Collection<ClassRef> nestedClasses) {
        if ( nestedClasses == null || nestedClasses.isEmpty() ) {
            return;
        }

        Map<String, InnerClass> classes = buildInnerClasses(nestedClasses);

        // create an InnerClasses attribute if it does not exist
        // add all InnerClasses to the existing InnerClasses (replace entries if existing)
    }
    */
/**
     * Build a new InnerClasses Attribute containing entries for all inner classes referenced by the current
     * constantpool, using the current ClassInfos or the old InnerClasses attribute to build the table.
     *
     * @return a new InnerClasses attribute or null if this class does not reference any inner classes.
     */
public InnerClasses buildInnerClassesAttribute() {
    ConstantPoolGen cpg = classGen.getConstantPool();
    // check+update InnerClasses attribute (and add referenced nested classes)
    List<ClassRef> referencedClasses = new LinkedList<ClassRef>();
    for (String name : ConstantPoolReferenceFinder.findReferencedClasses(classInfo)) {
        referencedClasses.add(classInfo.getAppInfo().getClassRef(name));
    }
    // find all referenced classes recursively, build InnerClass list from ClassInfo or old InnerClasses
    Collection<InnerClass> classes = buildInnerClasses(referencedClasses);
    if (classes.isEmpty()) {
        return null;
    }
    InnerClass[] ics = null;
    ics = classes.toArray(ics);
    int length = ics.length * 8 + 2;
    return new InnerClasses(cpg.addUtf8("InnerClasses"), length, ics, cpg.getConstantPool());
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) ClassRef(com.jopdesign.common.type.ClassRef) InnerClass(org.apache.bcel.classfile.InnerClass) InnerClasses(org.apache.bcel.classfile.InnerClasses) LinkedList(java.util.LinkedList)

Example 17 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class MemberInfo method setSynthetic.

////////////////////////////////////////////////////////////////////////////
// Attribute access and helpers for custom attributes
////////////////////////////////////////////////////////////////////////////    
public void setSynthetic(boolean flag) {
    // from major version 49 on, ACC_SYNTHETIC is supported
    Synthetic s = findSynthetic();
    if (getClassInfo().getMajor() < 49) {
        if (flag) {
            if (s == null) {
                ConstantPoolGen cpg = getClassInfo().getConstantPoolGen();
                int index = cpg.addUtf8("Synthetic");
                addAttribute(new Synthetic(index, 0, new byte[0], cpg.getConstantPool()));
            }
        } else {
            if (s != null) {
                removeAttribute(s);
            }
        }
    } else {
        accessFlags.isSynthetic(flag);
        if (!flag && s != null) {
            removeAttribute(s);
        }
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) Synthetic(org.apache.bcel.classfile.Synthetic)

Example 18 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class MemberInfo method getAnnotation.

/**
     * Get the annotation attribute of this member
     * @param visible whether to the the visible or invisible annotation attribute (see {@link AnnotationAttribute#isVisible()}
     * @param create if true, create the attribute if it does not exist
     * @return the annotation attribute or null if it does not exist and {@code create} is false
     */
public AnnotationAttribute getAnnotation(boolean visible, boolean create) {
    for (Attribute a : getAttributes()) {
        if (a instanceof AnnotationAttribute) {
            if (((AnnotationAttribute) a).isVisible() == visible) {
                return (AnnotationAttribute) a;
            }
        }
    }
    if (create) {
        ConstantPoolGen cpg = getClassInfo().getConstantPoolGen();
        String name = visible ? AnnotationReader.VISIBLE_ANNOTATION_NAME : AnnotationReader.INVISIBLE_ANNOTATION_NAME;
        AnnotationAttribute a = new AnnotationAttribute(cpg.addUtf8(name), 0, cpg.getConstantPool(), visible, 0);
        a.updateLength();
        addAttribute(a);
        return a;
    }
    return null;
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) AnnotationAttribute(com.jopdesign.common.bcel.AnnotationAttribute) Attribute(org.apache.bcel.classfile.Attribute) CallString(com.jopdesign.common.code.CallString) AnnotationAttribute(com.jopdesign.common.bcel.AnnotationAttribute)

Example 19 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class MemberInfo method setUnusedAnnotation.

public void setUnusedAnnotation() {
    AnnotationAttribute a = getAnnotation(false, true);
    if (a.findAnnotation(AnnotationAttribute.UNUSED_TAG_NAME) != null)
        return;
    ConstantPoolGen cpg = getConstantPoolGen();
    int nameIdx = cpg.addUtf8(AnnotationAttribute.UNUSED_TAG_NAME);
    Annotation an = new Annotation(nameIdx, cpg.getConstantPool(), 0);
    a.addAnnotation(an);
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) AnnotationAttribute(com.jopdesign.common.bcel.AnnotationAttribute) Annotation(com.jopdesign.common.bcel.Annotation)

Example 20 with ConstantPoolGen

use of org.apache.bcel.generic.ConstantPoolGen in project jop by jop-devel.

the class FindUsedConstants method visitJavaClass.

public void visitJavaClass(JavaClass clazz) {
    super.visitJavaClass(clazz);
    cpool = new ConstantPoolGen(clazz.getConstantPool());
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (!(methods[i].isAbstract() || methods[i].isNative())) {
            // methods[i] = find(methods[i]);
            find(methods[i]);
        }
    }
//		clazz.setConstantPool(cpoolNew.getConstantPool());
//System.out.println(clazz.getConstantPool());
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) Method(org.apache.bcel.classfile.Method)

Aggregations

ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)28 Instruction (org.apache.bcel.generic.Instruction)6 InstructionHandle (org.apache.bcel.generic.InstructionHandle)6 FieldInstruction (org.apache.bcel.generic.FieldInstruction)5 InstructionList (org.apache.bcel.generic.InstructionList)5 CallString (com.jopdesign.common.code.CallString)4 Constant (org.apache.bcel.classfile.Constant)4 Method (org.apache.bcel.classfile.Method)4 ObjectType (org.apache.bcel.generic.ObjectType)4 Type (org.apache.bcel.generic.Type)4 MethodInfo (com.jopdesign.common.MethodInfo)3 AnnotationAttribute (com.jopdesign.common.bcel.AnnotationAttribute)3 ClassRef (com.jopdesign.common.type.ClassRef)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 ArrayInstruction (org.apache.bcel.generic.ArrayInstruction)3 BranchInstruction (org.apache.bcel.generic.BranchInstruction)3 MethodGen (org.apache.bcel.generic.MethodGen)3 ClassInfo (com.jopdesign.common.ClassInfo)2 HashedString (com.jopdesign.common.misc.HashedString)2