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; }
@Override protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException { PsiConditionalExpression cond = (PsiConditionalExpression)element.getParent(); PsiElement savedBlock; if (cond.getElseExpression() == element) { savedBlock = element; } else { savedBlock = cond.getThenExpression(); } context.extractElement(savedBlock, cond); if (cond.getParent() instanceof PsiExpressionList) { context.delete(cond); } else { context.deleteExactly(cond); } }
@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); }
@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); if (!ExpressionUtils.hasType(expression, "java.text.SimpleDateFormat")) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList == null) { return; } final PsiExpression[] arguments = argumentList.getExpressions(); for (PsiExpression argument : arguments) { if (ExpressionUtils.hasType(argument, "java.util.Locale")) { return; } } registerNewExpressionError(expression); }
@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); }
@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); final PsiType type = expression.getType(); if (type == null) { return; } if (!CollectionUtils.isCollectionWithInitialCapacity(type)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList == null) { return; } final PsiExpression[] parameters = argumentList.getExpressions(); if (parameters.length != 0) { return; } registerError(expression); }
@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); if (!ExpressionUtils.hasType(expression, "java.text.SimpleDateFormat")) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList == null) { return; } final PsiExpression[] arguments = argumentList.getExpressions(); for (PsiExpression argument : arguments) { if (ExpressionUtils.hasType(argument, "java.util.Locale")) { return; } } registerError(expression); }
/** * 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); } } }
@UnsignedOrNotFound public static int findListIndex(@Unsigned int offset, @InstanceOf({PsiParameterList.class, PsiExpressionList.class}) @Nullable PsiElement listElement) { if ( listElement == null ) { return -1; } int index = -1; for( PsiElement element : listElement.getChildren() ) { if ( element.getTextRange().contains(offset) ) { return index; } if ( PsiUtil.isJavaToken(element, JavaTokenType.LPARENTH) || PsiUtil.isJavaToken(element, JavaTokenType.COMMA) ) { index++; } if ( PsiUtil.isJavaToken(element, JavaTokenType.RPARENTH) ) { return -1; } } return - 1; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethodCallExpression.class); if(methodCallExpression != null) { ExpressionUtils.bindCallTo(methodCallExpression, myTargetMethodName); if(myClearArguments) { PsiExpressionList argList = methodCallExpression.getArgumentList(); PsiExpression[] args = argList.getExpressions(); if(args.length > 0) { argList.deleteChildRange(args[0], args[args.length - 1]); } } } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement problemElement = descriptor.getPsiElement(); if(problemElement == null) { return; } PsiMethodCallExpression call = problemElement.getParent() instanceof PsiExpressionList && problemElement.getParent().getParent() instanceof PsiMethodCallExpression ? (PsiMethodCallExpression) problemElement.getParent().getParent() : null; PsiMethod targetMethod = call == null ? null : call.resolveMethod(); JavaPsiFacade facade = JavaPsiFacade.getInstance(project); problemElement.replace(facade.getElementFactory().createExpressionFromText(myReplacementText, null)); if(targetMethod != null) { ExtractMethodUtil.addCastsToEnsureResolveTarget(targetMethod, call); } }
@Override public void visitNewExpression(@NotNull PsiNewExpression newExpression) { if (passed) { return; } super.visitNewExpression(newExpression); final PsiExpressionList argumentList = newExpression.getArgumentList(); if (argumentList == null) { return; } final PsiExpression[] arguments = argumentList.getExpressions(); for (PsiExpression argument : arguments) { if (VariableAccessUtils.mayEvaluateToVariable(argument, variable)) { passed = true; break; } } }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { if (passed) { return; } super.visitMethodCallExpression(call); final PsiExpressionList argumentList = call.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); for (PsiExpression argument : arguments) { if (!VariableAccessUtils.mayEvaluateToVariable(argument, variable, myBuilderPattern)) { continue; } final PsiMethod method = call.resolveMethod(); if (method != null) { final PsiClass aClass = method.getContainingClass(); if (aClass != null) { final String name = aClass.getQualifiedName(); if (excludes.contains(name)) { continue; } } } passed = true; } }
@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); if(myIgnoreFields && expression.getParent() instanceof PsiField) { return; } final PsiType type = expression.getType(); if(!isCollectionWithInitialCapacity(type)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if(argumentList == null || argumentList.getExpressions().length != 0) { return; } registerNewExpressionError(expression); }
@Override public JavaElementVisitor createPsiVisitor(@NonNull final JavaContext context) { return new JavaElementVisitor() { @Override public void visitLambdaExpression(PsiLambdaExpression expression) { if (!(expression.getParent() instanceof PsiExpressionList)) { return; } PsiExpressionList exprList = (PsiExpressionList) expression.getParent(); if (!(exprList.getParent() instanceof PsiMethodCallExpression)) { return; } PsiMethodCallExpression call = (PsiMethodCallExpression) exprList.getParent(); if (call.getType() == null) { return; } String callType = call.getType().getCanonicalText(); if (!callType.startsWith("de.mobilej.thinr.Thinr")) { return; } markLeakSuspects(expression, expression, context); } }; }
@Nullable @Override public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) { if (ServiceManager.getService(element.getProject(), CamelService.class).isCamelPresent()) { PsiExpressionList exps = PsiTreeUtil.getNextSiblingOfType(originalElement, PsiExpressionList.class); if (exps != null) { if (exps.getExpressions().length >= 1) { // grab first string parameter (as the string would contain the camel endpoint uri final PsiClassType stringType = PsiType.getJavaLangString(element.getManager(), element.getResolveScope()); PsiExpression exp = Arrays.stream(exps.getExpressions()).filter( e -> e.getType() != null && stringType.isAssignableFrom(e.getType())) .findFirst().orElse(null); if (exp instanceof PsiLiteralExpression) { Object o = ((PsiLiteralExpression) exp).getValue(); String val = o != null ? o.toString() : null; // okay only allow this popup to work when its from a RouteBuilder class PsiClass clazz = PsiTreeUtil.getParentOfType(originalElement, PsiClass.class); if (clazz != null) { PsiClassType[] types = clazz.getExtendsListTypes(); boolean found = Arrays.stream(types).anyMatch(p -> p.getClassName().equals("RouteBuilder")); if (found) { String componentName = StringUtils.asComponentName(val); if (componentName != null) { // the quick info cannot be so wide so wrap at 120 chars return generateCamelComponentDocumentation(componentName, val, 120, element.getProject()); } } } } } } } return null; }
@Override public int getFlags(PsiElement elementInHost, PsiFile regexp) { final PsiExpressionList list = PsiTreeUtil.getParentOfType(elementInHost, PsiExpressionList.class); if (list != null && list.getExpressions().length == 2 && list.getExpressionTypes()[1] == PsiType.INT) { final Object result = JavaConstantExpressionEvaluator.computeConstantExpression(list.getExpressions()[1], false); if (result instanceof Integer) { //noinspection MagicConstant return ((Integer)result).intValue(); } } return 0; }
public boolean isOK(PsiExpression occurrence) { PsiElement parent = occurrence.getParent(); while(parent instanceof PsiExpression) { parent = parent.getParent(); } if(!(parent instanceof PsiExpressionList)) return true; parent = parent.getParent(); if(!(parent instanceof PsiMethodCallExpression)) return true; final String text = ((PsiMethodCallExpression) parent).getMethodExpression().getText(); return !getKeywordText().equals(text); }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (methodName == null) { return; } final boolean resultSet; if (methodName.startsWith("get") || methodName.startsWith("update")) { resultSet = true; } else if (methodName.startsWith("set")) { resultSet = false; } else { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length == 0) { return; } final PsiExpression argument = arguments[0]; final Object val = ExpressionUtils.computeConstantExpression(argument); if (!(val instanceof Integer) || ((Integer)val).intValue() != 0) { return; } final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (resultSet) { if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.ResultSet")) { registerError(argument, Boolean.TRUE); } } else if (arguments.length > 1) { if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.PreparedStatement")) { registerError(argument, Boolean.FALSE); } } }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression call) { super.visitMethodCallExpression(call); if (!MethodCallUtils.isEqualsCall(call)) { return; } final PsiExpressionList argumentList = call.getArgumentList(); final PsiExpression[] args = argumentList.getExpressions(); if (args.length == 0 || !ExpressionUtils.isNullLiteral(args[0])) { return; } registerError(args[0]); }
/** * Allows to check if given expression list has given number of anonymous classes. * * @param count interested number of anonymous classes used at the given expression list * @return <code>true</code> if given expression list contains given number of anonymous classes; * <code>false</code> otherwise */ public static boolean hasAnonymousClassesArguments(@NotNull PsiExpressionList expressionList, int count) { int found = 0; for (PsiExpression expression : expressionList.getExpressions()) { ASTNode node = expression.getNode(); if (isAnonymousClass(node)) { found++; } if (found >= count) { return true; } } return false; }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (methodName == null) { return; } final boolean resultSet; if (methodName.startsWith("get") || methodName.startsWith("update")) { resultSet = true; } else if (methodName.startsWith("set")) { resultSet = false; } else { return; } final PsiExpressionList argumentList = expression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length == 0) { return; } final PsiExpression argument = arguments[0]; final Object val = ExpressionUtils.computeConstantExpression(argument); if (!(val instanceof Integer) || ((Integer)val).intValue() != 0) { return; } final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (resultSet) { if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.ResultSet")) { registerError(argument, Boolean.valueOf(resultSet)); } } else { if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.PreparedStatement")) { registerError(argument, Boolean.valueOf(resultSet)); } } }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final String methodName = MethodCallUtils.getMethodName(expression); if (!HardcodedMethodConstants.PRINT_STACK_TRACE.equals( methodName)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList.getExpressions().length != 0) { return; } registerMethodCallError(expression); }
public MethodResolverProcessor(PsiClass classConstr, @NotNull PsiExpressionList argumentList, @NotNull PsiElement place, @NotNull PsiFile placeFile) { super(place, placeFile, new PsiConflictResolver[]{ new JavaMethodsConflictResolver(argumentList, PsiUtil.getLanguageLevel(placeFile)) }, new SmartList<CandidateInfo>()); setIsConstructor(true); setAccessClass(classConstr); setArgumentList(argumentList); }
private static PsiMethodCallExpression findCallExpression(@NotNull PsiElement anchor) { final PsiElement argList = PsiUtil.skipParenthesizedExprUp(anchor).getParent(); if(argList instanceof PsiExpressionList) { final PsiElement parent = argList.getParent(); if(parent instanceof PsiMethodCallExpression) { return (PsiMethodCallExpression) parent; } } return null; }
public static void registerIntentions(@NotNull JavaResolveResult[] candidates, @NotNull PsiExpressionList arguments, @Nullable HighlightInfo highlightInfo, TextRange fixRange) { for (JavaResolveResult candidate : candidates) { registerIntention(arguments, highlightInfo, fixRange, candidate, arguments); } }
private static void registerIntention(@NotNull PsiExpressionList arguments, @Nullable HighlightInfo highlightInfo, TextRange fixRange, @NotNull JavaResolveResult candidate, @NotNull PsiElement context) { if (!candidate.isStaticsScopeCorrect()) return; PsiMethod method = (PsiMethod)candidate.getElement(); PsiSubstitutor substitutor = candidate.getSubstitutor(); if (method != null && context.getManager().isInProject(method)) { QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, new RemoveRedundantArgumentsFix(method, arguments.getExpressions(), substitutor)); } }
protected WrapArrayToArraysAsListFix(final @NotNull PsiExpressionList list, final int i, final @NotNull PsiType toType, final @NotNull ArgumentFixerActionFactory fixerActionFactory) { super(list, i, toType, fixerActionFactory); }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { if (passed) { return; } super.visitMethodCallExpression(call); final PsiExpressionList argumentList = call.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); for (PsiExpression argument : arguments) { if (VariableAccessUtils.mayEvaluateToVariable(argument, variable)) { passed = true; break; } } }
@Override public void visitNewExpression(@NotNull PsiNewExpression newExpression) { if (passed) { return; } super.visitNewExpression(newExpression); final PsiExpressionList argumentList = newExpression.getArgumentList(); if (argumentList == null) { return; } final PsiExpression[] arguments = argumentList.getExpressions(); for (PsiExpression argument : arguments) { if (!VariableAccessUtils.mayEvaluateToVariable(argument, variable, myBuilderPattern)) { continue; } final PsiMethod constructor = newExpression.resolveConstructor(); if (constructor != null) { final PsiClass aClass = constructor.getContainingClass(); if (aClass != null) { final String name = aClass.getQualifiedName(); if (excludes.contains(name)) { continue; } } } passed = true; } }
@Nullable private static Color getColor(PsiExpressionList list) { try { final PsiExpression[] args = list.getExpressions(); final PsiType[] types = list.getExpressionTypes(); ColorConstructors type = getConstructorType(types); if(type != null) { switch(type) { case INT: return new Color(getInt(args[0])); case INT_BOOL: return new Color(getInt(args[0]), getBoolean(args[1])); case INT_x3: return new Color(getInt(args[0]), getInt(args[1]), getInt(args[2])); case INT_x4: return new Color(getInt(args[0]), getInt(args[1]), getInt(args[2]), getInt(args[3])); case FLOAT_x3: return new Color(getFloat(args[0]), getFloat(args[1]), getFloat(args[2])); case FLOAT_x4: return new Color(getFloat(args[0]), getFloat(args[1]), getFloat(args[2]), getFloat(args[3])); } } } catch(Exception ignore) { } return null; }
@Override protected boolean isApplicable(PsiMethod method, PsiElement place) { final PsiExpressionList argumentList = ((PsiMethodCallExpression) place).getArgumentList(); final MethodCandidateInfo candidateInfo = new MethodCandidateInfo(method, PsiSubstitutor.EMPTY, false, false, argumentList, null, argumentList.getExpressionTypes(), null); PsiSubstitutor substitutorForMethod = candidateInfo.getSubstitutor(); if(PsiUtil.isApplicable(method, substitutorForMethod, argumentList)) { final PsiType returnType = substitutorForMethod.substitute(method.getReturnType()); final PsiType expectedType = getExpectedType(); return expectedType == null || returnType == null || TypeConversionUtil.isAssignable(expectedType, returnType); } return false; }