Package javassist.bytecode

Examples of javassist.bytecode.LocalVariableAttribute


            // Signatures names
            CodeAttribute codeAttribute = (CodeAttribute) method.getMethodInfo().getAttribute("Code");
            if (codeAttribute == null || javassist.Modifier.isAbstract(method.getModifiers())) {
                continue;
            }
            LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute.getAttribute("LocalVariableTable");
            List<T2<Integer, String>> parameterNames = new ArrayList<T2<Integer, String>>();

            if (localVariableAttribute == null) {
                if (method.getParameterTypes().length > 0)
                    continue;
            } else {
                if (localVariableAttribute.tableLength() < method.getParameterTypes().length + (Modifier.isStatic(method.getModifiers()) ? 0 : 1)) {
                    Logger.warn("weird: skipping method %s %s as its number of local variables is incorrect (lv=%s || lv.length=%s || params.length=%s || (isStatic? %s)", method.getReturnType().getName(), method.getLongName(), localVariableAttribute, localVariableAttribute != null ? localVariableAttribute.tableLength() : -1, method.getParameterTypes().length, Modifier.isStatic(method.getModifiers()));
                }
                for (int i = 0; i < localVariableAttribute.tableLength(); i++) {
                    if (!localVariableAttribute.variableName(i).equals("__stackRecorder")) {
                        parameterNames.add(new T2<Integer, String>(localVariableAttribute.startPc(i) + localVariableAttribute.index(i), localVariableAttribute.variableName(i)));
                    }
                }
                Collections.sort(parameterNames, new Comparator<T2<Integer, String>>() {

                    public int compare(T2<Integer, String> o1, T2<Integer, String> o2) {
                        return o1._1.compareTo(o2._1);
                    }

                });
            }
            List<String> names = new ArrayList<String>();
            for (int i = 0; i < method.getParameterTypes().length + (Modifier.isStatic(method.getModifiers()) ? 0 : 1); i++) {
                if (localVariableAttribute == null) {
                    continue;
                }
                try {
                    String name = parameterNames.get(i)._2;
                    if (!name.equals("this")) {
                        names.add(name);
                    }
                } catch (Exception e) {
                    Logger.warn(e, "While applying localvariables to %s.%s, param %s", ctClass.getName(), method.getName(), i);
                }
            }
            StringBuilder iv = new StringBuilder();
            if (names.isEmpty()) {
                iv.append("new String[0];");
            } else {
                iv.append("new String[] {");
                for (Iterator<String> i = names.iterator(); i.hasNext(); ) {
                    iv.append("\"");
                    String aliasedName = i.next();
                    if (aliasedName.contains("$")) {
                        aliasedName = aliasedName.substring(0, aliasedName.indexOf("$"));
                    }
                    iv.append(aliasedName);
                    iv.append("\"");
                    if (i.hasNext()) {
                        iv.append(",");
                    }
                }
                iv.append("};");
            }

            String sigField = "$" + method.getName() + LocalVariablesNamesTracer.computeMethodHash(method.getParameterTypes());
            try { // #1198
                ctClass.getDeclaredField(sigField);
            } catch (NotFoundException nfe) {
                CtField signature = CtField.make("public static String[] " + sigField + " = " + iv.toString(), ctClass);
                ctClass.addField(signature);
            }

            if (localVariableAttribute == null || isScala(applicationClass)) {
                continue;
            }

            // OK.
            // Here after each local variable creation instruction,
            // we insert a call to yalp.utils.LocalVariables.addVariable('var', var)
            // without breaking everything...
            for (int i = 0; i < localVariableAttribute.tableLength(); i++) {

                // name of the local variable
                String name = localVariableAttribute.getConstPool().getUtf8Info(localVariableAttribute.nameIndex(i));

                // Normalize the variable name
                // For several reasons, both variables name and name$1 will be aliased to name
                String aliasedName = name;
                if (aliasedName.contains("$")) {
                    aliasedName = aliasedName.substring(0, aliasedName.indexOf("$"));
                }


                if (name.equals("this")) {
                    continue;
                }

                /* DEBUG
                IO.write(ctClass.toBytecode(), new File("/tmp/lv_"+applicationClass.name+".class"));
                ctClass.defrost();
                 */

                try {

                    // The instruction at which this local variable has been created
                    Integer pc = localVariableAttribute.startPc(i);

                    // Move to the next instruction (insertionPc)
                    CodeIterator codeIterator = codeAttribute.iterator();
                    codeIterator.move(pc);
                    pc = codeIterator.next();

                    Bytecode b = makeBytecodeForLVStore(method, localVariableAttribute.signature(i), name, localVariableAttribute.index(i));
                    codeIterator.insert(pc, b.get());
                    codeAttribute.setMaxStack(codeAttribute.computeMaxStack());

                    // Bon chaque instruction de cette méthode
                    while (codeIterator.hasNext()) {
                        int index = codeIterator.next();
                        int op = codeIterator.byteAt(index);

                        // DEBUG
                        // printOp(op);

                        int varNumber = -1;
                        // The variable changes
                        if (storeByCode.containsKey(op)) {
                            varNumber = storeByCode.get(op);
                            if (varNumber == -2) {
                                varNumber = codeIterator.byteAt(index + 1);
                            }
                        }

                        // Si c'est un store de la variable en cours d'examination
                        // et que c'est dans la frame d'utilisation de cette variable on trace l'affectation.
                        // (en fait la frame commence à localVariableAttribute.startPc(i)-1 qui est la première affectation
                        //  mais aussi l'initialisation de la variable qui est deja tracé plus haut, donc on commence à localVariableAttribute.startPc(i))
                        if (varNumber == localVariableAttribute.index(i) && index < localVariableAttribute.startPc(i) + localVariableAttribute.codeLength(i)) {
                            b = makeBytecodeForLVStore(method, localVariableAttribute.signature(i), aliasedName, varNumber);
                            codeIterator.insertEx(b.get());
                            codeAttribute.setMaxStack(codeAttribute.computeMaxStack());
                        }
                    }
                } catch (Exception e) {
View Full Code Here


            // Signatures names
            CodeAttribute codeAttribute = (CodeAttribute) method.getMethodInfo().getAttribute("Code");
            if (codeAttribute == null || javassist.Modifier.isAbstract(method.getModifiers())) {
                continue;
            }
            LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute.getAttribute("LocalVariableTable");
            if (localVariableAttribute != null) {
                for (int i = 0; i < localVariableAttribute.tableLength(); i++) {
                    sigChecksum.append(localVariableAttribute.variableName(i) + ",");
                }
            }
        }

        if (ctClass.getClassInitializer() != null) {
View Full Code Here

     
      CtMethod cm = cc.getDeclaredMethod(method.getName(), pool.get(paramTypeNames));

      MethodInfo methodInfo = cm.getMethodInfo()
      CodeAttribute codeAttribute = methodInfo.getCodeAttribute()
      LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag)
      if (attr == null)  {
          throw new Exception("class:"+cls.getName()+", have no LocalVariableTable, please use javac -g:{vars} to compile the source file");
      }
      String[] paramNames = new String[cm.getParameterTypes().length]
      int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1
      for (int i = 0; i < paramNames.length; i++) {
          paramNames[i] = attr.variableName(i + pos);
      }
      return paramNames;
    }
