Examples of InstructionHandle


Examples of org.apache.bcel.generic.InstructionHandle

    MethodGen orig,
    InstructionList il,
    InstructionHandle returnInstructionHandle,
    ConstantPoolGen cp) {
      logger.info("Inserting Exit code.");
      InstructionHandle backup = il.insert(
      returnInstructionHandle,
        instFact.createGetStatic(
          clazz.getClassName(),
          loggerAttribute.getName(),
          loggerAttribute.getType()));
View Full Code Here

Examples of org.apache.bcel.generic.InstructionHandle

                }
            }

        }// while (!icq.isEmpty()) END
       
        InstructionHandle ih = start.getInstruction();
        do{
            if ((ih.getInstruction() instanceof ReturnInstruction) && (!(cfg.isDead(ih)))) {
                InstructionContext ic = cfg.contextOf(ih);
                Frame f = ic.getOutFrame(new ArrayList<InstructionContext>()); // TODO: This is buggy, we check only the top-level return instructions this way. Maybe some maniac returns from a method when in a subroutine?
                LocalVariables lvs = f.getLocals();
                for (int i=0; i<lvs.maxLocals(); i++){
                    if (lvs.get(i) instanceof UninitializedObjectType){
                        this.addMessage("Warning: ReturnInstruction '"+ic+"' may leave method with an uninitialized object in the local variables array '"+lvs+"'.");
                    }
                }
                OperandStack os = f.getStack();
                for (int i=0; i<os.size(); i++){
                    if (os.peek(i) instanceof UninitializedObjectType){
                        this.addMessage("Warning: ReturnInstruction '"+ic+"' may leave method with an uninitialized object on the operand stack '"+os+"'.");
                    }
                }
                //see JVM $4.8.2
                //TODO implement all based on stack
                Type returnedType = null;
                if( ih.getPrev().getInstruction() instanceof InvokeInstruction )
                {
                    returnedType = ((InvokeInstruction)ih.getPrev().getInstruction()).getType(m.getConstantPool());
                }
                if( ih.getPrev().getInstruction() instanceof LoadInstruction )
                {
                    int index = ((LoadInstruction)ih.getPrev().getInstruction()).getIndex();
                    returnedType = lvs.get(index);
                }
                if( ih.getPrev().getInstruction() instanceof GETFIELD )
                {
                    returnedType = ((GETFIELD)ih.getPrev().getInstruction()).getType(m.getConstantPool());
                }
                if( returnedType != null )
                {
                    if( returnedType instanceof ObjectType )
                    {
                        try
                        {
                            if( !((ObjectType)returnedType).isAssignmentCompatibleWith(m.getReturnType()) )
                            {
                                throw new StructuralCodeConstraintException("Returned type "+returnedType+" does not match Method's return type "+m.getReturnType());
                            }
                        }
                        catch (Exception e)
                        {
                            //dont know what do do now, so raise RuntimeException
                            throw new RuntimeException(e);
                        }
                    }
                    else if( !returnedType.equals(m.getReturnType()) )
                    {
                        throw new StructuralCodeConstraintException("Returned type "+returnedType+" does not match Method's return type "+m.getReturnType());
                    }
                }
            }
        }while ((ih = ih.getNext()) != null);
       
    }
View Full Code Here

