@NotNull @Override public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) { return new JavaElementVisitor() { @Override public void visitReferenceExpression(PsiReferenceExpression psiReferenceExpression) { } @Override public void visitLocalVariable(PsiLocalVariable variable) { super.visitLocalVariable(variable); if (VariantPool.isJump()) { if (isFindYYY(variable.toString())) { holder.registerProblem(variable, DESCRIPTION_TEMPLATE, myQuickFix); log.info("isFindYYY" + variable ); } } } }; }
@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; } }
@Nullable @Override protected List<DfaVariableValue> create(PsiElement closure) { final Set<DfaVariableValue> result = ContainerUtil.newLinkedHashSet(); closure.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof PsiReferenceExpression) { DfaValue value = myFactory.createValue((PsiReferenceExpression)element); if (value instanceof DfaVariableValue) { result.add((DfaVariableValue)value); } } super.visitElement(element); } }); return ContainerUtil.newArrayList(result); }
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); }
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); }
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 visitReferenceExpression( @NotNull PsiReferenceExpression referenceExpression) { if (used) { return; } super.visitReferenceExpression(referenceExpression); final PsiElement target = referenceExpression.resolve(); if (target == null) { return; } if (target.equals(variable)) { used = true; //stopWalking(); } }
@Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); final String name = expression.getReferenceName(); if (!HardcodedMethodConstants.OUT.equals(name) && !HardcodedMethodConstants.ERR.equals(name)) { return; } final PsiElement referent = expression.resolve(); if (!(referent instanceof PsiField)) { return; } final PsiField field = (PsiField)referent; final PsiClass containingClass = field.getContainingClass(); if (containingClass == null) { return; } final String className = containingClass.getQualifiedName(); if (!"java.lang.System".equals(className)) { return; } registerError(expression, 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); }
public final boolean value( PsiFile psiFile ) { final boolean[] hasPolygeneImportPackage = new boolean[]{ false }; psiFile.accept( new JavaElementVisitor() { @Override public final void visitImportStatement( PsiImportStatement statement ) { String packageName = statement.getQualifiedName(); if( packageName != null && packageName.startsWith( "org.apache.polygene" ) ) { hasPolygeneImportPackage[ 0 ] = true; } } @Override public void visitReferenceExpression( PsiReferenceExpression expression ) { // Ignore } } ); return hasPolygeneImportPackage[ 0 ]; }
@NotNull public IncrementableSet<SourceTargetPair> resolveRelationships(PsiClass psiClass) { IncrementableSet<SourceTargetPair> incrementableSet = new IncrementableSet<>(); for (DiagramNode<PsiElement> node : myNodes) { PsiElement callee = node.getIdentifyingElement(); Collection<PsiReference> all = ReferencesSearch.search(callee, new LocalSearchScope (psiClass)).findAll(); for (PsiReference psiReference : all) { if (!(psiReference instanceof PsiReferenceExpression)) { continue; } PsiReferenceExpression referenceExpression = (PsiReferenceExpression) psiReference; PsiElement caller = PsiUtils.getRootPsiElement(psiClass, referenceExpression); if (caller == null) { continue; } incrementableSet.increment(new SourceTargetPair(caller, callee)); } } return incrementableSet; }
@Override public void visitReferenceExpression( @NotNull PsiReferenceExpression expression) { super.visitReferenceExpression(expression); final String name = expression.getReferenceName(); if (!HardcodedMethodConstants.OUT.equals(name) && !HardcodedMethodConstants.ERR.equals(name)) { return; } final PsiElement referent = expression.resolve(); if (!(referent instanceof PsiField)) { return; } final PsiField field = (PsiField)referent; final PsiClass containingClass = field.getContainingClass(); if (containingClass == null) { return; } final String className = containingClass.getQualifiedName(); if (!"java.lang.System".equals(className)) { 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); } } }
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; }
@Nullable public static ReplaceWithObjectsEqualsFix createFix(@NotNull PsiMethodCallExpression call, @NotNull PsiReferenceExpression methodExpression) { if(!"equals".equals(methodExpression.getReferenceName()) || call.getArgumentList().getExpressions().length != 1 || !PsiUtil.getLanguageLevel(call).isAtLeast(LanguageLevel.JDK_1_7)) { return null; } PsiExpression qualifier = methodExpression.getQualifierExpression(); PsiExpression noParens = PsiUtil.skipParenthesizedExprDown(qualifier); if(noParens == null) { return null; } PsiMethod method = call.resolveMethod(); if(method != null && method.getParameterList().getParametersCount() == 1 && method.getParameterList().getParameters()[0].getType().equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) { return new ReplaceWithObjectsEqualsFix(qualifier.getText(), noParens.getText()); } return null; }
static HighlightInfo checkRawConstructorReference(@NotNull PsiMethodReferenceExpression expression) { if(expression.isConstructor()) { PsiType[] typeParameters = expression.getTypeParameters(); if(typeParameters.length > 0) { PsiElement qualifier = expression.getQualifier(); if(qualifier instanceof PsiReferenceExpression) { PsiElement resolve = ((PsiReferenceExpression) qualifier).resolve(); if(resolve instanceof PsiClass && ((PsiClass) resolve).hasTypeParameters()) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression) .descriptionAndTooltip("Raw constructor reference with explicit type parameters for " + "constructor").create(); } } } } return null; }
protected static void replaceExpressionWithReferenceTo( @NotNull PsiExpression expression, @NotNull PsiMember target) throws IncorrectOperationException { final Project project = expression.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiElementFactory factory = psiFacade.getElementFactory(); final PsiReferenceExpression newExpression = (PsiReferenceExpression) factory.createExpressionFromText("xxx", expression); final PsiReferenceExpression replacementExpression = (PsiReferenceExpression)expression.replace(newExpression); final PsiElement element = replacementExpression.bindToElement(target); final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project); styleManager.shortenClassReferences(element); }
static Set<LookupElement> suggestQualifyingSuperCalls(PsiElement element, PsiJavaReference javaReference, ElementFilter elementFilter, JavaCompletionProcessor.Options options, Condition<String> nameCondition) { Set<LookupElement> set = ContainerUtil.newLinkedHashSet(); for(final String className : getContainingClassNames(element)) { PsiReferenceExpression fakeSuper = JavaCompletionUtil.createReference(className + ".super.rulez", element); PsiElement leaf = ObjectUtils.assertNotNull(fakeSuper.getReferenceNameElement()); JavaCompletionProcessor superProcessor = new JavaCompletionProcessor(leaf, elementFilter, options, nameCondition); fakeSuper.processVariants(superProcessor); for(CompletionElement completionElement : superProcessor.getResults()) { for(LookupElement item : JavaCompletionUtil.createLookupElements(completionElement, javaReference)) { set.add(withQualifiedSuper(className, item)); } } } return set; }
private static void registerPriorityActions(@NotNull QuickFixActionRegistrar registrar, @NotNull TextRange fixRange, @NotNull PsiReferenceExpression refExpr) { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(refExpr.getProject()); final Map<VariableKind, IntentionAction> map = new EnumMap<>(VariableKind.class); map.put(VariableKind.FIELD, new CreateFieldFromUsageFix(refExpr)); map.put(VariableKind.STATIC_FINAL_FIELD, new CreateConstantFieldFromUsageFix(refExpr)); if(!refExpr.isQualified()) { map.put(VariableKind.LOCAL_VARIABLE, new CreateLocalFromUsageFix(refExpr)); map.put(VariableKind.PARAMETER, new CreateParameterFromUsageFix(refExpr)); } final VariableKind kind = getKind(styleManager, refExpr); if(map.containsKey(kind)) { map.put(kind, PriorityIntentionActionWrapper.highPriority(map.get(kind))); } for(IntentionAction action : map.values()) { registrar.register(fixRange, action, null); } }
@NotNull @Override protected List<PsiMethod> getMembersToImport(boolean applicableOnly) { final Project project = myMethodCall.getProject(); PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project); final PsiMethodCallExpression element = myMethodCall.getElement(); PsiReferenceExpression reference = element == null ? null : element.getMethodExpression(); String name = reference == null ? null : reference.getReferenceName(); if(name == null) { return Collections.emptyList(); } final StaticMembersProcessor<PsiMethod> processor = new MyStaticMethodProcessor(element); cache.processMethodsWithName(name, element.getResolveScope(), processor); return processor.getMembersToImport(applicableOnly); }
@Nullable private static Icon getExpressionIcon(@NotNull PsiExpression expression) { if(expression instanceof PsiReferenceExpression) { final PsiElement element = ((PsiReferenceExpression) expression).resolve(); if(element != null) { return IconDescriptorUpdaters.getIcon(element, 0); } } if(expression instanceof PsiMethodCallExpression) { return AllIcons.Nodes.Method; } return null; }
@NotNull private static Nullness extractNullityFromWhenValue(PsiAnnotation nonNull) { PsiAnnotationMemberValue when = nonNull.findAttributeValue("when"); if(when instanceof PsiReferenceExpression) { String refName = ((PsiReferenceExpression) when).getReferenceName(); if("ALWAYS".equals(refName)) { return Nullness.NOT_NULL; } if("MAYBE".equals(refName) || "NEVER".equals(refName)) { return Nullness.NULLABLE; } } return Nullness.UNKNOWN; }
@Nullable private static PsiClass findPlaceClass(PsiElement element, PsiElement position) { if(position.getParent() instanceof PsiReferenceExpression) { final PsiExpression qualifierExpression = ((PsiReferenceExpression) position.getParent()).getQualifierExpression(); if(qualifierExpression != null) { final PsiType type = qualifierExpression.getType(); if(type instanceof PsiClassType) { final PsiClass psiClass = ((PsiClassType) type).resolve(); if(psiClass != null) { return psiClass; } } } } return PsiTreeUtil.getContextOfType(element, PsiClass.class, false); }