View Full Code Here

/*  546 */     ConstPool cp = this.methodInfo.getConstPool();
/*  547 */     CodeAttribute ca = this.methodInfo.getCodeAttribute();
/*  548 */     if (ca == null) {
/*  549 */       throw new CannotCompileException("no method body");
/*      */     }
/*  551 */     LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute("LocalVariableTable");
/*      */
/*  553 */     if (va == null) {
/*  554 */       va = new LocalVariableAttribute(cp);
/*  555 */       ca.getAttributes().add(va);
/*      */     }
/*      */
/*  558 */     int maxLocals = ca.getMaxLocals();
/*  559 */     String desc = Descriptor.of(type);
/*  560 */     va.addEntry(0, ca.getCodeLength(), cp.addUtf8Info(name), cp.addUtf8Info(desc), maxLocals);
/*      */
/*  562 */     ca.setMaxLocals(maxLocals + Descriptor.dataSize(desc));
/*      */   }
View Full Code Here

/*      */       }
/*      */       else {
/*  618 */         classInfo = this.methodInfo.getConstPool().addClassInfo(type);
/*      */       }
/*  620 */       ca.insertLocalVar(where, size);
/*  621 */       LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute("LocalVariableTable");
/*      */
/*  624 */       if (va != null) {
/*  625 */         va.shiftIndex(where, size);
/*      */       }
/*  627 */       StackMapTable smt = (StackMapTable)ca.getAttribute("StackMapTable");
/*  628 */       if (smt != null) {
/*  629 */         smt.insertLocal(where, StackMapTable.typeTagOf(typeDesc), classInfo);
/*      */       }
View Full Code Here

