Example usage for org.aspectj.apache.bcel.classfile JavaClass getAttributes

List of usage examples for org.aspectj.apache.bcel.classfile JavaClass getAttributes

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.classfile JavaClass getAttributes.

Prototype

public Attribute[] getAttributes() 

Source Link

Usage

From source file:br.jabuti.device.DeviceProbeInsert.java

License:Open Source License

public Map instrument() throws InvalidInstructionException, InvalidStackArgument {

    Map mp = super.instrument();

    Hashtable hs = new Hashtable();

    Iterator it = mp.keySet().iterator();
    // para cada classe:
    while (it.hasNext()) {
        String className = (String) it.next();
        JavaClass jv = (JavaClass) mp.get(className);

        // verifica se esta classe jah foi instrumentada por
        // esse instrumentador
        Attribute[] atr = jv.getAttributes();
        boolean achou = false;
        for (int j = 0; j < atr.length && !achou; j++) {
            if (atr[j] instanceof Unknown) {
                Unknown uatr = (Unknown) atr[j];
                byte[] b = uatr.getBytes();
                achou = Arrays.equals(b, JABUTI_J2ME_INSTR_ATTRIBUTE.getBytes());
            }/* w w  w . j  a  v a  2 s.c o  m*/
        }

        // se estah em inst eh pra instrumentar a classe
        if ((!achou) && className.equals(baseClass)) {
            // chama a rotina que faz a instrumentacao
            jv = doMethodInstrument(jv);
            hs.put(className, jv);
        } else { // coloca o codigo sem instrumentar
            hs.put(className, jv);
        }
    }
    return hs;
}

From source file:br.jabuti.probe.desktop.DefaultProbeInsert.java

License:Open Source License

public Map instrument() throws InvalidInstructionException, InvalidStackArgument {
    String[] userClass = null;// w ww . j  a va2 s  .co  m
    HashSet inst = new HashSet();

    if (instrumentList != null) {
        inst.addAll(instrumentList);
    }

    // pega o nome de todas as classes instrumentaveis do programa
    userClass = prog.getCodeClasses();
    Hashtable hs = new Hashtable();

    // para cada classe:
    for (int i = 0; i < userClass.length; i++) {

        RClassCode r = (RClassCode) prog.get(userClass[i]);
        JavaClass jv = r.getTheClass();

        // verifica se esta classe jah foi instrumentada por
        // esse instrumentador
        Attribute[] atr = jv.getAttributes();
        boolean achou = false;
        for (int j = 0; j < atr.length && !achou; j++) {
            if (atr[j] instanceof Unknown) {
                Unknown uatr = (Unknown) atr[j];
                byte[] b = uatr.getBytes();
                achou = Arrays.equals(b, JABUTI_DEFAULT_INSTR_ATTRIBUTE.getBytes());
            }
        }

        // se estah em inst eh pra instrumentar a classe
        if ((!achou) && inst.contains(userClass[i]) && !jv.isInterface()) {
            // chama a rotina que faz a instrumentacao
            try {
                jv = doDefaultInstrument(jv, userClass[i]);
                hs.put(r.getName(), jv);
            } catch (Exception e) {
                hs.put(userClass[i], jv);
            }
            inst.remove(userClass[i]);
        } else { // coloca o codigo sem instrumentar
            hs.put(userClass[i], jv);
        }
    }
    return hs;
}

From source file:br.jabuti.probe.mobiledevice.HostProbeInsert.java

License:Open Source License

