@Override
 public void visitElement(@NotNull PsiElement element) {
   if (read || written) {
     return;
   }
   super.visitElement(element);
 }
 @Override
 public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
   super.visitReferenceElement(reference);
   if (referenceFound) {
     return;
   }
   final String text = reference.getText();
   if (text.indexOf((int) '.') >= 0 || !name.equals(text)) {
     return;
   }
   final PsiElement element = reference.resolve();
   if (!(element instanceof PsiClass) || element instanceof PsiTypeParameter) {
     return;
   }
   final PsiClass aClass = (PsiClass) element;
   final String testClassName = aClass.getName();
   final String testClassQualifiedName = aClass.getQualifiedName();
   if (testClassQualifiedName == null
       || testClassName == null
       || testClassQualifiedName.equals(fullyQualifiedName)
       || !testClassName.equals(name)) {
     return;
   }
   referenceFound = true;
 }
 @Override
 public void visitElement(PsiElement element) {
   if (used) {
     return;
   }
   super.visitElement(element);
 }
 @Override
 public void visitArrayAccessExpression(final PsiArrayAccessExpression expression) {
   if (myCollectExpressions
       && !DebuggerUtils.hasSideEffectsOrReferencesMissingVars(expression, myVisibleLocals)) {
     myExpressions.add(new TextWithImportsImpl(expression));
   }
   super.visitArrayAccessExpression(expression);
 }
 @Override
 public void visitJavaFile(PsiJavaFile file) {
   super.visitJavaFile(file);
   final PsiPackage[] packages = ClassUtils.calculatePackagesRecursive(file);
   for (PsiPackage aPackage : packages) {
     numTestMethodsPerPackage.createBucket(aPackage);
   }
 }
 public void visitBreakStatement(PsiBreakStatement statement) {
   super.visitBreakStatement(statement);
   if (statement.getLabelIdentifier() != null) {
     elementCount++;
   } else if (!(statement.findExitedStatement() instanceof PsiSwitchStatement)) {
     elementCount++;
   }
 }
 @Override
 public void visitMethodCallExpression(final PsiMethodCallExpression expression) {
   if (myCollectExpressions) {
     final PsiMethod psiMethod = expression.resolveMethod();
     if (psiMethod != null
         && !DebuggerUtils.hasSideEffectsOrReferencesMissingVars(expression, myVisibleLocals)) {
       myExpressions.add(new TextWithImportsImpl(expression));
     }
   }
   super.visitMethodCallExpression(expression);
 }
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   super.visitReferenceExpression(expression);
   final PsiElement referent = expression.resolve();
   if (!(referent instanceof PsiParameter)) {
     return;
   }
   final PsiParameter parameter = (PsiParameter) referent;
   if (paramsToMerge.contains(parameter)) {
     parameterUsages.add(expression);
   }
 }
 @Override
 public void visitMethod(PsiMethod method) {
   super.visitMethod(method);
   final PsiClass aClass = method.getContainingClass();
   if (TestUtils.isJUnitTestMethod(method)) {
     final PsiPackage[] packages = ClassUtils.calculatePackagesRecursive(aClass);
     for (PsiPackage aPackage : packages) {
       numTestMethodsPerPackage.incrementBucketValue(aPackage, 1);
     }
   }
 }
 /** check if variable is used in nested/inner class. */
 @Override
 public void visitClass(PsiClass aClass) {
   if (read || written) {
     return;
   }
   super.visitClass(aClass);
   final VariableUsedVisitor visitor = new VariableUsedVisitor(variable);
   aClass.accept(visitor);
   read = visitor.isUsed();
   reference = visitor.getReference();
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression reference) {
   if (used) {
     return;
   }
   super.visitReferenceExpression(reference);
   final PsiElement element = reference.resolve();
   if (parameter.equals(element)) {
     used = true;
   }
 }
 public void visitMethod(PsiMethod method) {
   if (methodNestingDepth == 0) {
     elementCount = 0;
   }
   methodNestingDepth++;
   super.visitMethod(method);
   methodNestingDepth--;
   if (methodNestingDepth == 0 && !MethodUtils.isAbstract(method)) {
     postMetric(method, elementCount);
   }
 }
