@Override public void visitMethod(@NonNull JavaContext context, @Nullable JavaElementVisitor visitor, @NonNull PsiMethodCallExpression node, @NonNull PsiMethod method) { boolean isLayoutInflaterCall = isLayoutInflaterCall(context, node, method); boolean isViewInflateCall = isInViewCall(context, node, method); String name = method.getName(); boolean fromMethod = LAYOUTINFLATER_FROM.equals(name); boolean viewInflateMethod = VIEW_INFLATE.equals(name); if (isLayoutInflaterCall && fromMethod) { context.report(ISSUE_LAYOUTINFLATER, node, context.getLocation(node), "error use system LayoutInflater,should use LayoutInflaterWrapper."); return; } if (viewInflateMethod && isViewInflateCall) { context.report(ISSUE_VIEW_INFLATE, node, context.getLocation(node), "error use View.inflate(),should use LayoutInflaterWrapper.inflate."); return; } }
@Override public void visitMethod(JavaContext context, JavaElementVisitor visitor, PsiMethodCallExpression call, PsiMethod method) { JavaEvaluator evaluator = context.getEvaluator(); if (evaluator.getParameterCount(method) == 0) { PsiMethod parent = PsiTreeUtil.getParentOfType(call, PsiMethod.class); if (parent != null) { if (evaluator.isMemberInClass(method, CLASS_DIRECTOR) && evaluator.isMemberInSubClassOf(parent, CLASS_DIRECTOR_BACK_HANDLER, false)) { context.report(ISSUE, call, context.getLocation(call), "Can't call Stage.handleBack() in Stage.BackHandler.handleBack()"); } else if (evaluator.isMemberInClass(method, CLASS_STAGE) && evaluator.isMemberInSubClassOf(parent, CLASS_STAGE_BACK_HANDLER, false)) { context.report(ISSUE, call, context.getLocation(call), "Can't call Director.handleBack() in Director.BackHandler.handleBack()"); } } } }
@Override public void visitMethod(JavaContext context, JavaElementVisitor visitor, PsiMethodCallExpression call, PsiMethod method) { super.visitMethod(context, visitor, call, method); if (isRxSubscribeableClass(method.getContainingClass()) && !PsiType.VOID.equals(method.getReturnType())) { PsiElement element = LintUtils.skipParentheses(call.getParent()); if (element instanceof PsiExpressionStatement) { String message; if (isRx2(method.getContainingClass())) { message = "No reference to the disposable is kept"; } else { message = "No reference to the subscription is kept"; } context.report(ISSUE, call, context.getLocation(call), message); } } }
private int findArgPos( PsiMethodCallExpression methodCall, PsiElement firstElem ) { PsiExpression[] args = methodCall.getArgumentList().getExpressions(); for( int i = 0; i < args.length; i++ ) { PsiExpression arg = args[i]; PsiElement csr = firstElem; while( csr != null && csr != firstElem ) { csr = csr.getParent(); } if( csr == firstElem ) { return i; } } throw new IllegalStateException(); }
private PsiElement isBuildingChildScope(PsiMethodCallExpression call) { try { // Dot PsiElement nextSibling = call.getNextSibling(); // Parenthesis nextSibling = nextSibling.getNextSibling(); // Chained method name nextSibling = nextSibling.getParent(); // Chained method PsiElement chainedMethod = nextSibling.getParent(); if (chainedMethod instanceof PsiMethodCallExpression) { PsiExpressionList list = ((PsiMethodCallExpression) chainedMethod).getArgumentList(); // If chained method is taking an argument that returns an instance of something // (such as a new Module), it is probably safe to assume that the user is building a // child scope if (list.getFirstChild() != null && list.getFirstChild().getNextSibling() instanceof PsiNewExpression) { return chainedMethod; } } } catch (Exception e) { // A for effort } return null; }
/** * Reports issue if this call is inside PLog.x(). * Calling this method assumes actual calling method is 'String#format'. * * @see #ISSUE_NESTED_FORMAT */ private static void checkNestedStringFormat(JavaContext context, PsiMethodCallExpression call) { PsiElement current = call; while (true) { current = LintUtils.skipParentheses(current.getParent()); if (current == null || current instanceof PsiCodeBlock) { // Reached AST root or code block node; String.format not inside PLog.X(..). return; } if (current instanceof PsiMethodCallExpression) { PsiMethodCallExpression expression = (PsiMethodCallExpression) current; if (Pattern.matches("org\\.mym\\.plog\\.PLog\\.(v|d|i|w|e)", expression.getMethodExpression().getQualifiedName())) { sHelper.reportIssue(context, ISSUE_NESTED_FORMAT, call); return; } } } }
@Override public void visitMethod(JavaContext context, JavaElementVisitor visitor, PsiMethodCallExpression call, PsiMethod method) { PsiReferenceExpression methodExpression = call.getMethodExpression(); String fullyQualifiedMethodName = methodExpression.getQualifiedName(); if (fullyQualifiedMethodName.startsWith("android.util.Log") //Handle multiple overloaded out.print(and println, etc) methods. || fullyQualifiedMethodName.startsWith("java.lang.System.out.print")) { sHelper.reportIssue(context, ISSUE_LOG_CLASS, methodExpression); return; } if (fullyQualifiedMethodName.equals("java.lang.String.format")) { checkNestedStringFormat(context, call); return; } }
/** * Further refine search in order to match the exact Java Camel route. * Checks if the given {@link PsiElement} contains a 'to' method that points to the give route. * * @param route the complete Camel route to search for * @param psiElement the {@link PsiElement} that might contain the complete route definition * @return the {@link PsiElement} that contains the exact match of the Camel route, null if there is no exact match */ private PsiElement findJavaElement(String route, PsiElement psiElement) { Object value; if (psiElement instanceof PsiLiteralValue) { value = ((PsiLiteralValue) psiElement).getValue(); } else { value = psiElement.getText(); } if (route.equals(value)) { //the method 'to' is a PsiIdentifier not a PsiMethodCallExpression because it's part of method invocation chain PsiMethodCallExpression methodCall = PsiTreeUtil.getParentOfType(psiElement, PsiMethodCallExpression.class); if (methodCall != null) { if (Arrays.stream(JAVA_ROUTE_CALL).anyMatch(s -> s.equals(methodCall.getMethodExpression().getReferenceName()))) { return psiElement; } } } return null; }
public PsiMethodCallPattern withArguments(final ElementPattern<? extends PsiExpression>... arguments) { return with(new PatternCondition<PsiMethodCallExpression>("withArguments") { @Override public boolean accepts(@NotNull PsiMethodCallExpression callExpression, ProcessingContext context) { final PsiExpression[] actualArguments = callExpression.getArgumentList().getExpressions(); if (arguments.length != actualArguments.length) { return false; } for (int i = 0; i < actualArguments.length; i++) { if (!arguments[i].accepts(actualArguments[i], context)) { return false; } } return true; } }); }
@Override protected PsiElement getElement() { if (!myReferenceExpression.isValid() || !myReferenceExpression.getManager().isInProject(myReferenceExpression)) return null; PsiElement parent = myReferenceExpression.getParent(); if (parent instanceof PsiMethodCallExpression) return null; if (myReferenceExpression.getReferenceNameElement() != null) { if (!CreateFromUsageUtils.isValidReference(myReferenceExpression, false)) { return myReferenceExpression; } } return null; }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); if (!MethodCallUtils.isCallToMethod(expression, null, PsiType.VOID, HardcodedMethodConstants.FINALIZE)) { return; } final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class); if (containingMethod == null) { return; } if (MethodUtils.methodMatches(containingMethod, null, PsiType.VOID, HardcodedMethodConstants.FINALIZE)) { return; } registerMethodCallError(expression); }
private static boolean isSetSecurityManager( PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"setSecurityManager".equals(methodName)) { return false; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return false; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return false; } final String className = aClass.getQualifiedName(); if (className == null) { return false; } return "java.lang.System".equals(className); }
private static boolean isDriverManagerGetConnection( PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); if (!HardcodedMethodConstants.GET_CONNECTION.equals(methodName)) { return false; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return false; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return false; } final String className = aClass.getQualifiedName(); if (className == null) { return false; } return "java.sql.DriverManager".equals(className); }
@Override protected boolean isResourceCreation(PsiExpression expression) { if (expression instanceof PsiMethodCallExpression) { final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expression; final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"accept".equals(methodName)) { return false; } final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier == null || !TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.net.ServerSocket")) { return false; } return TypeUtils.expressionHasTypeOrSubtype(methodCallExpression, "java.net.Socket"); } else if (expression instanceof PsiNewExpression) { return TypeUtils.expressionHasTypeOrSubtype(expression, "java.net.Socket", "java.net.DatagramSocket", "java.net.ServerSocket") != null; } return false; }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); @NonNls final String exec = "exec"; if (!exec.equals(methodName)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } final String className = aClass.getQualifiedName(); if (!"java.lang.Runtime".equals(className)) { return; } registerMethodCallError(expression); }
@Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); if (!MethodCallUtils.isEqualsCall(expression)) { return; } final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier == null) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return; } final PsiExpression argument = arguments[0]; if (!EquivalenceChecker.expressionsAreEquivalent(qualifier, argument) || SideEffectChecker.mayHaveSideEffects(qualifier)) { return; } registerMethodCallError(expression); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final String methodName = MethodCallUtils.getMethodName(expression); if (!HardcodedMethodConstants.TO_STRING.equals(methodName)) { return; } final PsiType targetType = MethodCallUtils.getTargetType(expression); if (!TypeUtils.typeEquals(CommonClassNames.JAVA_UTIL_DATE, targetType)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList.getExpressions().length != 0) { return; } if (NonNlsUtils.isNonNlsAnnotatedUse(expression)) { return; } registerMethodCallError(expression); }
private static boolean isThreadYield( PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"yield".equals(methodName)) { return false; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return false; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return false; } final String className = aClass.getQualifiedName(); if (className == null) { return false; } return "java.lang.Thread".equals(className); }
private static boolean isStopSuspendOrResume( PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!("stop".equals(methodName) || "suspend".equals(methodName) || "resume".equals(methodName))) { return false; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return false; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return false; } final String className = aClass.getQualifiedName(); if (className == null) { return false; } return "java.lang.Thread".equals(className); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); if (!HardcodedMethodConstants.NOTIFY.equals(methodName) && !HardcodedMethodConstants.NOTIFY_ALL.equals(methodName)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } if (SynchronizationUtil.isInSynchronizedContext(expression)) { return; } registerMethodCallError(expression); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); if (!MethodCallUtils.isCallToMethod(expression, "java.lang.Thread", PsiType.VOID, "sleep", PsiType.LONG) && !MethodCallUtils.isCallToMethod(expression, "java.lang.Thread", PsiType.VOID, "sleep", PsiType.LONG, PsiType.INT)) { return; } if (!ControlFlowUtils.isInLoop(expression)) { return; } registerMethodCallError(expression); }
private static boolean isRunFinalizersOnExit( PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); @NonNls final String runFinalizers = "runFinalizersOnExit"; if (!runFinalizers.equals(methodName)) { return false; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return false; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return false; } final String className = aClass.getQualifiedName(); if (className == null) { return false; } return "java.lang.System".equals(className); }
@Override public void visitMethodCallExpression(PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String name = methodExpression.getReferenceName(); if (!"setScale".equals(name) && !"divide".equals(name)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if (PsiUtilCore.hasErrorElementChild(argumentList)) { return; } final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return; } if (!TypeUtils.expressionHasTypeOrSubtype(expression, "java.math.BigDecimal")) { return; } registerMethodCallError(expression); }
private PsiMethodCallExpression findMethodCall(PsiElement element) { // This covers the case if there is a method being used to initialize a variable.. // i.e int a = random(); if (element instanceof PsiDeclarationStatement) { PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) element; for (PsiElement declarationElement : declarationStatement.getDeclaredElements()) { if (declarationElement instanceof PsiVariable) { PsiVariable variable = (PsiVariable) declarationElement; PsiExpression initializer = variable.getInitializer(); if (initializer instanceof PsiMethodCallExpression) { return (PsiMethodCallExpression) initializer; } } } } if (element instanceof PsiExpressionStatement) { PsiExpression expression = ((PsiExpressionStatement) element).getExpression(); if (expression instanceof PsiMethodCallExpression) { return (PsiMethodCallExpression) expression; } } return null; }
/** * Check MethodCallExpressions for calls for default (argument less) constructor * Produce an error if resolved constructor method is build by lombok and contains some arguments */ @Override public void visitMethodCallExpression(PsiMethodCallExpression methodCall) { super.visitMethodCallExpression(methodCall); PsiExpressionList list = methodCall.getArgumentList(); PsiReferenceExpression referenceToMethod = methodCall.getMethodExpression(); boolean isThisOrSuper = referenceToMethod.getReferenceNameElement() instanceof PsiKeyword; final int parameterCount = list.getExpressions().length; if (isThisOrSuper && parameterCount == 0) { JavaResolveResult[] results = referenceToMethod.multiResolve(true); JavaResolveResult resolveResult = results.length == 1 ? results[0] : JavaResolveResult.EMPTY; PsiElement resolved = resolveResult.getElement(); if (resolved instanceof LombokLightMethodBuilder && ((LombokLightMethodBuilder) resolved).getParameterList().getParameters().length != 0) { holder.registerProblem(methodCall, "Default constructor doesn't exist", ProblemHighlightType.ERROR); } } }
private boolean isChainingConstructor(PsiMethod constructor) { PsiCodeBlock body = constructor.getBody(); if (body != null) { PsiStatement[] statements = body.getStatements(); if (statements.length == 1 && statements[0] instanceof PsiExpressionStatement) { PsiExpression expression = ((PsiExpressionStatement) statements[0]).getExpression(); if (expression instanceof PsiMethodCallExpression) { PsiReferenceExpression methodExpr = ((PsiMethodCallExpression) expression).getMethodExpression(); if ("this".equals(methodExpr.getReferenceName())) { PsiElement resolved = methodExpr.resolve(); return resolved instanceof PsiMethod && ((PsiMethod) resolved).isConstructor(); //delegated via "this" call } } } } return false; }
public boolean satisfiedBy(PsiElement element){ if(!GuiceUtils.isBinding(element)){ return false; } final PsiMethodCallExpression call = (PsiMethodCallExpression) element; if(GuiceUtils.findProvidingClassForBinding(call) == null){ return false; } final PsiClass implementedClass = GuiceUtils.findImplementedClassForBinding(call); if(implementedClass == null){ return false; } if(AnnotationUtil.isAnnotated(implementedClass, "com.google.inject.ImplementedBy", true) || AnnotationUtil.isAnnotated(implementedClass, "com.google.inject.ProvidedBy", true)){ return false; } return true; }