List of usage examples for org.aspectj.apache.bcel.generic Type getReturnType
public static Type getReturnType(String signature)
From source file:br.jabuti.graph.datastructure.ig.InstructionGraph.java
License:Open Source License
/** <P>This is the important method in this class. Once the graph * has been created, it will fill the instruction nodes with some * information, as described in {@link InstructionNode}. <p> * <p>The algorithm is similar to that presented by Sun to compute * the stack to the instruction. There is local array where * the "changed" instructions are inserted. The program removes an * instruction from it and compute for all its successors the * stack and local variable configuration. If they changed for a * given successor x, then x is added to the changed array. Exception * handlers (i.e., secondary edges) are also used to get the * successor set.</p>//from w w w .j a va2s . c o m * <p>Initialy the entry node and all the exception handlers are inserted * in the changed array. In addition, the array is consulted using a stack * policy, except for the initial insertion of the exception handlers * that are placed at the bottom of the stack. This because the handlers * are changed very often. And then pl;acing them at the bottom might * avoid removing/inserting it in the array many times * </p> * * @param all Indicates if all the possible configurations should be * computed or not. Computing all the configurations means that * all the possible configurations that arrive at a struction will * be store. The "not all" option means that a new configuration * will be merged with the old one and only one is stored * * @throws InvalidInstructionException If the method finds an node with * a non valid JVM instruction * @throws InvalidStackArgument If e struction is reached by two * configuration of the stack with differen sizes */ public void calcStack(boolean all) throws InvalidInstructionException, InvalidStackArgument { if (il == null) { return; } Vector changed = new Vector(); ConstantPoolGen cp = meth.getConstantPool(); calcReqLocal(); // pega o tipo das variaveis locais dos argumentos Type[] pars = meth.getArgumentTypes(); int h = meth.getMaxLocals(); VMLocal locals = new VMLocal(h); // pega a instrucao inicial InstructionNode start = (InstructionNode) getEntryNodes(); // se nao eh static tem que colocar "this" em local[0] int l = 0; if (!meth.isStatic()) { String s = meth.getClassName(); s = "()L" + s.replace('.', '/') + ";"; Type t = Type.getReturnType(s); locals.add(t, l++); start.defLocalAdd(InstructionNode.STR_LOCAL + 0); } // coloca or argumentos nas outras "locals" for (int j = 0; j < pars.length; l += pars[j].getSize(), j++) { locals.add(pars[j], l); start.defLocalAdd(InstructionNode.STR_LOCAL + l); } // calcula o stack para a instrucao inicial if (all) { start.initAllStack(new VMStack(meth.getMaxStack()), locals, cp); } else { start.initStack(new VMStack(meth.getMaxStack()), locals, cp); } changed.add(start); // calcula o stack e locais para cada exception handler CodeExceptionGen[] ceg = meth.getExceptionHandlers(); Hashtable hs = new Hashtable(); for (int i = 0; i < ceg.length; i++) { Type tex = ceg[i].getCatchType(); if (tex == null) { tex = EXCEPTION_TYPE; } hs.put(ceg[i].getHandlerPC(), tex); } locals = new VMLocal(h); VMStack exst = new VMStack(meth.getMaxStack()); for (int i = 0; i < size(); i++) { InstructionNode vef = (InstructionNode) elementAt(i); Type tex = (Type) hs.get(vef.ih); if (tex == null) { continue; } exst.push(new VMStackElement(tex, "", null)); if (all) { vef.initAllStack(exst); } else { vef.initStack(exst, locals, cp); } changed.add(vef); // insere no fim embora "changed" seja // usado como pilha exst.pop(); // esvazia a pilha p/ o proximo } while (!changed.isEmpty()) { InstructionNode curr = (InstructionNode) changed.remove(0); curr.setChanged(false); // atualiza cada sucessor Set<GraphNode> nx = getArrivingNodesByPrimaryEdge(curr); Iterator<GraphNode> i = nx.iterator(); while (i.hasNext()) { InstructionNode fx = (InstructionNode) i.next(); boolean b = fx.changed(); if (all) { fx.calcAllStack(curr, cp); } else { fx.calcStack(curr, cp); } if ((!b) && fx.changed()) { changed.insertElementAt(fx, 0); } else { fx.setChanged(b); } } // atualiza cada tratador de excessoes Set<GraphNode> ex = getArrivingNodesBySecondaryEdge(curr); Iterator<GraphNode> j = ex.iterator(); while (j.hasNext()) { InstructionNode exi = (InstructionNode) j.next(); boolean b = exi.changed(); if (all) { exi.calcAllLocal(curr, cp); } else { exi.calcLocal(curr, cp); } if ((!b) && exi.changed()) { changed.insertElementAt(exi, 0); } else { exi.setChanged(b); } } curr.removeNextStackLocal(); } // now merge the subrotines // mergeJSR(); }
From source file:org.caesarj.mixer.intern.ClassModifyingVisitor.java
License:Open Source License
/** * Checks method references to super-constructors and outerclass methods * and modifies them to refer to the correct new outer classes. *//*from w ww . ja v a 2s.co m*/ protected void modifyMethodAndFieldRefs(JavaClass clazz) { ConstantPool cp = clazz.getConstantPool(); for (int i = 1; i < cp.getLength(); i++) { Constant c = cp.getConstant(i); if (c == null) continue; if (c.getTag() == Constants.CONSTANT_Fieldref) { ConstantFieldref fieldRef = (ConstantFieldref) c; String targetClass = fieldRef.getClass(cp); if (Tools.sameClass(targetClass, oldOuterClassName)) { int classIndex = fieldRef.getClassIndex(); ConstantClass cc = (ConstantClass) cp.getConstant(classIndex); int nameIndex = cc.getNameIndex(); cp.setConstant(nameIndex, new ConstantUtf8(newOuterClassName)); } } else if (c.getTag() == Constants.CONSTANT_Methodref) { ConstantMethodref mr = (ConstantMethodref) c; String targetClassName = mr.getClass(cp); int nameAndTypeIndex = mr.getNameAndTypeIndex(); ConstantNameAndType nat = ((ConstantNameAndType) cp.getConstant(nameAndTypeIndex)); String methodName = nat.getName(cp); // check for innerclass construction if (Tools.isPrefix(oldClassName, targetClassName)) { String innerIdent = targetClassName.substring(oldClassName.length() + 1); String newInnerName = newClassName + "$" + innerIdent; try { Integer.parseInt(innerIdent); } catch (Exception e) { // not an anonymous inner class continue; } // Change target class to new inner class int targetClassIndex = mr.getClassIndex(); int targetNameIndex = ((ConstantClass) cp.getConstant(targetClassIndex)).getNameIndex(); cp.setConstant(targetNameIndex, new ConstantUtf8(newInnerName)); // Change argument class to new class Type[] args = Type.getArgumentTypes(nat.getSignature(cp)); for (int j = 0; j < args.length; j++) { Type arg = args[j]; String signature = arg.getSignature(); String argumentType = arg.toString(); if (Tools.sameClass(argumentType, oldClassName)) { args[j] = Type.getType("L" + newClassName + ";"); } } String signature = Type.getMethodSignature(Type.getReturnType(nat.getSignature(cp)), args); int signatureIndex = nat.getSignatureIndex(); cp.setConstant(signatureIndex, new ConstantUtf8(signature)); } // Check for superconstructor calls with otuer class parameter if (Tools.sameClass(targetClassName, newSuperclassName)) { if (methodName.equals("<init>")) { Type[] args = Type.getArgumentTypes(nat.getSignature(cp)); if (args.length == 1) { String argumentType = args[0].toString(); // if parameter is of old super-outer-class type, set new signature if (Tools.sameClass(argumentType, outerOfOldSuper)) { cp.setConstant(nat.getSignatureIndex(), new ConstantUtf8("(L" + outerOfNewSuper + ";)V")); } } } } // check whether its a call to our old outer class if (Tools.isPrefix(targetClassName, oldOuterClassName)) { // String newTargetClass = Tools.getNewOuterName( // oldClassName, // targetClassName, // outerClasses); int classIndex = mr.getClassIndex(); ConstantClass cc = (ConstantClass) cp.getConstant(classIndex); int nameIndex = cc.getNameIndex(); cp.setConstant(nameIndex, new ConstantUtf8(newOuterClassName)); } } } }
From source file:org.caesarj.mixer.intern.ClassModifyingVisitor.java
License:Open Source License
public void visitMethod(Method method) { final ConstantPool cp = method.getConstantPool(); // we search for outer-class-access functions, which // are static, have exactly one argument of this class' type and // return an instance of the outer class' type if (method.isStatic() && method.getName().startsWith("access$")) { String returnType = Type.getReturnType(method.getSignature()).toString(); if (!Tools.sameClass(returnType, oldOuterClassName)) return; Type[] argTypes = Type.getArgumentTypes(method.getSignature()); if (argTypes.length != 1) return; // construct the new signature & use it to overwrite the old one String newSignature = "(L" + newClassName + ";)L" + newOuterClassName + ";"; int index = method.getSignatureIndex(); cp.setConstant(index, new ConstantUtf8(newSignature)); }/*from ww w .jav a 2 s . com*/ // and we check for constructors else if (method.getName().equals("<init>")) { Type[] argTypes = Type.getArgumentTypes(method.getSignature()); for (int argIdx = 0; argIdx < argTypes.length; argIdx++) { // modify the signature if neccessary if (Tools.sameClass(argTypes[argIdx].toString(), oldOuterClassName)) { // construct the new signature and use it to overwrite the old one argTypes[argIdx] = Type.getType("L" + newOuterClassName + ";"); } } // construct new signature String signature = Type.getMethodSignature(method.getReturnType(), argTypes); int signatureIndex = method.getSignatureIndex(); cp.setConstant(signatureIndex, new ConstantUtf8(signature)); } }