Examples of org.apache.bcel.generic.InstructionHandle

                mg.removeLineNumbers();
            }
            mg.stripAttributes(skipDebug);
            InstructionList il = mg.getInstructionList();
            if (il != null) {
                InstructionHandle ih = il.getStart();
                while (ih != null) {
                    ih = ih.getNext();
                }
                if (compute) {
                    mg.setMaxStack();
                    mg.setMaxLocals();
                }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

  }

  private void deleteNewAndDup() {
    final ConstantPool cpg = getEnclosingClass().getConstantPool();
    int depth = 1;
    InstructionHandle ih = range.getStart();

    // Go back from where we are looking for 'NEW' that takes us to a stack depth of 0. INVOKESPECIAL <init>
    while (true) {
      Instruction inst = ih.getInstruction();
      if (inst.opcode == Constants.INVOKESPECIAL && ((InvokeInstruction) inst).getName(cpg).equals("<init>")) {
        depth++;
      } else if (inst.opcode == Constants.NEW) {
        depth--;
        if (depth == 0) {
          break;
        }
      } else if (inst.opcode == Constants.DUP_X2) {
        // This code seen in the wild (by Brad):
        // 40: new #12; //class java/lang/StringBuffer
        // STACK: STRINGBUFFER
        // 43: dup
        // STACK: STRINGBUFFER/STRINGBUFFER
        // 44: aload_0
        // STACK: STRINGBUFFER/STRINGBUFFER/THIS
        // 45: dup_x2
        // STACK: THIS/STRINGBUFFER/STRINGBUFFER/THIS
        // 46: getfield #36; //Field value:Ljava/lang/String;
        // STACK: THIS/STRINGBUFFER/STRINGBUFFER/STRING<value>
        // 49: invokestatic #37; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
        // STACK: THIS/STRINGBUFFER/STRINGBUFFER/STRING
        // 52: invokespecial #19; //Method java/lang/StringBuffer."<init>":(Ljava/lang/String;)V
        // STACK: THIS/STRINGBUFFER
        // 55: aload_1
        // STACK: THIS/STRINGBUFFER/LOCAL1
        // 56: invokevirtual #22; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
        // STACK: THIS/STRINGBUFFER
        // 59: invokevirtual #34; //Method java/lang/StringBuffer.toString:()Ljava/lang/String;
        // STACK: THIS/STRING
        // 62: putfield #36; //Field value:Ljava/lang/String;
        // STACK: <empty>
        // 65: return

        // if we attempt to match on the ctor call to StringBuffer.<init> then we get into trouble.
        // if we simply delete the new/dup pair without fixing up the dup_x2 then the dup_x2 will fail due to there
        // not being 3 elements on the stack for it to work with. The fix *in this situation* is to change it to
        // a simple 'dup'

        // this fix is *not* very clean - but a general purpose decent solution will take much longer and this
        // bytecode sequence has only been seen once in the wild.
        ih.setInstruction(InstructionConstants.DUP);
      }
      ih = ih.getPrev();
    }
    // now IH points to the NEW. We're followed by the DUP, and that is followed
    // by the actual instruction we care about.
    InstructionHandle newHandle = ih;
    InstructionHandle endHandle = newHandle.getNext();
    InstructionHandle nextHandle;
    if (endHandle.getInstruction().opcode == Constants.DUP) {
      nextHandle = endHandle.getNext();
      retargetFrom(newHandle, nextHandle);
      retargetFrom(endHandle, nextHandle);
    } else if (endHandle.getInstruction().opcode == Constants.DUP_X1) {
      InstructionHandle dupHandle = endHandle;
      endHandle = endHandle.getNext();
      nextHandle = endHandle.getNext();
      boolean skipEndRepositioning = false;
      if (endHandle.getInstruction().opcode == Constants.SWAP) {
      } else if (endHandle.getInstruction().opcode == Constants.IMPDEP1) {
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

        il.append(fact.createInvoke("java/lang/reflect/Method", "getAnnotation", jlaAnnotation, new Type[] { jlClass },
            Constants.INVOKEVIRTUAL));
        il.append(InstructionConstants.DUP);
        il.append(fact
            .createPutStatic(shadow.getEnclosingClass().getName(), annotationCachingField.getName(), jlAnnotation));
        InstructionHandle ifNullElse = il.append(InstructionConstants.NOP);
        ifNonNull.setTarget(ifNullElse);

      } else { // init/preinit/ctor-call/ctor-exec
        il.append(fact.createConstant(BcelWorld.makeBcelType(containingType)));
        buildArray(il, fact, jlClass, paramTypes, 1);
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

   * @param enclosingMethod where to find ih's instruction list.
   */
  public static void replaceInstruction(InstructionHandle ih, InstructionList replacementInstructions,
      LazyMethodGen enclosingMethod) {
    InstructionList il = enclosingMethod.getBody();
    InstructionHandle fresh = il.append(ih, replacementInstructions);
    deleteInstruction(ih, fresh, enclosingMethod);
  }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

   * Prepare the around advice, flag it as cannot be inlined if it can't be
   *
   * @param aroundAdvice
   */
  private void openAroundAdvice(LazyMethodGen aroundAdvice) {
    InstructionHandle curr = aroundAdvice.getBody().getStart();
    InstructionHandle end = aroundAdvice.getBody().getEnd();
    ConstantPool cpg = aroundAdvice.getEnclosingClass().getConstantPool();
    InstructionFactory factory = aroundAdvice.getEnclosingClass().getFactory();

    boolean realizedCannotInline = false;
    while (curr != end) {
      if (realizedCannotInline) {
        // we know we cannot inline this advice so no need for futher handling
        break;
      }
      InstructionHandle next = curr.getNext();
      Instruction inst = curr.getInstruction();

      // open-up method call
      if ((inst instanceof InvokeInstruction)) {
        InvokeInstruction invoke = (InvokeInstruction) inst;
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

      }
    }
    il.append(getAdviceArgSetup(shadow, extraArgVar, null));
    il.append(getNonTestAdviceInstructions(shadow));

    InstructionHandle ifYesAdvice = il.getStart();
    il.insert(getTestInstructions(shadow, ifYesAdvice, ifNoAdvice, ifYesAdvice));

    // If inserting instructions at the start of a method, we need a nice line number for this entry
    // in the stack trace
    if (shadow.getKind() == Shadow.MethodExecution && getKind() == AdviceKind.Before) {
      int lineNumber = 0;
      // Uncomment this code if you think we should use the method decl line number when it exists...
      // // If the advised join point is in a class built by AspectJ, we can use the declaration line number
      // boolean b = shadow.getEnclosingMethod().getMemberView().hasDeclarationLineNumberInfo();
      // if (b) {
      // lineNumber = shadow.getEnclosingMethod().getMemberView().getDeclarationLineNumber();
      // } else { // If it wasn't, the best we can do is the line number of the first instruction in the method
      lineNumber = shadow.getEnclosingMethod().getMemberView().getLineNumberOfFirstInstruction();
      // }
      InstructionHandle start = il.getStart();
      if (lineNumber > 0) {
        start.addTargeter(new LineNumberTag(lineNumber));
      }
      // Fix up the local variables: find any that have a startPC of 0 and ensure they target the new start of the method
      LocalVariableTable lvt = shadow.getEnclosingMethod().getMemberView().getMethod().getLocalVariableTable();
      if (lvt != null) {
        LocalVariable[] lvTable = lvt.getLocalVariableTable();
        for (int i = 0; i < lvTable.length; i++) {
          LocalVariable lv = lvTable[i];
          if (lv.getStartPC() == 0) {
            start.addTargeter(new LocalVariableTag(lv.getSignature(), lv.getName(), lv.getIndex(), 0));
          }
        }
      }
    }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    // Look for ctors to modify
    for (LazyMethodGen aMethod : mgs) {
      if (LazyMethodGen.isConstructor(aMethod)) {
        InstructionList insList = aMethod.getBody();
        InstructionHandle handle = insList.getStart();
        while (handle != null) {
          if (handle.getInstruction().opcode == Constants.INVOKESPECIAL) {
            ConstantPool cpg = newParentTarget.getConstantPool();
            InvokeInstruction invokeSpecial = (InvokeInstruction) handle.getInstruction();
            if (invokeSpecial.getClassName(cpg).equals(currentParent)
                && invokeSpecial.getMethodName(cpg).equals("<init>")) {
              // System.err.println("Transforming super call '<init>" + invokeSpecial.getSignature(cpg) + "'");

              // 1. Check there is a ctor in the new parent with
              // the same signature
              ResolvedMember newCtor = getConstructorWithSignature(newParent, invokeSpecial.getSignature(cpg));

              if (newCtor == null) {

                // 2. Check ITDCs to see if the necessary ctor is provided that way
                boolean satisfiedByITDC = false;
                for (Iterator<ConcreteTypeMunger> ii = newParentTarget.getType()
                    .getInterTypeMungersIncludingSupers().iterator(); ii.hasNext() && !satisfiedByITDC;) {
                  ConcreteTypeMunger m = ii.next();
                  if (m.getMunger() instanceof NewConstructorTypeMunger) {
                    if (m.getSignature().getSignature().equals(invokeSpecial.getSignature(cpg))) {
                      satisfiedByITDC = true;
                    }
                  }
                }

                if (!satisfiedByITDC) {
                  String csig = createReadableCtorSig(newParent, cpg, invokeSpecial);
                  weaver.getWorld()
                      .getMessageHandler()
                      .handleMessage(
                          MessageUtil.error(
                              "Unable to modify hierarchy for " + newParentTarget.getClassName()
                                  + " - the constructor " + csig + " is missing",
                              this.getSourceLocation()));
                  return false;
                }
              }

              int idx = cpg.addMethodref(newParent.getName(), invokeSpecial.getMethodName(cpg),
                  invokeSpecial.getSignature(cpg));
              invokeSpecial.setIndex(idx);
            }
          }
          handle = handle.getNext();
        }
      }
    }
    return true;
  }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

        body.append(fact.createInvoke(munger.getImplClassName(), "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
        body.append(Utility.createSet(fact, munger.getDelegate(weaver.getLazyClassGen().getType())));
      }

      // if not null use the instance we've got
      InstructionHandle ifNonNullElse = body.append(InstructionConstants.ALOAD_0);
      ifNonNull.setTarget(ifNonNullElse);
      body.append(Utility.createGet(fact, munger.getDelegate(weaver.getLazyClassGen().getType())));

      // args
      int pos = 0;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.