diff options
| author | Stephan Herrmann | 2014-07-16 17:17:21 +0000 |
|---|---|---|
| committer | Stephan Herrmann | 2014-07-22 13:03:21 +0000 |
| commit | 6dc3139df48b05a697942ac75c097b0346555e03 (patch) | |
| tree | 5c32be63a8c91ef0056b427ec980a3cf9b8050d4 | |
| parent | 0d60094dd9c1c614c9f260f13ccbc556ab13b245 (diff) | |
| download | eclipse.jdt.core-6dc3139df48b05a697942ac75c097b0346555e03.tar.gz eclipse.jdt.core-6dc3139df48b05a697942ac75c097b0346555e03.tar.xz eclipse.jdt.core-6dc3139df48b05a697942ac75c097b0346555e03.zip | |
Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect
"throws E"
10 files changed, 98 insertions, 5 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java index 4b92fecdaa..8b4f5aa17a 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java @@ -5383,6 +5383,19 @@ public void testTypeVariable7err() { "Null type safety (type annotations): The expression of type \'String\' needs unchecked conversion to conform to \'@NonNull String\'\n" + "----------\n"); } +//Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" +public void testTypeVariable8() { + runConformTestWithLibs( + new String[] { + "Test.java", + "@org.eclipse.jdt.annotation.NonNullByDefault\n" + + "public class Test<E extends Exception> {\n" + + " void test() throws E {}\n" + // was: Nullness annotations are not applicable at this location + "}\n" + }, + getCompilerOptions(), + ""); +} // Bug 439516 - [1.8][null] NonNullByDefault wrongly applied to implicit type bound of binary type public void testTypeVariable10() { runConformTestWithLibs( diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java index cf59548237..d5213d3d6a 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AbstractMethodDeclaration.java @@ -21,6 +21,7 @@ * Bug 403216 - [1.8][null] TypeReference#captureTypeAnnotations treats type annotations as type argument annotations * Bug 417295 - [1.8[[null] Massage type annotated null analysis to gel well with deep encoded type bindings. * Bug 392238 - [1.8][compiler][null] Detect semantically invalid null type annotations + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -585,7 +586,7 @@ public abstract class AbstractMethodDeclaration this.scope.problemReporter().illegalTypeForExplicitThis(this.receiver, enclosingReceiver); } - if (resolvedReceiverType.hasNullTypeAnnotations()) { + if (this.receiver.type.hasNullTypeAnnotation()) { this.scope.problemReporter().nullAnnotationUnsupportedLocation(this.receiver.type); } } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Argument.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Argument.java index 6ca22713cf..669bde1421 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Argument.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Argument.java @@ -12,6 +12,7 @@ * bug 365519 - editorial cleanup after bug 186342 and bug 365387 * Bug 417295 - [1.8[[null] Massage type annotated null analysis to gel well with deep encoded type bindings. * Bug 392238 - [1.8][compiler][null] Detect semantically invalid null type annotations + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" * Andy Clement (GoPivotal, Inc) aclement@gopivotal.com - Contributions for * Bug 409246 - [1.8][compiler] Type annotations on catch parameters not handled properly *******************************************************************************/ @@ -196,7 +197,9 @@ public class Argument extends LocalDeclaration { } resolveAnnotations(scope, this.annotations, this.binding, true); Annotation.isTypeUseCompatible(this.type, scope, this.annotations); - if (this.type.resolvedType != null && this.type.resolvedType.hasNullTypeAnnotations()) { + if (scope.compilerOptions().isAnnotationBasedNullAnalysisEnabled && + (this.type.hasNullTypeAnnotation() || TypeReference.containsNullAnnotation(this.annotations))) + { scope.problemReporter().nullAnnotationUnsupportedLocation(this.type); } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ArrayTypeReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ArrayTypeReference.java index 05136255a6..5ea23f7166 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ArrayTypeReference.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ArrayTypeReference.java @@ -9,6 +9,7 @@ * IBM Corporation - initial API and implementation * Stephan Herrmann - Contribution for * Bug 429958 - [1.8][null] evaluate new DefaultLocation attribute of @NonNullByDefault + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -179,4 +180,20 @@ public class ArrayTypeReference extends SingleTypeReference { TypeBinding internalResolveType = super.internalResolveType(scope, location); return internalResolveType; } + + @Override + public boolean hasNullTypeAnnotation() { + if (super.hasNullTypeAnnotation()) + return true; + if (this.resolvedType != null && !this.resolvedType.hasNullTypeAnnotations()) + return false; // shortcut + if (this.annotationsOnDimensions != null) { + for (int i = 0; i < this.annotationsOnDimensions.length; i++) { + Annotation[] innerAnnotations = this.annotationsOnDimensions[i]; + if (containsNullAnnotation(innerAnnotations)) + return true; + } + } + return false; + } } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java index a144e3a6ce..b1488d0598 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java @@ -14,6 +14,7 @@ * Bug 392099 - [1.8][compiler][null] Apply null annotation on types for null analysis * Bug 417295 - [1.8[[null] Massage type annotated null analysis to gel well with deep encoded type bindings. * Bug 392238 - [1.8][compiler][null] Detect semantically invalid null type annotations + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" * Andy Clement - Contributions for * Bug 383624 - [1.8][compiler] Revive code generation support for type annotations (from Olivier's work) *******************************************************************************/ @@ -89,7 +90,7 @@ public TypeBinding resolveType(BlockScope scope) { this.constant = Constant.NotAConstant; TypeBinding expressionType = this.expression.resolveType(scope); TypeBinding checkedType = this.type.resolveType(scope, true /* check bounds*/); - if (expressionType != null && checkedType != null && checkedType.hasNullTypeAnnotations()) { + if (expressionType != null && checkedType != null && this.type.hasNullTypeAnnotation()) { // don't complain if the entire operation is redundant anyway if (!expressionType.isCompatibleWith(checkedType) || NullAnnotationMatching.analyse(checkedType, expressionType, -1).isAnyMismatch()) scope.problemReporter().nullAnnotationUnsupportedLocation(this.type); diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedQualifiedTypeReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedQualifiedTypeReference.java index ea156176cc..f907238848 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedQualifiedTypeReference.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedQualifiedTypeReference.java @@ -14,6 +14,7 @@ * Bug 416181 - [1.8][compiler][null] Invalid assignment is not rejected by the compiler * Bug 429958 - [1.8][null] evaluate new DefaultLocation attribute of @NonNullByDefault * Bug 434600 - Incorrect null analysis error reporting on type parameters + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" * Andy Clement - Contributions for * Bug 383624 - [1.8][compiler] Revive code generation support for type annotations (from Olivier's work) *******************************************************************************/ @@ -98,6 +99,24 @@ public class ParameterizedQualifiedTypeReference extends ArrayQualifiedTypeRefer return true; } + @Override + public boolean hasNullTypeAnnotation() { + if (super.hasNullTypeAnnotation()) + return true; + if (this.resolvedType != null && !this.resolvedType.hasNullTypeAnnotations()) + return false; // shortcut + if (this.typeArguments != null) { + for (int i = 0; i < this.typeArguments.length; i++) { + TypeReference[] arguments = this.typeArguments[i]; + for (int j = 0; j < arguments.length; j++) { + if (arguments[i].hasNullTypeAnnotation()) + return true; + } + } + } + return false; + } + /** * @return char[][] */ diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedSingleTypeReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedSingleTypeReference.java index 5fad2610a9..25e8b8a16c 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedSingleTypeReference.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ParameterizedSingleTypeReference.java @@ -14,6 +14,7 @@ * Bug 415043 - [1.8][null] Follow-up re null type annotations after bug 392099 * Bug 429958 - [1.8][null] evaluate new DefaultLocation attribute of @NonNullByDefault * Bug 434600 - Incorrect null analysis error reporting on type parameters + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" * Andy Clement - Contributions for * Bug 383624 - [1.8][compiler] Revive code generation support for type annotations (from Olivier's work) *******************************************************************************/ @@ -119,6 +120,21 @@ public class ParameterizedSingleTypeReference extends ArrayTypeReference { return true; } + @Override + public boolean hasNullTypeAnnotation() { + if (super.hasNullTypeAnnotation()) + return true; + if (this.resolvedType != null && !this.resolvedType.hasNullTypeAnnotations()) + return false; // shortcut + if (this.typeArguments != null) { + for (int i = 0; i < this.typeArguments.length; i++) { + if (this.typeArguments[i].hasNullTypeAnnotation()) + return true; + } + } + return false; + } + /* * No need to check for reference to raw type per construction */ diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java index 51a63781ec..506733a7ee 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java @@ -26,6 +26,7 @@ * Bug 428264 - [1.8] method reference of generic class causes problems (wrong inference result or NPE) * Bug 392238 - [1.8][compiler][null] Detect semantically invalid null type annotations * Bug 426537 - [1.8][inference] Eclipse compiler thinks I<? super J> is compatible with I<J<?>> - raw type J involved + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" * Andy Clement (GoPivotal, Inc) aclement@gopivotal.com - Contribution for * Bug 383624 - [1.8][compiler] Revive code generation support for type annotations (from Olivier's work) *******************************************************************************/ @@ -409,7 +410,7 @@ public class ReferenceExpression extends FunctionalExpression implements Invocat return this.resolvedType = null; } - if (this.lhs instanceof TypeReference && lhsType.hasNullTypeAnnotations()) { + if (this.lhs instanceof TypeReference && ((TypeReference)this.lhs).hasNullTypeAnnotation()) { scope.problemReporter().nullAnnotationUnsupportedLocation((TypeReference) this.lhs); } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeReference.java index 4bddd9fc6e..272704e48a 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeReference.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeReference.java @@ -20,6 +20,7 @@ * Bug 434600 - Incorrect null analysis error reporting on type parameters * Bug 439516 - [1.8][null] NonNullByDefault wrongly applied to implicit type bound of binary type * Bug 438458 - [1.8][null] clean up handling of null type annotations wrt type variables + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" * Andy Clement (GoPivotal, Inc) aclement@gopivotal.com - Contributions for * Bug 383624 - [1.8][compiler] Revive code generation support for type annotations (from Olivier's work) * Bug 409236 - [1.8][compiler] Type annotations on intersection cast types dropped by code generator @@ -689,6 +690,26 @@ public Annotation findAnnotation(long nullTagBits) { } return null; } +public boolean hasNullTypeAnnotation() { + if (this.annotations != null) { + Annotation[] innerAnnotations = this.annotations[this.annotations.length-1]; + if (containsNullAnnotation(innerAnnotations)) + return true; + } + return false; +} +public static boolean containsNullAnnotation(Annotation[] annotations) { + if (annotations != null) { + for (int i = 0; i < annotations.length; i++) { + if (annotations[i] != null + && annotations[i].resolvedType != null + && (annotations[i].resolvedType.id == TypeIds.T_ConfiguredAnnotationNonNull + || annotations[i].resolvedType.id == TypeIds.T_ConfiguredAnnotationNullable)) + return true; + } + } + return false; +} public TypeReference[] getTypeReferences() { return new TypeReference [] { this }; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java index e434522501..7cbbe9cc93 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java @@ -33,6 +33,7 @@ * Bug 429958 - [1.8][null] evaluate new DefaultLocation attribute of @NonNullByDefault * Bug 432348 - [1.8] Internal compiler error (NPE) after upgrade to 1.8 * Bug 438458 - [1.8][null] clean up handling of null type annotations wrt type variables + * Bug 435570 - [1.8][null] @NonNullByDefault illegally tries to affect "throws E" * Jesper S Moller <jesper@selskabet.org> - Contributions for * Bug 412153 - [1.8][compiler] Check validity of annotations which may be repeatable * Till Brychcy - Contributions for @@ -1816,7 +1817,7 @@ public MethodBinding resolveTypesFor(MethodBinding method) { if ((resolvedExceptionType.tagBits & TagBits.HasMissingType) != 0) { method.tagBits |= TagBits.HasMissingType; } - if (resolvedExceptionType.hasNullTypeAnnotations()) { + if (exceptionTypes[i].hasNullTypeAnnotation()) { methodDecl.scope.problemReporter().nullAnnotationUnsupportedLocation(exceptionTypes[i]); } method.modifiers |= (resolvedExceptionType.modifiers & ExtraCompilerModifiers.AccGenericSignature); |
