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 public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (!(psiElement instanceof PsiNewExpression)) return; PsiNewExpression expr = (PsiNewExpression)psiElement; int count = 0; for (PsiElement element : expr.getChildren()) { if (element.getNode().getElementType() == JavaTokenType.LBRACKET) { count++; } else if (element.getNode().getElementType() == JavaTokenType.RBRACKET) { count--; } } if (count > 0) { editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "]"); } }
@Override public void visitArrayInitializerExpression( PsiArrayInitializerExpression expression) { super.visitArrayInitializerExpression(expression); final PsiExpression[] initializers = expression.getInitializers(); if (initializers.length > 0) { return; } if (expression.getParent() instanceof PsiNewExpression) { return; } if (ExpressionUtils.isDeclaredConstant(expression)) { return; } registerError(expression); }
@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 visitExpressionStatement(@NotNull PsiExpressionStatement statement) { super.visitExpressionStatement(statement); final PsiExpression expression = statement.getExpression(); if (!(expression instanceof PsiNewExpression)) { return; } final PsiNewExpression newExpression = (PsiNewExpression)expression; final PsiExpression[] arrayDimensions = newExpression.getArrayDimensions(); if (arrayDimensions.length != 0) { return; } if (newExpression.getArrayInitializer() != null) { return; } registerNewExpressionError(newExpression); }
@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 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); }
private static boolean isUnusedInAnonymousClass(@NotNull PsiMethod method) { PsiClass containingClass = method.getContainingClass(); if(!(containingClass instanceof PsiAnonymousClass)) { return false; } if(containingClass.getParent() instanceof PsiNewExpression && containingClass.getParent().getParent() instanceof PsiVariable && !method.getHierarchicalMethodSignature().getSuperSignatures() .isEmpty()) { // references outside anonymous class can still resolve to this method, see com.intellij.psi.scope.util.PsiScopesUtil.setupAndRunProcessor() return false; } return MethodReferencesSearch.search(method, new LocalSearchScope(containingClass), false).findFirst() == null; }
@Override public void visitArrayInitializerExpression(PsiArrayInitializerExpression expression) { super.visitArrayInitializerExpression(expression); final PsiExpression[] initializers = expression.getInitializers(); if(initializers.length > 0) { return; } if(expression.getParent() instanceof PsiNewExpression) { return; } if(ExpressionUtils.isDeclaredConstant(expression)) { return; } registerError(expression); }
@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 apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(!(psiElement instanceof PsiNewExpression)) { return; } PsiNewExpression expr = (PsiNewExpression) psiElement; int count = 0; for(PsiElement element : expr.getChildren()) { if(element.getNode().getElementType() == JavaTokenType.LBRACKET) { count++; } else if(element.getNode().getElementType() == JavaTokenType.RBRACKET) { count--; } } if(count > 0) { editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "]"); } }
@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 void visitNewExpression(PsiNewExpression expression) { super.visitNewExpression(expression); PsiMethod constructor = expression.resolveConstructor(); if (constructor == null) return; JavaEvaluator evaluator = mContext.getEvaluator(); PsiClass cls = constructor.getContainingClass(); boolean isView = evaluator.extendsClass(cls, CLASS_VIEW, true); if (!isView) return; Location location = this.mContext.getLocation(expression); PsiElement psiElement = expression.getParent(); //创建的变量没有赋值给本地变量,无法指定注解 if (!(psiElement instanceof PsiLocalVariable)) { this.mContext.report(DynamicNewViewDetector.ISSUE, expression, location, "检测到动态创建view操作,new 操作的结果需要赋值给本地变量"); return; } PsiLocalVariable localVariable = (PsiLocalVariable) psiElement; PsiModifierList modifierList = localVariable.getModifierList(); if (modifierList == null) { this.mContext.report(DynamicNewViewDetector.ISSUE, expression, location, "检测到动态创建view操作,确认是否为view指定了唯一标识"); return; } PsiAnnotation[] annotations = modifierList.getAnnotations(); for (PsiAnnotation annotation : annotations) { String fullName = annotation.getQualifiedName(); if (IDENTIFIER_ANNOTATION.equals(fullName)) { return; } } this.mContext.report(DynamicNewViewDetector.ISSUE, expression, location, "检测到动态创建view操作,确认是否为view指定了唯一标识"); }
@Override public boolean isApplicableTo(PsiElement e) { if (e instanceof PsiArrayInitializerExpression) { final PsiElement gParent = e.getParent(); if (gParent instanceof PsiNewExpression && gParent.getParent() instanceof PsiVariable) { return true; } } return false; }
protected boolean isAvailableOnElementInEditorAndFile(@NotNull final PsiElement element, @NotNull final Editor editor, @NotNull PsiFile file, @NotNull DataContext context) { final PsiElement targetElement = file.findElementAt(editor.getCaretModel().getOffset()); if (PsiTreeUtil.getParentOfType(targetElement, PsiAnonymousClass.class) != null) { return true; } if (PsiTreeUtil.getParentOfType(element, PsiAnonymousClass.class) != null) { return true; } final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class); return newExpression != null && newExpression.getAnonymousClass() != null; }
PsiExpression getQualifier () { PsiElement element = getElement(); if (element instanceof PsiReferenceExpression) { return ((PsiReferenceExpression)element).getQualifierExpression(); } else if (element instanceof PsiNewExpression) return ((PsiNewExpression)element).getQualifier(); return null; }
@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); if (!TypeUtils.expressionHasTypeOrSubtype(expression, "java.lang.ClassLoader")) { return; } registerNewExpressionError(expression); }
@Override public void visitNewExpression( @NotNull PsiNewExpression expression) { super.visitNewExpression(expression); if (!ExpressionUtils.isZeroLengthArrayConstruction(expression)) { return; } if (ExpressionUtils.isDeclaredConstant(expression)) { return; } registerError(expression); }
@Override public void visitNewExpression( @NotNull PsiNewExpression newExpression) { super.visitNewExpression(newExpression); final PsiType type = newExpression.getType(); if (type == null) { return; } @NonNls final String typeString = type.getCanonicalText(); if (!"java.lang.ProcessBuilder".equals(typeString)) { return; } registerNewExpressionError(newExpression); }
@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { final PsiType type = expression.getType(); if (!(type instanceof PsiClassType)) { return; } final PsiClass aClass = ((PsiClassType)type).resolve(); if (aClass == null) { return; } if (!UtilityClassUtil.isUtilityClass(aClass)) { return; } registerNewExpressionError(expression); }
public boolean satisfiedBy(@NotNull PsiElement element) { if (!(element instanceof PsiArrayInitializerExpression)) { return false; } final PsiArrayInitializerExpression arrayInitializerExpression = (PsiArrayInitializerExpression)element; if (arrayInitializerExpression.getType() == null) { return false; } final PsiElement parent = element.getParent(); return !(parent instanceof PsiNewExpression); }
@Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); if(!ConstructionUtils.isEmptyArrayInitializer(expression)) { return; } if(ExpressionUtils.isDeclaredConstant(expression)) { return; } registerError(expression); }
@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; } }
private static boolean isConstructorCallPlace(@NotNull PsiElement position) { return CachedValuesManager.getCachedValue(position, () -> { boolean result = JavaClassNameCompletionContributor.AFTER_NEW.accepts(position) && !JavaClassNameInsertHandler.isArrayTypeExpected(PsiTreeUtil.getParentOfType(position, PsiNewExpression .class)); return CachedValueProvider.Result.create(result, position); }); }
@Nullable public static Color getJavaColorFromExpression(@Nullable PsiElement element) { if(element instanceof PsiNewExpression) { final PsiNewExpression expr = (PsiNewExpression) element; if(isColorType(expr.getType())) { return getColor(expr.getArgumentList()); } } return null; }
@Override protected boolean shouldShowExplicitLambdaType(@NotNull PsiAnonymousClass anonymousClass, @NotNull PsiNewExpression expression) { PsiElement parent = expression.getParent(); if(parent instanceof PsiReferenceExpression || parent instanceof PsiAssignmentExpression) { return true; } ExpectedTypeInfo[] types = ExpectedTypesProvider.getExpectedTypes(expression, false); return types.length != 1 || !types[0].getType().equals(anonymousClass.getBaseClassType()); }