diff options
author | Tor Norbye <tnorbye@google.com> | 2013-05-06 10:36:42 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2013-05-06 10:36:42 -0700 |
commit | f56a0fff1a336635c966bffc25e16af9a4e6e988 (patch) | |
tree | 19e53b8f61e9fc94d35d5d6d97cc1499cad1cc59 /java/java-impl/src/com/intellij | |
parent | 8fb0021093e7d978cc06043ba4c06b0a47778294 (diff) | |
download | idea-f56a0fff1a336635c966bffc25e16af9a4e6e988.tar.gz |
Snapshot 36a7a0702ddda30083713c9b8f140495d5f09d32 from master branch of git://git.jetbrains.org/idea/community.git
Change-Id: Ia9068e36d373808400a123a395b037bdb6940a17
Diffstat (limited to 'java/java-impl/src/com/intellij')
8 files changed, 80 insertions, 50 deletions
diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java index 1e5d68fb2eca..3f9bf87694f3 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java @@ -893,6 +893,7 @@ public class GenericsHighlightUtil { return typeParameters[0]; } + //http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2 @Nullable public static HighlightInfo checkAccessStaticFieldFromEnumConstructor(PsiReferenceExpression expr, JavaResolveResult result) { final PsiElement resolved = result.getElement(); @@ -903,14 +904,17 @@ public class GenericsHighlightUtil { if (constructorOrInitializer == null) return null; if (constructorOrInitializer.hasModifierProperty(PsiModifier.STATIC)) return null; final PsiClass aClass = constructorOrInitializer.getContainingClass(); - if (aClass == null) return null; - if (!aClass.isEnum()) return null; + if (aClass == null || !(aClass.isEnum() || aClass instanceof PsiEnumConstantInitializer)) return null; final PsiField field = (PsiField)resolved; - if (field.getContainingClass() != aClass) return null; - final PsiType type = field.getType(); + if (aClass instanceof PsiEnumConstantInitializer) { + if (field.getContainingClass() != aClass.getSuperClass()) return null; + } else if (field.getContainingClass() != aClass) return null; + - //TODO is access to enum constant is allowed ? - if (type instanceof PsiClassType && ((PsiClassType)type).resolve() == aClass) return null; + if (!JavaVersionService.getInstance().isAtLeast(field, JavaSdkVersion.JDK_1_6)) { + final PsiType type = field.getType(); + if (type instanceof PsiClassType && ((PsiClassType)type).resolve() == aClass) return null; + } if (PsiUtil.isCompileTimeConstant(field)) return null; diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java index 2487c3d680ab..58e2acceefbe 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightClassUtil.java @@ -778,7 +778,7 @@ public class HighlightClassUtil { if (!PsiUtil.isInnerClass(base)) return; if (resolve == resolved && baseClass != null && (!PsiTreeUtil.isAncestor(baseClass, extendRef, true) || aClass.hasModifierProperty(PsiModifier.STATIC)) && - !hasEnclosingInstanceInScope(baseClass, extendRef, true, true) && !qualifiedNewCalledInConstructors(aClass, baseClass)) { + !hasEnclosingInstanceInScope(baseClass, extendRef, !aClass.hasModifierProperty(PsiModifier.STATIC), true) && !qualifiedNewCalledInConstructors(aClass, baseClass)) { String description = JavaErrorMessages.message("no.enclosing.instance.in.scope", HighlightUtil.formatClass(baseClass)); infos[0] = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(extendRef).descriptionAndTooltip(description).create(); } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java index 7a4ca788a22a..5a0f156c5bbd 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightControlFlowUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +41,7 @@ import java.util.*; /** * @author cdr - * Date: Aug 8, 2002 + * @since Aug 8, 2002 */ public class HighlightControlFlowUtil { private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance(); @@ -50,48 +50,42 @@ public class HighlightControlFlowUtil { @Nullable public static HighlightInfo checkMissingReturnStatement(PsiCodeBlock body, PsiType returnType) { - - if (body == null - || returnType == null - || PsiType.VOID.equals(returnType)) { + if (body == null || returnType == null || PsiType.VOID.equals(returnType.getDeepComponentType())) { return null; } + // do not compute constant expressions for if() statement condition // see JLS 14.20 Unreachable Statements try { - final ControlFlow controlFlow = getControlFlowNoConstantEvaluate(body); + ControlFlow controlFlow = getControlFlowNoConstantEvaluate(body); if (!ControlFlowUtil.returnPresent(controlFlow)) { - final PsiJavaToken rBrace = body.getRBrace(); - final PsiElement context = rBrace == null - ? body.getLastChild() - : rBrace; - String description = JavaErrorMessages.message("missing.return.statement"); - final HighlightInfo highlightInfo = - HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(context).descriptionAndTooltip(description).create(); - final PsiElement parent = body.getParent(); + PsiJavaToken rBrace = body.getRBrace(); + PsiElement context = rBrace == null ? body.getLastChild() : rBrace; + String message = JavaErrorMessages.message("missing.return.statement"); + HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(context).descriptionAndTooltip(message).create(); + PsiElement parent = body.getParent(); if (parent instanceof PsiMethod) { - final PsiMethod method = (PsiMethod)parent; - QuickFixAction.registerQuickFixAction(highlightInfo, new AddReturnFix(method)); + PsiMethod method = (PsiMethod)parent; + QuickFixAction.registerQuickFixAction(info, new AddReturnFix(method)); IntentionAction fix = QUICK_FIX_FACTORY.createMethodReturnFix(method, PsiType.VOID, true); - QuickFixAction.registerQuickFixAction(highlightInfo, fix); + QuickFixAction.registerQuickFixAction(info, fix); } - return highlightInfo; + return info; } } - catch (AnalysisCanceledException e) { - // incomplete code - } - return null; + catch (AnalysisCanceledException ignored) { } + return null; } - public static ControlFlow getControlFlowNoConstantEvaluate(final PsiElement body) throws AnalysisCanceledException { - return ControlFlowFactory.getInstance(body.getProject()).getControlFlow(body, - LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(), - false); + public static ControlFlow getControlFlowNoConstantEvaluate(PsiElement body) throws AnalysisCanceledException { + LocalsOrMyInstanceFieldsControlFlowPolicy policy = LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(); + return ControlFlowFactory.getInstance(body.getProject()).getControlFlow(body, policy, false); } - private static ControlFlow getControlFlow(final PsiElement context) throws AnalysisCanceledException { - return ControlFlowFactory.getInstance(context.getProject()).getControlFlow(context, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance()); + + private static ControlFlow getControlFlow(PsiElement context) throws AnalysisCanceledException { + LocalsOrMyInstanceFieldsControlFlowPolicy policy = LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(); + return ControlFlowFactory.getInstance(context.getProject()).getControlFlow(context, policy); } public static HighlightInfo checkUnreachableStatement(PsiCodeBlock codeBlock) { @@ -683,12 +677,12 @@ public class HighlightControlFlowUtil { if (innerClass != null) { if (variable instanceof PsiParameter) { final PsiElement parent = variable.getParent(); - if (parent instanceof PsiParameterList && parent.getParent() instanceof PsiLambdaExpression && + if (parent instanceof PsiParameterList && parent.getParent() instanceof PsiLambdaExpression && notAccessedForWriting(variable, new LocalSearchScope(((PsiParameter)variable).getDeclarationScope()))) { return null; } } - if (PsiUtil.getLanguageLevel(variable).isAtLeast(LanguageLevel.JDK_1_8) && + if (PsiUtil.getLanguageLevel(variable).isAtLeast(LanguageLevel.JDK_1_8) && isEffectivelyFinal(variable, innerClass, context)) { return null; } @@ -738,7 +732,7 @@ public class HighlightControlFlowUtil { } return effectivelyFinal; } - + private static boolean notAccessedForWriting(PsiVariable variable, final LocalSearchScope searchScope) { for (PsiReference reference : ReferencesSearch.search(variable, searchScope)) { final PsiElement element = reference.getElement(); diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java index 2098dd6bacaf..8fc4b7391b3c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -32,6 +32,8 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; +import com.intellij.openapi.projectRoots.JavaSdkVersion; +import com.intellij.openapi.projectRoots.JavaVersionService; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Condition; @@ -52,7 +54,10 @@ import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.templateLanguages.OuterLanguageElement; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.*; -import com.intellij.util.*; +import com.intellij.util.ArrayUtil; +import com.intellij.util.ArrayUtilRt; +import com.intellij.util.Function; +import com.intellij.util.IncorrectOperationException; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.UIUtil; import com.intellij.xml.util.XmlStringUtil; @@ -1806,10 +1811,10 @@ public class HighlightUtil extends HighlightUtilBase { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeElement).descriptionAndTooltip(description).create(); } } + return null; } - @Nullable public static HighlightInfo checkIllegalVoidType(@NotNull PsiKeyword type) { if (!PsiKeyword.VOID.equals(type.getText())) return null; @@ -1817,15 +1822,17 @@ public class HighlightUtil extends HighlightUtilBase { PsiElement parent = type.getParent(); if (parent instanceof PsiTypeElement) { PsiElement typeOwner = parent.getParent(); + if (typeOwner != null) { + // do not highlight incomplete declarations + if (PsiUtilCore.hasErrorElementChild(typeOwner)) return null; + } + if (typeOwner instanceof PsiMethod) { - if (((PsiMethod)typeOwner).getReturnTypeElement() == parent) return null; + PsiMethod method = (PsiMethod)typeOwner; + if (method.getReturnTypeElement() == parent && PsiType.VOID.equals(method.getReturnType())) return null; } - else if (// like in Class c = void.class; - typeOwner instanceof PsiClassObjectAccessExpression && - TypeConversionUtil.isVoidType(((PsiClassObjectAccessExpression)typeOwner).getOperand().getType()) || - // do not highlight incomplete declarations - typeOwner != null && PsiUtilCore.hasErrorElementChild(typeOwner)) { - return null; + else if (typeOwner instanceof PsiClassObjectAccessExpression) { + if (TypeConversionUtil.isVoidType(((PsiClassObjectAccessExpression)typeOwner).getOperand().getType())) return null; } else if (typeOwner instanceof JavaCodeFragment) { if (typeOwner.getUserData(PsiUtil.VALID_VOID_TYPE_IN_CODE_FRAGMENT) != null) return null; @@ -2002,6 +2009,7 @@ public class HighlightUtil extends HighlightUtilBase { @Nullable public static HighlightInfo checkImplicitThisReferenceBeforeSuper(@NotNull PsiClass aClass) { + if (JavaVersionService.getInstance().isAtLeast(aClass, JavaSdkVersion.JDK_1_7)) return null; if (aClass instanceof PsiAnonymousClass) return null; PsiClass superClass = aClass.getSuperClass(); if (superClass == null || !PsiUtil.isInnerClass(superClass)) return null; @@ -2636,6 +2644,19 @@ public class HighlightUtil extends HighlightUtilBase { return null; } + @Nullable + static HighlightInfo checkForStatement(@NotNull PsiForStatement statement) { + PsiStatement init = statement.getInitialization(); + if (!(init == null || init instanceof PsiEmptyStatement || + init instanceof PsiDeclarationStatement || + init instanceof PsiExpressionStatement || init instanceof PsiExpressionListStatement)) { + String message = JavaErrorMessages.message("invalid.statement"); + return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(init).descriptionAndTooltip(message).create(); + } + + return null; + } + private enum Feature { GENERICS(LanguageLevel.JDK_1_5, "feature.generics"), ANNOTATIONS(LanguageLevel.JDK_1_5, "feature.annotations"), diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java index 44b5431e8e08..f611668b576c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java @@ -469,6 +469,11 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh } @Override + public void visitForStatement(PsiForStatement statement) { + myHolder.add(HighlightUtil.checkForStatement(statement)); + } + + @Override public void visitForeachStatement(final PsiForeachStatement statement) { myHolder.add(HighlightUtil.checkForEachFeature(statement)); } diff --git a/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java b/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java index fdfb71688530..bc9195dc6ee7 100644 --- a/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspection.java @@ -425,7 +425,10 @@ public class NullableStuffInspection extends BaseLocalInspectionTool { private static LocalQuickFix createChangeDefaultNotNullFix(NullableNotNullManager nullableManager, PsiModifierListOwner modifierListOwner) { final PsiAnnotation annotation = AnnotationUtil.findAnnotation(modifierListOwner, nullableManager.getNotNulls()); if (annotation != null) { - return new ChangeNullableDefaultsFix(annotation.getQualifiedName(), null, nullableManager); + final PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement(); + if (referenceElement != null && referenceElement.resolve() != null) { + return new ChangeNullableDefaultsFix(annotation.getQualifiedName(), null, nullableManager); + } } return null; } diff --git a/java/java-impl/src/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java b/java/java-impl/src/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java index c9480d3d40d6..470c5d68513a 100644 --- a/java/java-impl/src/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryHandler.java @@ -112,7 +112,7 @@ public class ReplaceConstructorWithFactoryHandler final PsiMethod[] constructors = aClass.getConstructors(); if (constructors.length > 0) { String message = - RefactoringBundle.message("class.does.not.have.implicit.default.consructor", aClass.getQualifiedName()) ; + RefactoringBundle.message("class.does.not.have.implicit.default.constructor", aClass.getQualifiedName()) ; CommonRefactoringUtil.showErrorHint(myProject, editor, message, REFACTORING_NAME, HelpID.REPLACE_CONSTRUCTOR_WITH_FACTORY); return; } diff --git a/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java b/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java index b053945ddf6c..db3e95b83938 100644 --- a/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java +++ b/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java @@ -216,6 +216,9 @@ public class InlineUtil { PsiElement lastInitializerSibling = initializers[initializers.length - 1]; while (lastInitializerSibling != null) { final PsiElement nextSibling = lastInitializerSibling.getNextSibling(); + if (nextSibling == null) { + break; + } if (nextSibling.getNode().getElementType() == JavaTokenType.RBRACE) break; lastInitializerSibling = nextSibling; } |