public Map instrument() throws InvalidInstructionException, InvalidStackArgument {
    HashSet inst = new HashSet();

    Map mp = super.instrument();

    if (classList != null) {
        inst.addAll(classList);/*  w w w.  j  av  a2s .  co  m*/
    }

    Hashtable hs = new Hashtable();

    Iterator it = mp.keySet().iterator();
    // para cada classe:
    while (it.hasNext()) {
        String className = (String) it.next();
        JavaClass jv = (JavaClass) mp.get(className);

        // verifica se esta classe jah foi instrumentada por
        // esse instrumentador
        Attribute[] atr = jv.getAttributes();
        boolean achou = false;
        for (int j = 0; j < atr.length && !achou; j++) {
            if (atr[j] instanceof Unknown) {
                Unknown uatr = (Unknown) atr[j];
                byte[] b = uatr.getBytes();
                achou = Arrays.equals(b, JABUTI_HOST_INSTR_ATTRIBUTE.getBytes());
            }
        }

        // se estah em inst eh pra instrumentar a classe
        if ((!achou) && inst.contains(className)) {
            // chama a rotina que faz a instrumentacao
            jv = doClassInstrument(jv);
            jv = doMethodInstrument(jv);
            inst.remove(className);
            hs.put(className, jv);
        } else { // coloca o codigo sem instrumentar
            hs.put(className, jv);
        }
    }
    return hs;
}

From source file:org.caesarj.mixer.intern.ClassModifyingVisitor.java

License:Open Source License

protected JavaClass transform(JavaClass clazz) throws MixerException {
    // empty collection of anonymous inner classes
    anonymousInners = new Vector<String>();

    oldOuterClassName = Tools.getOuterClass(clazz, oldClassName);
    oldSuperclassName = clazz.getSuperclassName();

    // create a copy as work base
    JavaClass newClass = clazz.copy();

    // find indices of class and super class name
    int classNameIndex = newClass.getClassNameIndex(), superclassNameIndex = newClass.getSuperclassNameIndex();
    ConstantClass cc = (ConstantClass) newClass.getConstantPool().getConstant(classNameIndex),
            csc = (ConstantClass) newClass.getConstantPool().getConstant(superclassNameIndex);
    classNameIndex = cc.getNameIndex();//from w w w.  j a  va 2s  . co  m
    superclassNameIndex = csc.getNameIndex();

    // Set new class & superclass name
    newClass.getConstantPool().setConstant(superclassNameIndex, new ConstantUtf8(newSuperclassName));
    newClass.getConstantPool().setConstant(classNameIndex, new ConstantUtf8(newClassName));

    // visit fields, methods and local variables to replace type references
    new DescendingVisitor(newClass, this).visit();

    // Delete all inner class references 
    Attribute[] atts = newClass.getAttributes();
    Vector<Attribute> v = new Vector<Attribute>();
    for (int i = 0; i < atts.length; i++) {
        Attribute attribute = atts[i];
        if (attribute.getTag() == Constants.ATTR_INNER_CLASSES) {
            InnerClasses ic = (InnerClasses) attribute;
            ic.setInnerClasses(new InnerClass[0]);
            ic.setLength(2);

        }
        v.add(attribute);
    }
    atts = v.toArray(new Attribute[v.size()]);
    newClass.setAttributes(atts);

    newClass = removeFactoryMethods(newClass);

    // Mixin classes must be abstract, because they do not implement factory methods,
    // (if the class is final => it was a anonymous inner class)
    if (!newClass.isFinal()) {
        newClass.setAccessFlags(newClass.getAccessFlags() | Constants.ACC_ABSTRACT);
    }

    // take a look at all methodrefs
    modifyMethodAndFieldRefs(newClass);

    // return generated class
    return newClass;
}

From source file:org.caesarj.mixer.intern.Tools.java

License:Open Source License

public static InnerClasses getInnerClassesAttribute(JavaClass clazz) {
    Attribute[] attributes = clazz.getAttributes();
    for (int i = 0; i < attributes.length; i++) {
        Attribute attribute = attributes[i];
        if (attribute.getTag() == Constants.ATTR_INNER_CLASSES) {
            return ((InnerClasses) attribute);
        }//from   w  ww  .  j av a 2s .  co  m
    }
    return null;
}

From source file:org.caesarj.mixer.intern.Tools.java

License:Open Source License

public static void addAttribute(JavaClass clazz, Attribute att) {
    Attribute[] attributes = clazz.getAttributes();
    Attribute[] newAttributes = new Attribute[attributes.length + 1];
    for (int i = 0; i < attributes.length + 1; i++) {
        if (i < attributes.length)
            newAttributes[i] = attributes[i];
        else//from  w  w  w . j  av  a 2s .  c  o  m
            newAttributes[i] = att;
    }
    clazz.setAttributes(newAttributes);
}