@Override
 public void endVisit(MethodInvocation methodInvocation) {
   IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
   if (methodBinding != null) {
     invocationHandler(methodBinding, methodInvocation);
   }
 }
예제 #2
0
  /* METHOD INVOCATION PART */
  @Override
  public boolean visit(MethodInvocation node) {
    IMethodBinding binding = node.resolveMethodBinding();
    List args = node.arguments();
    handleMethodCall(node, binding, args);

    return super.visit(node);
  }
  /**
   * Find all method invocations from the called method. Since we only traverse into the AST on the
   * wanted method declaration, this method should not hit on more method invocations than those in
   * the wanted method.
   *
   * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodInvocation)
   */
  public boolean visit(MethodInvocation node) {
    progressMonitorWorked(1);
    if (!isFurtherTraversalNecessary(node)) {
      return false;
    }

    if (isNodeWithinMethod(node)) {
      addMethodCall(node.resolveMethodBinding(), node);
    }

    return true;
  }
예제 #4
0
 @Override
 public void endVisit(MethodInvocation node) {
   // TODO need more complex cases
   Stack<VMExpression> arguments = new Stack<VMExpression>();
   for (int i = 0; i < node.arguments().size(); i++) {
     arguments.push((VMExpression) expressions.pop());
   }
   VMExpression role = null;
   if (!Modifier.isStatic(node.resolveMethodBinding().getModifiers())) {
     role = (node.getExpression() == null) ? null : (VMExpression) (expressions.pop());
   }
   VMMethodInvocation methodInvo = new VMMethodInvocation(node, node.getName().toString(), role);
   while (!arguments.empty()) {
     methodInvo.getArguments().add(arguments.pop());
   }
   expressions.push(methodInvo);
 }
예제 #5
0
  private static boolean replaceEnsureClassInitialized(ImplWriter w, MethodInvocation node) {
    if (!node.getName().getIdentifier().equals("ensureClassInitialized")) {
      return false;
    }

    if (node.arguments().size() != 1 || !(node.arguments().get(0) instanceof TypeLiteral)) {
      return false;
    }

    IMethodBinding mb = node.resolveMethodBinding();
    if (!mb.getDeclaringClass().getQualifiedName().equals("sun.misc.Unsafe")) {
      return false;
    }

    TypeLiteral tl = (TypeLiteral) node.arguments().get(0);
    ITypeBinding tb = tl.getType().resolveBinding();
    w.hardDep(tb);
    w.print(CName.relative(tb, w.type, true) + "::clinit()");

    return true;
  }
  /**
   * Decide whether or not the given invocation resolves to a MongoDB collection.
   *
   * @param invocation
   * @return
   */
  private boolean isGetCollection(final MethodInvocation invocation) {
    boolean returnValue;
    String currentVariable;
    String invocationString;

    returnValue = false;
    if (invocation
        .resolveMethodBinding()
        .getDeclaringClass()
        .getQualifiedName()
        .equals("com.mongodb.DB")) {
      if (invocation.getName().toString().equals("getCollection")) {
        invocationString = invocation.getParent().toString();
        currentVariable = invocationString.substring(0, invocationString.indexOf('='));
        if (this.variableName.equals(currentVariable)) {
          returnValue = true;
        }
      }
    }

    return returnValue;
  }
예제 #7
0
    @Override
    public boolean visit(MethodInvocation node) {
      IMethodBinding methodBinding = node.resolveMethodBinding();
      ITypeBinding declaringClass = methodBinding.getDeclaringClass();
      IMethodBinding methodDeclaration = methodBinding.getMethodDeclaration();

      String denyToSrcLocation = getSourceLocation(methodBinding, declaringClass);

      if (denyToSrc != null
          && denyToSrcLocation != null
          && !denyToSrc.matcher(denyToSrcLocation).find()) {
        return true;
      }

      String declaringClassName = declaringClass.getName();
      String declaringClassPackage =
          declaringClass.getPackage() == null ? "" : declaringClass.getPackage().getName() + ".";
      String methodDeclarationName = methodDeclaration.getName();

      String methodCall = declaringClassPackage + declaringClassName + "#" + methodDeclarationName;

      if (denyTo.matcher(methodCall).matches()) {
        String txt =
            createWarningText(
                this.acu.getFullyQualifiedClassName(),
                methodCall,
                this.acu.getSourceLocation(),
                denyToSrcLocation);
        this.acu.addMarker(
            "Method dependency " + txt,
            this.acu.getCompilationUnit().getLineNumber(node.getStartPosition()),
            IMarker.SEVERITY_WARNING);
      }

      return true;
    }
