private static boolean addSuperMethods(final HierarchicalMethodSignature signature, final PsiMethod method, final PsiClass parentClass, final boolean allowStaticMethod, final boolean checkBases, final Processor<MethodSignatureBackedByPsiMethod> consumer) { PsiMethod signatureMethod = signature.getMethod(); PsiClass hisClass = signatureMethod.getContainingClass(); if (parentClass == null || InheritanceUtil.isInheritorOrSelf(parentClass, hisClass, true)) { if (isAcceptable(signatureMethod, method, allowStaticMethod)) { if (parentClass != null && !parentClass.equals(hisClass) && !checkBases) { return true; } LOG.assertTrue(signatureMethod != method, method); // method != method.getsuper() return consumer.process(signature); //no need to check super classes } } for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) { if (MethodSignatureUtil.isSubsignature(superSignature, signature)) { addSuperMethods(superSignature, method, parentClass, allowStaticMethod, checkBases, consumer); } } return true; }
private static boolean checkForListIsEmpty(GrExpression condition, GrExpression elseBranch) { if (condition instanceof GrMethodCall) condition = ((GrMethodCall)condition).getInvokedExpression(); if (!(condition instanceof GrReferenceExpression)) return false; final GrExpression qualifier = ((GrReferenceExpression)condition).getQualifier(); if (qualifier == null) return false; if (!PsiEquivalenceUtil.areElementsEquivalent(qualifier, elseBranch)) return false; final PsiType type = qualifier.getType(); if (type == null) return false; if (!InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_LIST)) return false; final PsiElement resolved = ((GrReferenceExpression)condition).resolve(); return resolved instanceof PsiMethod && "isEmpty".equals(((PsiMethod)resolved).getName()) && ((PsiMethod)resolved).getParameterList().getParametersCount() == 0; }
public static Location getParameterizedLocation(PsiClass psiClass, String paramSetName, String parameterizedClassName) { final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(psiClass, Collections.singleton(JUnitUtil.RUN_WITH)); if (annotation != null) { final PsiAnnotationMemberValue attributeValue = annotation.findAttributeValue("value"); if (attributeValue instanceof PsiClassObjectAccessExpression) { final PsiTypeElement operand = ((PsiClassObjectAccessExpression)attributeValue).getOperand(); if (InheritanceUtil.isInheritor(operand.getType(), parameterizedClassName)) { return new PsiMemberParameterizedLocation(psiClass.getProject(), psiClass, null, paramSetName); } } } return null; }
private static boolean encodeQualifier(PsiClass containingClass, PsiReference reference, PsiClass targetClass) { if (reference instanceof PsiReferenceExpression) { final PsiElement qualifier = ((PsiReferenceExpression)reference).getQualifier(); if (qualifier != null) { if (qualifier instanceof PsiReferenceExpression) { final PsiElement resolved = ((PsiReferenceExpression)qualifier).resolve(); if (resolved == containingClass || resolved instanceof PsiClass && InheritanceUtil.isInheritorOrSelf(targetClass, (PsiClass)resolved, true)) { return true; } } qualifier.putCopyableUserData(ChangeContextUtil.CAN_REMOVE_QUALIFIER_KEY, ChangeContextUtil.canRemoveQualifier((PsiReferenceExpression)reference)); } } return false; }
private static boolean isClassHasConstructorWithMap(PsiClass aClass) { PsiMethod[] constructors = aClass.getConstructors(); if (constructors.length == 0) return true; for (PsiMethod constructor : constructors) { PsiParameterList parameterList = constructor.getParameterList(); PsiParameter[] parameters = parameterList.getParameters(); if (parameters.length == 0) return true; final PsiParameter first = parameters[0]; if (InheritanceUtil.isInheritor(first.getType(), CommonClassNames.JAVA_UTIL_MAP)) return true; if (first instanceof GrParameter && ((GrParameter)first).getTypeGroovy() == null) return true; //if constructor has only optional parameters it can be used as default constructor with map args if (!PsiUtil.isConstructorHasRequiredParameters(constructor)) return true; } return false; }
private static boolean isMessageFormatCall(PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String referenceName = methodExpression.getReferenceName(); if (!"format".equals(referenceName)) { return false; } final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); if (!(qualifierExpression instanceof PsiReferenceExpression)) { return false; } final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifierExpression; final PsiElement target = referenceExpression.resolve(); if (!(target instanceof PsiClass)) { return false; } final PsiClass aClass = (PsiClass)target; return InheritanceUtil.isInheritor(aClass, "java.text.MessageFormat"); }
private static boolean isUselessObjectMethod(PsiMethod method, PsiMethod parentMethod, PsiType qualifierType) { if (!OBJECT_METHOD_PATTERN.accepts(method)) { return false; } if (OBJECT_METHOD_PATTERN.accepts(parentMethod) && method.getName().equals(parentMethod.getName())) { return false; } if ("toString".equals(method.getName())) { if (qualifierType.equalsToText(CommonClassNames.JAVA_LANG_STRING_BUFFER) || InheritanceUtil.isInheritor(qualifierType, CommonClassNames.JAVA_LANG_ABSTRACT_STRING_BUILDER)) { return false; } } return true; }
@Override public boolean isApplicable(@NotNull PsiElement element, @NotNull Document copyDocument, int newOffset) { if (!PsiUtil.isLanguageLevel7OrHigher(element)) return false; PsiExpression initializer = JavaPostfixTemplatesUtils.getTopmostExpression(element); if (initializer == null) return false; final PsiType type = initializer.getType(); if (!(type instanceof PsiClassType)) return false; final PsiClass aClass = ((PsiClassType)type).resolve(); Project project = element.getProject(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final PsiClass autoCloseable = facade.findClass(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, ProjectScope.getLibrariesScope(project)); if (!InheritanceUtil.isInheritorOrSelf(aClass, autoCloseable, true)) return false; return true; }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { if (!element.getLanguage().isKindOf(JavaLanguage.INSTANCE)) return false; if (!PsiUtil.getLanguageLevel(element).isAtLeast(LanguageLevel.JDK_1_7)) return false; final PsiLocalVariable variable = PsiTreeUtil.getParentOfType(element, PsiLocalVariable.class); if (variable == null) return false; final PsiExpression initializer = variable.getInitializer(); if (initializer == null) return false; final PsiElement declaration = variable.getParent(); if (!(declaration instanceof PsiDeclarationStatement)) return false; final PsiElement codeBlock = declaration.getParent(); if (!(codeBlock instanceof PsiCodeBlock)) return false; return InheritanceUtil.isInheritor(variable.getType(), CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE); }
@Override public boolean isAcceptable(Object element, PsiElement context) { if (element instanceof PsiMethodCallExpression) { final PsiMethodCallExpression callExpression = (PsiMethodCallExpression)element; final PsiMethod psiMethod = callExpression.resolveMethod(); if (psiMethod != null) { if (!myMethodNames.contains(psiMethod.getName())) { return false; } final PsiClass psiClass = psiMethod.getContainingClass(); final PsiClass expectedClass = JavaPsiFacade.getInstance(psiClass.getProject()).findClass(myClassName, psiClass.getResolveScope()); return InheritanceUtil.isInheritorOrSelf(psiClass, expectedClass, true); } } return false; }
protected boolean isResourceCreation(PsiExpression expression) { if (!(expression instanceof PsiMethodCallExpression)) { return false; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expression; final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final String name = methodExpression.getReferenceName(); if (name == null || !creationMethodNameSet.contains(name)) { return false; } final PsiMethod method = methodCallExpression.resolveMethod(); if (method == null) { return false; } for (int i = 0; i < creationMethodName.length; i++) { if (!name.equals(creationMethodName[i])) { continue; } final PsiClass containingClass = method.getContainingClass(); final String expectedClassName = creationMethodClassName[i]; if (InheritanceUtil.isInheritor(containingClass, false, expectedClassName)) { return true; } } return false; }
protected boolean isInSuper(PsiElement member) { if (!(member instanceof PsiMember)) return false; final PsiManager manager = member.getManager(); if (InheritanceUtil.isInheritorOrSelf(mySuper, ((PsiMember)member).getContainingClass(), true)) return true; if (member instanceof PsiField) { final PsiClass containingClass = ((PsiField) member).getContainingClass(); LanguageLevel languageLevel = PsiUtil.getLanguageLevel(member); if (manager.areElementsEquivalent(containingClass, JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().getArrayClass(languageLevel))) { return true; } } else if (member instanceof PsiMethod) { return mySuper.findMethodBySignature((PsiMethod) member, true) != null; } return false; }
public static boolean variableHasTypeOrSubtype(@Nullable PsiVariable variable, @NonNls @NotNull String... typeNames) { if (variable == null) { return false; } final PsiType type = variable.getType(); if (!(type instanceof PsiClassType)) { return false; } final PsiClassType classType = (PsiClassType)type; final PsiClass aClass = classType.resolve(); if (aClass == null) { return false; } for (String typeName : typeNames) { if (InheritanceUtil.isInheritor(aClass, typeName)) { return true; } } return false; }
@Override public void visitField(PsiField field) { super.visitField(field); final PsiType type = field.getType(); if (!(type instanceof PsiClassType)) { return; } final PsiClassType classType = (PsiClassType)type; final PsiClass aClass = classType.resolve(); if (aClass == null) { return; } if (!InheritanceUtil.isInheritor(aClass, "java.lang.ThreadLocal")) { return; } final PsiModifierList modifierList = field.getModifierList(); if (modifierList == null) { return; } if (modifierList.hasModifierProperty(PsiModifier.STATIC) && modifierList.hasModifierProperty(PsiModifier.FINAL)) { return; } registerFieldError(field, field); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super final PsiClass aClass = method.getContainingClass(); @NonNls final String methodName = method.getName(); if (!"setup".equals(methodName)) { return; } if (aClass == null) { return; } if (!InheritanceUtil.isInheritor(aClass, "junit.framework.TestCase")) { return; } registerMethodError(method); }
private static boolean hasJavaLangErrorType(PsiType type) { if (type instanceof PsiDisjunctionType) { final PsiDisjunctionType disjunctionType = (PsiDisjunctionType)type; for (PsiType disjunction : disjunctionType.getDisjunctions()) { if (hasJavaLangErrorType(disjunction)) { return true; } } } else if (type instanceof PsiClassType) { final PsiClassType classType = (PsiClassType)type; final PsiClass aClass = classType.resolve(); if (InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_LANG_ERROR) && !InheritanceUtil.isInheritor(aClass, "java.lang.ThreadDeath")) { return true; } } return false; }
private boolean isJdk6Override(PsiMethod method, PsiClass methodClass) { final PsiMethod[] superMethods = method.findSuperMethods(); boolean hasSupers = false; for (PsiMethod superMethod : superMethods) { final PsiClass superClass = superMethod.getContainingClass(); if (!InheritanceUtil.isInheritorOrSelf(methodClass, superClass, true)) { continue; } hasSupers = true; if (!superMethod.hasModifierProperty(PsiModifier.PROTECTED)) { return true; } } // is override except if this is an interface method // overriding a protected method in java.lang.Object // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6501053 return hasSupers && !methodClass.isInterface(); }
private boolean isJdk5Override(PsiMethod method, PsiClass methodClass) { final PsiMethod[] superMethods = method.findSuperMethods(); for (PsiMethod superMethod : superMethods) { final PsiClass superClass = superMethod.getContainingClass(); if (superClass == null || !InheritanceUtil.isInheritorOrSelf(methodClass, superClass, true)) { continue; } if (superClass.isInterface()) { continue; } if (methodClass.isInterface() && superMethod.hasModifierProperty(PsiModifier.PROTECTED)) { // only true for J2SE java.lang.Object.clone(), but might // be different on other/newer java platforms continue; } return true; } return false; }
@NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { final XmlAttributeValue attributeValue = (XmlAttributeValue)element; final PsiElement parent = attributeValue.getParent(); if (parent instanceof XmlAttribute) { final XmlAttributeDescriptor descriptor = ((XmlAttribute)parent).getDescriptor(); if (descriptor instanceof JavaFxPropertyAttributeDescriptor) { final PsiElement declaration = descriptor.getDeclaration(); if (declaration instanceof PsiField) { final PsiField field = (PsiField)declaration; final PsiClassType propertyClassType = JavaFxPsiUtil.getPropertyClassType(field); if (propertyClassType != null && InheritanceUtil.isInheritor(propertyClassType, JavaFxCommonClassNames.JAVAFX_SCENE_PAINT)) { return new PsiReference[] {new JavaFxColorReference(attributeValue)}; } } } } return PsiReference.EMPTY_ARRAY; }
private boolean isListNonGetMethodCall( PsiReferenceExpression reference) { final PsiElement parent = reference.getParent(); if (!(parent instanceof PsiMethodCallExpression)) { return false; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)parent; final PsiMethod method = methodCallExpression.resolveMethod(); if (method == null) { return false; } final PsiClass parentClass = PsiTreeUtil.getParentOfType( methodCallExpression, PsiClass.class); final PsiClass containingClass = method.getContainingClass(); if (!InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) { return false; } return !isListGetExpression(methodCallExpression); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super @NonNls final String methodName = method.getName(); if (!"suite".equals(methodName)) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } if (!InheritanceUtil.isInheritor(aClass, "junit.framework.TestCase")) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } if (method.hasModifierProperty(PsiModifier.STATIC)) { return; } registerMethodError(method); }
public static boolean isSketchClass(PsiClass klass) { if (klass.getQualifiedName() == null) { return false; } // klass.isInheritor(myBase, true) && ConfigurationUtil.PUBLIC_INSTANTIATABLE_CLASS.value(klass) // @TODO This would only find Processing 3 PApplet classes. Investigate handling Processing 2.0. return InheritanceUtil.isInheritor(klass, "processing.core.PApplet") && ConfigurationUtil.PUBLIC_INSTANTIATABLE_CLASS.value(klass); }
@Override public void visitMethod(@NotNull PsiMethod method) { if (MethodUtils.isCompareTo(method) && InheritanceUtil.isInheritor(method.getContainingClass(), false, CommonClassNames.JAVA_LANG_COMPARABLE)) { checkUsedNonFinalFields(method); } }
public static PsiReferenceExpression qualifyReference(@NotNull PsiReferenceExpression referenceExpression, @NotNull PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException { PsiManager manager = referenceExpression.getManager(); PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(referenceExpression, PsiMethodCallExpression.class, true); while (methodCallExpression != null) { if (isSuperOrThisMethodCall(methodCallExpression)) { return referenceExpression; } methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression.class, true); } PsiReferenceExpression expressionFromText; final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); if (qualifyingClass == null) { PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class); final PsiClass containingClass = member.getContainingClass(); if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) { while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) { parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true); } LOG.assertTrue(parentClass != null); expressionFromText = (PsiReferenceExpression)factory.createExpressionFromText("A.this." + member.getName(), null); ((PsiThisExpression)expressionFromText.getQualifierExpression()).getQualifier().replace(factory.createClassReferenceElement(parentClass)); } else { expressionFromText = (PsiReferenceExpression)factory.createExpressionFromText("this." + member.getName(), null); } } else { expressionFromText = (PsiReferenceExpression)factory.createExpressionFromText("A." + member.getName(), null); expressionFromText.setQualifierExpression(factory.createReferenceExpression(qualifyingClass)); } CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); expressionFromText = (PsiReferenceExpression)codeStyleManager.reformat(expressionFromText); return (PsiReferenceExpression)referenceExpression.replace(expressionFromText); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final PsiReferenceExpression methodExpression = expression.getMethodExpression(); @NonNls final String methodName = methodExpression.getReferenceName(); if (!"sleep".equals(methodName)) { return; } final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class, PsiLambdaExpression.class); boolean isSynced = false; if (containingMethod != null && containingMethod.hasModifierProperty(PsiModifier.SYNCHRONIZED)) { isSynced = true; } final PsiSynchronizedStatement containingSyncStatement = PsiTreeUtil.getParentOfType(expression, PsiSynchronizedStatement.class, true, PsiClass.class, PsiLambdaExpression.class); if (containingSyncStatement != null) { isSynced = true; } if (!isSynced) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiClass methodClass = method.getContainingClass(); if (methodClass == null || !InheritanceUtil.isInheritor(methodClass, "java.lang.Thread")) { return; } registerMethodCallError(expression); }
@PsiModifier.ModifierConstant public static String getPossibleVisibility(final PsiMember psiMethod, final PsiElement place) { Project project = psiMethod.getProject(); if (PsiUtil.isAccessible(project, psiMethod, place, null)) return getVisibilityModifier(psiMethod.getModifierList()); if (JavaPsiFacade.getInstance(project).arePackagesTheSame(psiMethod, place)) { return PsiModifier.PACKAGE_LOCAL; } if (InheritanceUtil.isInheritorOrSelf(PsiTreeUtil.getParentOfType(place, PsiClass.class), psiMethod.getContainingClass(), true)) { return PsiModifier.PROTECTED; } return PsiModifier.PUBLIC; }
@Override public void visitMethodCallExpression(GrMethodCallExpression grMethodCallExpression) { super.visitMethodCallExpression(grMethodCallExpression); final GrArgumentList args = grMethodCallExpression.getArgumentList(); if (args == null) { return; } if (args.getExpressionArguments().length != 2) { return; } if (PsiImplUtil.hasNamedArguments(args)) { return; } final GrExpression methodExpression = grMethodCallExpression.getInvokedExpression(); if (!(methodExpression instanceof GrReferenceExpression)) { return; } final GrReferenceExpression referenceExpression = (GrReferenceExpression) methodExpression; final String name = referenceExpression.getReferenceName(); if (!"put".equals(name)) { return; } final GrExpression qualifier = referenceExpression.getQualifierExpression(); if (qualifier == null || PsiUtil.isThisOrSuperRef(qualifier)) { return; } if (referenceExpression.getDotTokenType() == GroovyTokenTypes.mOPTIONAL_DOT) return; final PsiType type = qualifier.getType(); if (!InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP)) { return; } registerMethodCallError(grMethodCallExpression); }
private void processThrowsTags(@NotNull PsiDocTag[] tags, @NotNull Map<PsiClassType, PsiClass> declaredExceptions, @NotNull InspectionManager manager, @NotNull final List<ProblemDescriptor> problems, boolean isOnTheFly) { for (PsiDocTag tag : tags) { if ("throws".equals(tag.getName()) || "exception".equals(tag.getName())) { final PsiDocTagValue value = tag.getValueElement(); if (value == null) continue; final PsiElement firstChild = value.getFirstChild(); if (firstChild == null) continue; final PsiElement psiElement = firstChild.getFirstChild(); if (!(psiElement instanceof PsiJavaCodeReferenceElement)) continue; final PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement)psiElement; final PsiElement element = ref.resolve(); if (element instanceof PsiClass){ final PsiClass exceptionClass = (PsiClass)element; for (Iterator<PsiClassType> it = declaredExceptions.keySet().iterator(); it.hasNext();) { PsiClassType classType = it.next(); final PsiClass psiClass = declaredExceptions.get(classType); if (InheritanceUtil.isInheritorOrSelf(exceptionClass, psiClass, true)) { if (!myIgnoreEmptyDescriptions && extractThrowsTagDescription(tag).isEmpty()) { problems.add(createDescriptor(tag.getNameElement(), InspectionsBundle.message("inspection.javadoc.method.problem.missing.tag.description", "<code>" + tag.getName() + "</code>"), manager, isOnTheFly)); } it.remove(); } } } } } }
@NotNull @Override public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) { return new XmlElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { DomElement element = DomUtil.getDomElement(tag); if (element instanceof Extension) { ExtensionPoint extensionPoint = ((Extension)element).getExtensionPoint(); if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), "com.intellij.codeInspection.InspectionEP")) { boolean key = tag.getAttribute("key") != null; boolean groupKey = tag.getAttribute("groupKey") != null; if (key) { if (tag.getAttribute("bundle") == null) { checkDefaultBundle(element, holder); } } else if (tag.getAttribute("displayName") == null) { registerProblem(element, holder, "displayName or key should be specified", "displayName", "key"); } if (groupKey) { if (tag.getAttribute("bundle") == null && tag.getAttribute("groupBundle") == null) { checkDefaultBundle(element, holder); } } else if (tag.getAttribute("groupName") == null) { registerProblem(element, holder, "groupName or groupKey should be specified", "groupName", "groupKey"); } } } } }; }
boolean registerInstanceExpression(PsiExpression instanceExpression, final PsiClass contextClass) { if (myInstanceExpression == null) { if (instanceExpression != null) { final PsiType type = instanceExpression.getType(); if (!(type instanceof PsiClassType)) return false; final PsiClass hisClass = ((PsiClassType) type).resolve(); if (hisClass == null || !InheritanceUtil.isInheritorOrSelf(hisClass, contextClass, true)) return false; } myInstanceExpression = Ref.create(instanceExpression); return true; } else { if (myInstanceExpression.get() == null) { myInstanceExpression.set(instanceExpression); return instanceExpression == null; } else { if (instanceExpression != null) { return PsiEquivalenceUtil.areElementsEquivalent(instanceExpression, myInstanceExpression.get()); } else { return myInstanceExpression.get() == null || myInstanceExpression.get() instanceof PsiThisExpression; } } } }
public static boolean isSuiteMethod(@NotNull PsiMethod psiMethod) { if (!psiMethod.hasModifierProperty(PsiModifier.PUBLIC)) return false; if (!psiMethod.hasModifierProperty(PsiModifier.STATIC)) return false; if (psiMethod.isConstructor()) return false; if (psiMethod.getParameterList().getParametersCount() > 0) return false; final PsiType returnType = psiMethod.getReturnType(); if (returnType == null || returnType instanceof PsiPrimitiveType) return false; return returnType.equalsToText(TEST_INTERFACE)|| returnType.equalsToText(TESTSUITE_CLASS) || InheritanceUtil.isInheritor(returnType, TEST_INTERFACE); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super; @NonNls final String methodName = method.getName(); if (!myMethodName.equals(methodName)) { return; } if (method.hasModifierProperty(PsiModifier.ABSTRACT)) { return; } if (method.getBody() == null) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } final PsiClass targetClass = method.getContainingClass(); if (targetClass == null) { return; } if (!InheritanceUtil.isInheritor(targetClass, "junit.framework.TestCase")) { return; } if (MethodCallUtils.containsSuperMethodCall(myMethodName, method)) { return; } registerMethodError(method); }
@Override public void visitForeachStatement(@NotNull PsiForeachStatement statement) { super.visitForeachStatement(statement); final PsiExpression iteratedValue = statement.getIteratedValue(); if (iteratedValue == null || !InheritanceUtil.isInheritor(iteratedValue.getType(), CommonClassNames.JAVA_LANG_ITERABLE)) { return; } registerStatementError(statement); }
@Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); if (!MethodCallUtils.isCallDuringObjectConstruction(expression)) { return; } final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); if (qualifier != null) { if (!(qualifier instanceof PsiThisExpression || qualifier instanceof PsiSuperExpression)) { return; } } final PsiClass containingClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class); if (containingClass == null || containingClass.hasModifierProperty(PsiModifier.FINAL)) { return; } final PsiMethod calledMethod = expression.resolveMethod(); if (calledMethod == null || !PsiUtil.canBeOverriden(calledMethod)) { return; } final PsiClass calledMethodClass = calledMethod.getContainingClass(); if (!InheritanceUtil.isInheritorOrSelf(containingClass, calledMethodClass, true)) { return; } if (!MethodUtils.isOverriddenInHierarchy(calledMethod, containingClass)) { return; } if (CloneUtils.isClone(calledMethod)) { final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class); if (CloneUtils.isClone(containingMethod)) { return; } } registerMethodCallError(expression); }
@Override @NotNull public PsiElementVisitor buildInternalVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) { return new JavaElementVisitor() { @Override public void visitMethodCallExpression(final PsiMethodCallExpression expression) { super.visitMethodCallExpression(expression); final Project project = expression.getProject(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final PsiElement target = expression.getMethodExpression().resolve(); if (!(target instanceof PsiMethod)) return; final PsiMethod method = (PsiMethod)target; if (!SETTER_METHOD_NAME.equals(method.getName())) return; final PsiClass aClass = ((PsiMethod)target).getContainingClass(); final PsiClass comboClass = facade.findClass(COMBO_BOX_CLASS_NAME, GlobalSearchScope.allScope(project)); if (!InheritanceUtil.isInheritorOrSelf(aClass, comboClass, true)) return; final PsiExpression[] arguments = expression.getArgumentList().getExpressions(); if (arguments.length != 1) return; final PsiType type = arguments[0].getType(); if (!(type instanceof PsiClassType)) return; final PsiClass rendererClass = ((PsiClassType)type).resolve(); for (String rightClassName : RIGHT_RENDERER_CLASS_NAMES) { final PsiClass rightClass = facade.findClass(rightClassName, GlobalSearchScope.allScope(project)); if (InheritanceUtil.isInheritorOrSelf(rendererClass, rightClass, true)) return; } holder.registerProblem(expression, MESSAGE); } }; }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super; @NonNls final String methodName = method.getName(); if (!"setUp".equals(methodName)) { return; } final PsiType returnType = method.getReturnType(); if (returnType == null) { return; } final PsiClass targetClass = method.getContainingClass(); if (targetClass == null) { return; } if (!InheritanceUtil.isInheritor(targetClass, "junit.framework.TestCase")) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0 || !returnType.equals(PsiType.VOID) || !method.hasModifierProperty(PsiModifier.PUBLIC) && !method.hasModifierProperty(PsiModifier.PROTECTED)) { registerMethodError(method); } }
@Override public void visitClass(@NotNull PsiClass aClass) { //don't call super, to prevent drilldown if (FileTypeUtils.isInServerPageFile(aClass.getContainingFile())) { return; } if (aClass.isInterface() || aClass.isEnum() || aClass.isAnnotationType()) { return; } if (aClass instanceof PsiTypeParameter) { return; } final PsiMethod[] constructors = aClass.getConstructors(); if (constructors.length > 0) { return; } final PsiMethod[] methods = aClass.getMethods(); if (methods.length > 0) { return; } final PsiField[] fields = aClass.getFields(); if (fields.length > 0) { return; } final PsiClassInitializer[] initializers = aClass.getInitializers(); if (initializers.length > 0) { return; } if (ignoreClassWithParameterization && isSuperParametrization(aClass)) { return; } if (AnnotationUtil.isAnnotated(aClass, ignorableAnnotations)) { return; } if (ignoreThrowables && InheritanceUtil.isInheritor(aClass, "java.lang.Throwable")) { return; } registerClassError(aClass, aClass); }
private static boolean methodMatches(@NotNull PsiMethod method, @NonNls @Nullable String containingClassName, @Nullable PsiType returnType, @Nullable PsiType... parameterTypes) { if (parameterTypes != null) { final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != parameterTypes.length) { return false; } final PsiParameter[] parameters = parameterList.getParameters(); for (int i = 0; i < parameters.length; i++) { final PsiParameter parameter = parameters[i]; final PsiType type = parameter.getType(); final PsiType parameterType = parameterTypes[i]; if (PsiType.NULL.equals(parameterType)) { continue; } if (parameterType != null && !EquivalenceChecker.typesAreEquivalent(type, parameterType)) { return false; } } } if (returnType != null) { final PsiType methodReturnType = method.getReturnType(); if (!EquivalenceChecker.typesAreEquivalent(returnType, methodReturnType)) { return false; } } if (containingClassName != null) { final PsiClass containingClass = method.getContainingClass(); return InheritanceUtil.isInheritor(containingClass, containingClassName); } return true; }
@Override public void visitNewExpression( @NotNull PsiNewExpression newExpression) { super.visitNewExpression(newExpression); final PsiType type = newExpression.getType(); if (type == null) { return; } if (!(type instanceof PsiClassType)) { return; } final PsiClass resolveClass = ((PsiClassType)type).resolve(); if (resolveClass == null) { return; } if (resolveClass.isEnum() || resolveClass.isInterface() || resolveClass.isAnnotationType()) { return; } if (resolveClass instanceof PsiTypeParameter) { return; } if (!InheritanceUtil.isInheritor(resolveClass, "java.awt.peer.ComponentPeer")) { return; } registerNewExpressionError(newExpression); }
@Override public void visitElement(PsiElement element) { if (!(element instanceof PsiClass)) { super.visitElement(element); return; } final PsiClass aClass = (PsiClass)element; if (aClass.isInterface() || aClass.isEnum() || aClass.isAnnotationType()) { return; } if (aClass instanceof PsiTypeParameter) { return; } if (!aClass.hasModifierProperty(PsiModifier.ABSTRACT)) { return; } if (!InheritanceUtil.isInheritor(aClass, "junit.framework.TestCase")) { return; } final String name = aClass.getName(); if (name == null) { return; } if (isValid(name)) { return; } registerClassError(aClass, name); }