/** * 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); } } }
@NotNull public JavaResolveResult[] getResult() { JavaResolveResult[] cachedResult = myCachedResult; if(cachedResult == null) { List<CandidateInfo> conflicts = getResults(); if(!conflicts.isEmpty()) { for(PsiConflictResolver resolver : myResolvers) { CandidateInfo candidate = resolver.resolveConflict(conflicts); if(candidate != null) { conflicts.clear(); conflicts.add(candidate); break; } } } myCachedResult = cachedResult = conflicts.toArray(new JavaResolveResult[conflicts.size()]); } return cachedResult; }
private static void registerSwapFixes(final PsiExpression[] expressions, final PsiCall callExpression, final List<PsiCall> permutations, MethodCandidateInfo candidate, final int incompatibilitiesCount, final int minIncompatibleIndex, final int maxIncompatibleIndex) throws IncorrectOperationException { PsiMethod method = candidate.getElement(); PsiSubstitutor substitutor = candidate.getSubstitutor(); if (incompatibilitiesCount >= 3) return; // no way we can fix it by swapping for (int i = minIncompatibleIndex; i < maxIncompatibleIndex; i++) { for (int j = i+1; j <= maxIncompatibleIndex; j++) { ArrayUtil.swap(expressions, i, j); if (PsiUtil.isApplicable(method, substitutor, expressions)) { PsiCall copy = (PsiCall)callExpression.copy(); PsiExpression[] copyExpressions = copy.getArgumentList().getExpressions(); copyExpressions[i].replace(expressions[i]); copyExpressions[j].replace(expressions[j]); JavaResolveResult result = copy.resolveMethodGenerics(); if (result.getElement() != null && result.isValidResult()) { permutations.add(copy); if (permutations.size() > 1) return; } } ArrayUtil.swap(expressions, i, j); } } }
@RequiredReadAction @Nullable @Override public PsiElement[] getSymbolTypeDeclarations(@NotNull PsiElement targetElement, Editor editor, int offset) { PsiType type; if (targetElement instanceof PsiVariable){ type = ((PsiVariable)targetElement).getType(); } else if (targetElement instanceof PsiMethod){ type = ((PsiMethod)targetElement).getReturnType(); } else{ return null; } if (type == null) return null; if (editor != null) { final PsiReference reference = TargetElementUtil.findReference(editor, offset); if (reference instanceof PsiJavaReference) { final JavaResolveResult resolveResult = ((PsiJavaReference)reference).advancedResolve(true); type = resolveResult.getSubstitutor().substitute(type); } } PsiClass psiClass = PsiUtil.resolveClassInType(type); return psiClass == null ? null : new PsiElement[] {psiClass}; }
public static PsiVariable resolveVariable(@NotNull PsiJavaCodeReferenceElement ref, boolean[] problemWithAccess, boolean[] problemWithStatic ) { /* long time1 = System.currentTimeMillis(); */ final VariableResolverProcessor processor = new VariableResolverProcessor(ref, ref.getContainingFile()); PsiScopesUtil.resolveAndWalk(processor, ref, null); /* long time2 = System.currentTimeMillis(); Statistics.resolveVariableTime += (time2 - time1); Statistics.resolveVariableCount++; */ final JavaResolveResult[] result = processor.getResult(); if (result.length != 1) return null; final PsiVariable refVar = (PsiVariable)result[0].getElement(); if (problemWithAccess != null) { problemWithAccess[0] = !result[0].isAccessible(); } if (problemWithStatic != null) { problemWithStatic[0] = !result[0].isStaticsScopeCorrect(); } return refVar; }
public void testDuplicateStaticImport() throws Exception { PsiReference ref = configure(); final JavaResolveResult result = ((PsiJavaReference)ref).advancedResolve(true); PsiElement target = result.getElement(); assertNotNull(target); assertTrue(result.isValidResult()); }
public void testRhombExtending() throws Exception { PsiReference ref = configure(); final JavaResolveResult result = ((PsiJavaReference)ref).advancedResolve(true); PsiElement target = result.getElement(); assertNotNull(target); assertTrue(result.isValidResult()); }
@Override public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) { JavaResolveResult[] cachedResult = myCachedResult; if(cachedResult != null && cachedResult.length == 1 && cachedResult[0].isAccessible()) { return false; } if(myName == null || PsiUtil.checkName(element, myName, myPlace)) { return super.execute(element, state); } return true; }
public static PsiVariable resolveVariable(@NotNull PsiJavaCodeReferenceElement ref, boolean[] problemWithAccess, boolean[] problemWithStatic) { /* long time1 = System.currentTimeMillis(); */ final VariableResolverProcessor processor = new VariableResolverProcessor(ref, ref.getContainingFile()); PsiScopesUtil.resolveAndWalk(processor, ref, null); /* long time2 = System.currentTimeMillis(); Statistics.resolveVariableTime += (time2 - time1); Statistics.resolveVariableCount++; */ final JavaResolveResult[] result = processor.getResult(); if(result.length != 1) { return null; } final PsiVariable refVar = (PsiVariable) result[0].getElement(); if(problemWithAccess != null) { problemWithAccess[0] = !result[0].isAccessible(); } if(problemWithStatic != null) { problemWithStatic[0] = !result[0].isStaticsScopeCorrect(); } return refVar; }
@NotNull @Override public JavaResolveResult advancedResolve(boolean incompleteCode) { final JavaResolveResult[] results = multiResolve(incompleteCode); return results.length == 1 ? results[0] : JavaResolveResult.EMPTY; }
@NotNull @Override public List<StandardMethodContract> toContracts(PsiMethod method, Supplier<PsiCodeBlock> body) { PsiMethodCallExpression call = ObjectUtil.tryCast(expression.restoreExpression(body.get()), PsiMethodCallExpression.class); if(call == null) { return Collections.emptyList(); } JavaResolveResult result = call.resolveMethodGenerics(); PsiMethod targetMethod = ObjectUtil.tryCast(result.getElement(), PsiMethod.class); if(targetMethod == null) { return Collections.emptyList(); } PsiParameter[] parameters = targetMethod.getParameterList().getParameters(); PsiExpression[] arguments = call.getArgumentList().getExpressions(); boolean varArgCall = MethodCallInstruction.isVarArgCall(targetMethod, result.getSubstitutor(), arguments, parameters); List<StandardMethodContract> fromDelegate = ContainerUtil.mapNotNull(ControlFlowAnalyzer.getMethodContracts(targetMethod), dc -> convertDelegatedMethodContract(method, parameters, arguments, varArgCall, dc)); if(NullableNotNullManager.isNotNull(targetMethod)) { return ContainerUtil.concat(ContainerUtil.map(fromDelegate, DelegationContract::returnNotNull), Collections.singletonList(new StandardMethodContract(emptyConstraints(method), MethodContract.ValueConstraint.NOT_NULL_VALUE))); } return fromDelegate; }
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)); } }
public Collection<JavaMethodCandidateInfo> getMethods() { return ContainerUtil.mapNotNull(getCandidates(), new Function<JavaResolveResult, JavaMethodCandidateInfo>() { @Override public JavaMethodCandidateInfo fun(final JavaResolveResult javaResolveResult) { return new JavaMethodCandidateInfo((PsiMethod) javaResolveResult.getElement(), javaResolveResult.getSubstitutor()); } }); }
@Override protected AccessStaticViaInstanceFix createAccessStaticViaInstanceFix(PsiReferenceExpression expr, boolean onTheFly, JavaResolveResult result) { return new AccessStaticViaInstanceFix(expr, result, onTheFly); }
public JavaResolveResult toJavaResolveResult() { return new JavaResult(this); }