예제 #8
0
 public static boolean isActionCall(MethodInvocation methodInvocation) {
   IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
   ITypeBinding declaringClass = methodBinding.getDeclaringClass();
   return typeIsAssignableFrom(declaringClass, hu.elte.txtuml.api.model.Action.class);
 }
 @Override
 public boolean visit(MethodInvocation node) {
   String methodKey = castHelper.methodKey(node);
   String methodName = node.getName().toString();
   int lineNumber = compilationUnit.getLineNumber(node.getStartPosition());
   // 函数调用在线程调用到的函数中(单个函数可能牵扯到多个线程)
   if (threadMethodMapTable.containsKey(methodKey) && node.resolveMethodBinding() != null) {
     IMethodBinding iMethodBinding = node.resolveMethodBinding();
     // 1.中断判断函数(中断接受节点)
     if ((methodName.equals("interrupted") || methodName.equals("isInterrupted"))) {
       if (iMethodBinding.getDeclaringClass() != null
           && iMethodBinding.getDeclaringClass().getBinaryName().equals("java.lang.Thread")) {
         System.out.println(node);
         System.out.println("interrupted judge");
         HashSet<String> threads = threadMethodMapTable.get(methodKey);
         // (1)添加到每个线程的中断接受节点集合
         for (String thread : threads) {
           ThreadInformation threadInformation = threadInfo.get(thread);
           Node interruptedNode = new Node(filePath, lineNumber);
           threadInformation.getInterruptNodes().add(interruptedNode);
         }
         // (2)变量自身判断(根据自己所绑定的线程类型而添加中断接受节点)
         interruptAcceptNodeHandle(node);
       }
     }
     // 2.引发中断的函数调用(中断通知节点)
     else if (methodName.equals("interrupt")) {
       ThreadInterruptNode threadInterruptNode = new ThreadInterruptNode(filePath, lineNumber);
       // (1)自中断
       if (node.getExpression() instanceof MethodInvocation
           && ((MethodInvocation) node.getExpression())
               .getName()
               .getIdentifier()
               .equals("currentThread")) {
         // 对有可能调用该自中断的线程添加到中断通知节点中
         HashSet<String> threads = threadMethodMapTable.get(methodKey);
         for (String thread : threads) {
           threadInterruptNode.getThreadKeyList().add(thread);
         }
         threadInterruptNodes.add(threadInterruptNode);
       }
       // (2)调用中断,通过变量来定位threadKey
       else if (iMethodBinding.getDeclaringClass() != null
           && iMethodBinding.getDeclaringClass().getBinaryName().equals("java.lang.Thread")) {
         interruptNotifyNodeHandle(node, threadInterruptNode);
       }
     }
     // 3.会抛出中断异常的函数(中断接受节点)
     else {
       ITypeBinding[] typeBindings = node.resolveMethodBinding().getExceptionTypes();
       for (ITypeBinding iTypeBinding : typeBindings) {
         if (iTypeBinding.getBinaryName().equals("java.lang.InterruptedException")) {
           System.out.println(node);
           System.out.println("interrupted exception");
           HashSet<String> threads = threadMethodMapTable.get(methodKey);
           // (1)添加到每个线程的中断接受节点集合
           for (String thread : threads) {
             ThreadInformation threadInformation = threadInfo.get(thread);
             Node interruptedNode = new Node(filePath, lineNumber);
             threadInformation.getInterruptNodes().add(interruptedNode);
           }
           // (2)变量自身判断(根据自己所绑定的线程类型而添加中断接受节点)
           interruptAcceptNodeHandle(node);
         }
       }
     }
   }
   return super.visit(node);
 }
  private void collectSideEffects(Expression expr, List<Expression> sideEffectExprs) {
    // local variable, parameter, enum constant, etc.
    // OR method starting with is*(), get*()
    // except atomic long, atomic integer, etc.
    switch (expr.getNodeType()) {
      case METHOD_INVOCATION:
        MethodInvocation mi = (MethodInvocation) expr;
        methodHasSideEffects(mi.resolveMethodBinding(), mi, sideEffectExprs);
        collectSideEffects(mi.getExpression(), sideEffectExprs);
        collectSideEffects(arguments(mi), sideEffectExprs);
        break;

      case SUPER_METHOD_INVOCATION:
        SuperMethodInvocation smi = (SuperMethodInvocation) expr;
        methodHasSideEffects(smi.resolveMethodBinding(), smi, sideEffectExprs);
        collectSideEffects(arguments(smi), sideEffectExprs);
        break;

      case CLASS_INSTANCE_CREATION:
        ClassInstanceCreation cic = (ClassInstanceCreation) expr;
        methodHasSideEffects(cic.resolveConstructorBinding(), cic, sideEffectExprs);
        collectSideEffects(cic.getExpression(), sideEffectExprs);
        collectSideEffects(arguments(cic), sideEffectExprs);
        break;

      case ARRAY_ACCESS:
        ArrayAccess aa = (ArrayAccess) expr;
        collectSideEffects(aa.getArray(), sideEffectExprs);
        collectSideEffects(aa.getIndex(), sideEffectExprs);
        break;
      case ARRAY_CREATION:
        ArrayCreation ac = (ArrayCreation) expr;
        collectSideEffects(ac.getInitializer(), sideEffectExprs);
        collectSideEffects(ac.dimensions(), sideEffectExprs);
        break;
      case ARRAY_INITIALIZER:
        ArrayInitializer ai = (ArrayInitializer) expr;
        collectSideEffects(expressions(ai), sideEffectExprs);
        break;
      case ASSIGNMENT:
        Assignment as = (Assignment) expr;
        collectSideEffects(as.getLeftHandSide(), sideEffectExprs);
        collectSideEffects(as.getRightHandSide(), sideEffectExprs);
        break;

      case CONDITIONAL_EXPRESSION:
        ConditionalExpression ce = (ConditionalExpression) expr;
        collectSideEffects(ce.getExpression(), sideEffectExprs);
        collectSideEffects(ce.getThenExpression(), sideEffectExprs);
        collectSideEffects(ce.getElseExpression(), sideEffectExprs);
        break;

      case FIELD_ACCESS:
        FieldAccess fa = (FieldAccess) expr;
        collectSideEffects(fa.getExpression(), sideEffectExprs);
        collectSideEffects(fa.getName(), sideEffectExprs);
        break;
      case SUPER_FIELD_ACCESS:
        SuperFieldAccess sfa = (SuperFieldAccess) expr;
        collectSideEffects(sfa.getQualifier(), sideEffectExprs);
        collectSideEffects(sfa.getName(), sideEffectExprs);
        break;
      case THIS_EXPRESSION:
        collectSideEffects(((ThisExpression) expr).getQualifier(), sideEffectExprs);
        break;
      case VARIABLE_DECLARATION_EXPRESSION:
        collectSideEffects((VariableDeclarationExpression) expr, sideEffectExprs);
        break;

      case INFIX_EXPRESSION:
        InfixExpression ie = (InfixExpression) expr;
        collectSideEffects(ie.getLeftOperand(), sideEffectExprs);
        collectSideEffects(ie.getRightOperand(), sideEffectExprs);
        collectSideEffects(extendedOperands(ie), sideEffectExprs);
        break;

      case CAST_EXPRESSION:
        collectSideEffects(((CastExpression) expr).getExpression(), sideEffectExprs);
        break;
      case INSTANCEOF_EXPRESSION:
        collectSideEffects(((InstanceofExpression) expr).getLeftOperand(), sideEffectExprs);
        break;
      case PARENTHESIZED_EXPRESSION:
        collectSideEffects(((ParenthesizedExpression) expr).getExpression(), sideEffectExprs);
        break;
      case POSTFIX_EXPRESSION:
        sideEffectExprs.add(expr);
        break;
      case PREFIX_EXPRESSION:
        PrefixExpression pe = (PrefixExpression) expr;
        PrefixExpression.Operator op = pe.getOperator();
        if (PrefixExpression.Operator.INCREMENT.equals(op)
            | PrefixExpression.Operator.DECREMENT.equals(op)) {
          sideEffectExprs.add(pe);
        } else {
          collectSideEffects(pe.getOperand(), sideEffectExprs);
        }
        break;

      default:
        // literals
        // names
    }
  }