/*     */   }
/*     */
/*     */   public boolean recordLocalVariables(CodeAttribute ca, int pc)
/*     */     throws CompileError
/*     */   {
/* 272 */     LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute("LocalVariableTable");
/*     */
/* 275 */     if (va == null) {
/* 276 */       return false;
/*     */     }
/* 278 */     int n = va.tableLength();
/* 279 */     for (int i = 0; i < n; i++) {
/* 280 */       int start = va.startPc(i);
/* 281 */       int len = va.codeLength(i);
/* 282 */       if ((start <= pc) && (pc < start + len)) {
/* 283 */         this.gen.recordVariable(va.descriptor(i), va.variableName(i), va.index(i), this.stable);
/*     */       }
/*     */     }
/*     */
/* 287 */     return true;
/*     */   }
View Full Code Here

/*     */   }
/*     */
/*     */   public boolean recordParamNames(CodeAttribute ca, int numOfLocalVars)
/*     */     throws CompileError
/*     */   {
/* 302 */     LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute("LocalVariableTable");
/*     */
/* 305 */     if (va == null) {
/* 306 */       return false;
/*     */     }
/* 308 */     int n = va.tableLength();
/* 309 */     for (int i = 0; i < n; i++) {
/* 310 */       int index = va.index(i);
/* 311 */       if (index < numOfLocalVars) {
/* 312 */         this.gen.recordVariable(va.descriptor(i), va.variableName(i), index, this.stable);
/*     */       }
/*     */     }
/*     */
/* 316 */     return true;
/*     */   }
View Full Code Here

     *              LocalVariableAttribute.
     */
    public boolean recordLocalVariables(CodeAttribute ca, int pc)
        throws CompileError
    {
        LocalVariableAttribute va
            = (LocalVariableAttribute)
              ca.getAttribute(LocalVariableAttribute.tag);
        if (va == null)
            return false;

        int n = va.tableLength();
        for (int i = 0; i < n; ++i) {
            int start = va.startPc(i);
            int len = va.codeLength(i);
            if (start <= pc && pc < start + len)
                gen.recordVariable(va.descriptor(i), va.variableName(i),
                                   va.index(i), stable);
        }

        return true;
    }
View Full Code Here

     *              LocalVariableAttribute.
     */
    public boolean recordParamNames(CodeAttribute ca, int numOfLocalVars)
        throws CompileError
    {
        LocalVariableAttribute va
            = (LocalVariableAttribute)
              ca.getAttribute(LocalVariableAttribute.tag);
        if (va == null)
            return false;

        int n = va.tableLength();
        for (int i = 0; i < n; ++i) {
            int index = va.index(i);
            if (index < numOfLocalVars)
                gen.recordVariable(va.descriptor(i), va.variableName(i),
                                   index, stable);
        }

        return true;
    }
View Full Code Here

        final MetadataAdapter md = getMetadataAdapter();

        for (Object method : md.getMethods(cls)) {
            String key = md.getMethodFullKey(cls, method);
            if (acceptResult(key)) {
                LocalVariableAttribute table = (LocalVariableAttribute) ((MethodInfo) method).getCodeAttribute().getAttribute(LocalVariableAttribute.tag);
                int length = table.tableLength();
                int i = Modifier.isStatic(((MethodInfo) method).getAccessFlags()) ? 0 : 1; //skip this
                if (i < length) {
                    List<String> names = new ArrayList<String>(length - i);
                    while (i < length) names.add(((MethodInfo) method).getConstPool().getUtf8Info(table.nameIndex(i++)));
                    getStore().put(key, Joiner.on(", ").join(names));
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of javassist.bytecode.LocalVariableAttribute

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.