Пример #13
0
 @Override
 public void visitThisExpression(PsiThisExpression expression) {
   super.visitThisExpression(expression);
   final PsiJavaCodeReferenceElement qualifier = expression.getQualifier();
   if (qualifier != null && qualifier.isReferenceTo(mySourceClass)) {
     try {
       qualifier.bindToElement(myTargetSuperClass);
     } catch (IncorrectOperationException e) {
       LOG.error(e);
     }
   }
 }
 @Override
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   if (argumentsContainCatchParameter || !visited.add(expression)) {
     return;
   }
   super.visitReferenceExpression(expression);
   final PsiElement target = expression.resolve();
   if (!parameter.equals(target)) {
     if (target instanceof PsiLocalVariable) {
       final PsiLocalVariable variable = (PsiLocalVariable) target;
       final Query<PsiReference> query =
           ReferencesSearch.search(variable, variable.getUseScope(), false);
       query.forEach(
           reference -> {
             final PsiElement element = reference.getElement();
             final PsiElement parent =
                 PsiTreeUtil.skipParentsOfType(element, PsiParenthesizedExpression.class);
             if (!(parent instanceof PsiReferenceExpression)) {
               return true;
             }
             final PsiElement grandParent = parent.getParent();
             if (!(grandParent instanceof PsiMethodCallExpression)) {
               return true;
             }
             final PsiMethodCallExpression methodCallExpression =
                 (PsiMethodCallExpression) grandParent;
             final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
             final PsiExpression[] arguments = argumentList.getExpressions();
             for (PsiExpression argument : arguments) {
               argument.accept(ReferenceFinder.this);
             }
             return true;
           });
       final PsiExpression initializer = variable.getInitializer();
       if (initializer != null) {
         initializer.accept(this);
       }
     }
     return;
   }
   if (ignoreGetMessage) {
     argumentsContainCatchParameter = true;
   } else {
     final PsiElement parent = expression.getParent();
     if (parent instanceof PsiReferenceExpression) {
       final PsiElement grandParent = parent.getParent();
       if (grandParent instanceof PsiMethodCallExpression) {
         return;
       }
     }
     argumentsContainCatchParameter = true;
   }
 }
 @Override
 public void visitReferenceExpression(final PsiReferenceExpression reference) {
   if (myLineRange.intersects(reference.getTextRange())) {
     final PsiElement psiElement = reference.resolve();
     if (psiElement instanceof PsiVariable) {
       final PsiVariable var = (PsiVariable) psiElement;
       if (var instanceof PsiField) {
         if (myCollectExpressions
             && !DebuggerUtils.hasSideEffectsOrReferencesMissingVars(
                 reference, myVisibleLocals)) {
           /*
           if (var instanceof PsiEnumConstant && reference.getQualifier() == null) {
             final PsiClass enumClass = ((PsiEnumConstant)var).getContainingClass();
             if (enumClass != null) {
               final PsiExpression expression = JavaPsiFacade.getInstance(var.getProject()).getParserFacade().createExpressionFromText(enumClass.getName() + "." + var.getName(), var);
               final PsiReference ref = expression.getReference();
               if (ref != null) {
                 ref.bindToElement(var);
                 myExpressions.add(new TextWithImportsImpl(expression));
               }
             }
           }
           else {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
           */
           final PsiModifierList modifierList = var.getModifierList();
           boolean isConstant =
               (var instanceof PsiEnumConstant)
                   || (modifierList != null
                       && modifierList.hasModifierProperty(PsiModifier.STATIC)
                       && modifierList.hasModifierProperty(PsiModifier.FINAL));
           if (!isConstant) {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
         }
       } else {
         if (myVisibleLocals.contains(var.getName())) {
           myVars.add(var.getName());
         } else {
           // fix for variables used in inner classes
           if (!Comparing.equal(
               PsiTreeUtil.getParentOfType(reference, PsiClass.class),
               PsiTreeUtil.getParentOfType(var, PsiClass.class))) {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
         }
       }
     }
   }
   super.visitReferenceExpression(reference);
 }
Пример #16
0
 @Override
 public void visitSuperExpression(PsiSuperExpression expression) {
   super.visitSuperExpression(expression);
   final PsiJavaCodeReferenceElement qualifier = expression.getQualifier();
   if (qualifier != null && qualifier.isReferenceTo(mySourceClass)) {
     try {
       expression.replace(
           JavaPsiFacade.getElementFactory(myProject)
               .createExpressionFromText(myTargetSuperClass.getName() + ".this", null));
     } catch (IncorrectOperationException e) {
       LOG.error(e);
     }
   }
 }
    public void visitClass(PsiClass aClass) {
      super.visitClass(aClass);
      int prevCommentLines = 0;
      if (!ClassUtils.isAnonymous(aClass)) {
        prevCommentLines = commentLines;
        commentLines = 0;
      }
      super.visitClass(aClass);

      if (!ClassUtils.isAnonymous(aClass)) {
        if (aClass.isInterface()) {
          commentLines = prevCommentLines;
        } else {
          int linesOfCode = LineUtil.countLines(aClass);
          final PsiClass[] innerClasses = aClass.getInnerClasses();
          for (PsiClass innerClass : innerClasses) {
            linesOfCode -= LineUtil.countLines(innerClass);
          }
          postMetric(aClass, commentLines, linesOfCode - commentLines);
          commentLines = prevCommentLines;
        }
      }
    }
 @Override
 public void visitReturnStatement(@NotNull PsiReturnStatement returnStatement) {
   if (read || written) {
     return;
   }
   super.visitReturnStatement(returnStatement);
   final PsiExpression returnValue = returnStatement.getReturnValue();
   if (returnValue == null) {
     return;
   }
   final VariableUsedVisitor visitor = new VariableUsedVisitor(variable);
   returnValue.accept(visitor);
   read = visitor.isUsed();
   reference = visitor.getReference();
 }
Пример #19
0
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) {
   if (containsAssertion) {
     return;
   }
   super.visitMethodCallExpression(call);
   final PsiReferenceExpression methodExpression = call.getMethodExpression();
   @NonNls final String methodName = methodExpression.getReferenceName();
   if (methodName == null) {
     return;
   }
   if (methodName.startsWith("assert") || methodName.startsWith("fail")) {
     containsAssertion = true;
   }
 }
 @Override
 public void visitVariable(@NotNull PsiVariable variable) {
   if (read || written) {
     return;
   }
   super.visitVariable(variable);
   final PsiExpression initalizer = variable.getInitializer();
   if (initalizer == null) {
     return;
   }
   final VariableUsedVisitor visitor = new VariableUsedVisitor(variable);
   initalizer.accept(visitor);
   read = visitor.isUsed();
   reference = visitor.getReference();
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
   if (used) {
     return;
   }
   super.visitReferenceExpression(expression);
   final PsiElement referent = expression.resolve();
   if (referent == null) {
     return;
   }
   if (referent.equals(variable)) {
     reference = expression;
     used = true;
   }
 }
Пример #22
0
 @Override
 public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
   if (referenceFound) {
     return;
   }
   super.visitReferenceElement(reference);
   if (reference.isQualified()) {
     return;
   }
   final PsiElement target = reference.resolve();
   for (PsiMember member : members) {
     if (member.equals(target)) {
       referenceFound = true;
       return;
     }
   }
 }
    public void visitClass(PsiClass aClass) {
      super.visitClass(aClass);
      if (isConcreteClass(aClass)) {
        final PsiField[] allFields = aClass.getAllFields();
        int numInheritedFields = 0;
        for (final PsiField field : allFields) {
          final PsiClass containingClass = field.getContainingClass();
          if (containingClass != null
              && !containingClass.equals(aClass)
              && !field.hasModifierProperty(PsiModifier.STATIC)) {
            numInheritedFields++;
          }
        }

        postMetric(aClass, numInheritedFields);
      }
    }
 @Override
 public void visitArrayInitializerExpression(PsiArrayInitializerExpression expression) {
   if (read || written) {
     return;
   }
   super.visitArrayInitializerExpression(expression);
   final PsiExpression[] arguments = expression.getInitializers();
   for (final PsiExpression argument : arguments) {
     final VariableUsedVisitor visitor = new VariableUsedVisitor(variable);
     argument.accept(visitor);
     if (visitor.isUsed()) {
       read = true;
       reference = visitor.getReference();
       return;
     }
   }
 }
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) {
   if (read || written) {
     return;
   }
   super.visitMethodCallExpression(call);
   final PsiExpressionList argumentList = call.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   for (final PsiExpression argument : arguments) {
     final VariableUsedVisitor visitor = new VariableUsedVisitor(variable);
     argument.accept(visitor);
     if (visitor.isUsed()) {
       read = true;
       reference = visitor.getReference();
       return;
     }
   }
 }
    public void visitMethodCallExpression(PsiMethodCallExpression psiMethodCallExpression) {
      PsiReference reference = psiMethodCallExpression.getMethodExpression().getReference();
      if (reference == null) {
        return;
      }
      PsiElement e = reference.resolve();
      if (e instanceof PsiMethod) {
        assert myBaseMethod != null;
        PsiMethod m = (PsiMethod) e;
        if (m.getContainingClass() == myBaseMethod.getContainingClass()) {
          myInfo.registerDependency(myBaseMethod, m);
        }
      }

      // We process all method call expression children because there is a possible case like below:
      //   new Runnable() {
      //     void test();
      //   }.run();
      // Here we want to process that 'Runnable.run()' implementation.
      super.visitMethodCallExpression(psiMethodCallExpression);
    }
 @Override
 public void visitPostfixExpression(@NotNull PsiPostfixExpression postfixExpression) {
   if (read || written) {
     return;
   }
   super.visitPostfixExpression(postfixExpression);
   final IElementType tokenType = postfixExpression.getOperationTokenType();
   if (!tokenType.equals(JavaTokenType.PLUSPLUS)
       && !tokenType.equals(JavaTokenType.MINUSMINUS)) {
     return;
   }
   final PsiExpression operand = postfixExpression.getOperand();
   if (!(operand instanceof PsiReferenceExpression)) {
     return;
   }
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) operand;
   final PsiElement target = referenceExpression.resolve();
   if (!variable.equals(target)) {
     return;
   }
   written = true;
 }
 @Override
 public void visitAssignmentExpression(@NotNull PsiAssignmentExpression assignment) {
   if (read || written) {
     return;
   }
   super.visitAssignmentExpression(assignment);
   final PsiExpression lhs = assignment.getLExpression();
   if (lhs instanceof PsiReferenceExpression) {
     PsiReferenceExpression referenceExpression = (PsiReferenceExpression) lhs;
     final PsiElement target = referenceExpression.resolve();
     if (variable.equals(target)) {
       written = true;
       return;
     }
   }
   final PsiExpression rhs = assignment.getRExpression();
   if (rhs == null) {
     return;
   }
   final VariableUsedVisitor visitor = new VariableUsedVisitor(variable);
   rhs.accept(visitor);
   read = visitor.isUsed();
   reference = visitor.getReference();
 }
 public void visitAssertStatement(PsiAssertStatement statement) {
   super.visitAssertStatement(statement);
   elementCount++;
 }
Пример #30
0
 @Override
 public void visitElement(PsiElement element) {
   if (referenceFound) return;
   super.visitElement(element);
 }