diff options
author | Pierre-Yves B. | 2018-12-17 22:47:30 +0000 |
---|---|---|
committer | Stephan Herrmann | 2019-01-13 18:23:56 +0000 |
commit | abf93b41f080158cf2a0f2630bf4a409a995f65b (patch) | |
tree | 572d1151e47c50c4b71c45fdb8252bd469a63ffb /org.eclipse.jdt.core/compiler/org/eclipse | |
parent | cc6979905d4f875aa69d50cb9a0e6325d3cd4d48 (diff) | |
download | eclipse.jdt.core-abf93b41f080158cf2a0f2630bf4a409a995f65b.tar.gz eclipse.jdt.core-abf93b41f080158cf2a0f2630bf4a409a995f65b.tar.xz eclipse.jdt.core-abf93b41f080158cf2a0f2630bf4a409a995f65b.zip |
Bug 542520 - [JUnit 5] Warning The method xxx from the type X is neverI20190113-1800
used locally is shown when using MethodSource
Change-Id: Ia77781af8b72b9321122fe1e062544d46777e497
Signed-off-by: Pierre-Yves B. <PyvesDev@gmail.com>
Diffstat (limited to 'org.eclipse.jdt.core/compiler/org/eclipse')
4 files changed, 58 insertions, 3 deletions
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java index 73e4615d54..afb99dfd62 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java @@ -18,6 +18,8 @@ * Bug 424727 - [compiler][null] NullPointerException in nullAnnotationUnsupportedLocation(ProblemReporter.java:5708) * Bug 457210 - [1.8][compiler][null] Wrong Nullness errors given on full build build but not on incremental build? * Keigo Imai - Contribution for bug 388903 - Cannot extend inner class as an anonymous class when it extends the outer class + * Pierre-Yves B. <pyvesdev@gmail.com> - Contribution for + * Bug 542520 - [JUnit 5] Warning The method xxx from the type X is never used locally is shown when using MethodSource *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -30,6 +32,7 @@ import org.eclipse.jdt.internal.compiler.flow.*; import org.eclipse.jdt.internal.compiler.lookup.*; import org.eclipse.jdt.internal.compiler.parser.*; import org.eclipse.jdt.internal.compiler.problem.*; +import org.eclipse.jdt.internal.compiler.util.SimpleSetOfCharArray; import org.eclipse.jdt.internal.compiler.util.Util; public class TypeDeclaration extends Statement implements ProblemSeverities, ReferenceContext { @@ -745,6 +748,7 @@ private void internalAnalyseCode(FlowContext flowContext, FlowInfo flowInfo) { if (this.methods != null) { UnconditionalFlowInfo outerInfo = flowInfo.unconditionalFieldLessCopy(); FlowInfo constructorInfo = nonStaticFieldInfo.unconditionalInits().discardNonFieldInitializations().addInitializationsFrom(outerInfo); + SimpleSetOfCharArray jUnitMethodSourceValues = getJUnitMethodSourceValues(); for (int i = 0, count = this.methods.length; i < count; i++) { AbstractMethodDeclaration method = this.methods[i]; if (method.ignoreFurtherInvestigation) @@ -760,6 +764,10 @@ private void internalAnalyseCode(FlowContext flowContext, FlowInfo flowInfo) { ((ConstructorDeclaration)method).analyseCode(this.scope, initializerContext, constructorInfo.copy(), flowInfo.reachMode()); } } else { // regular method + // JUnit 5 only accepts methods without arguments for method sources + if (method.arguments == null && jUnitMethodSourceValues.includes(method.selector) && method.binding != null) { + method.binding.modifiers |= ExtraCompilerModifiers.AccLocallyUsed; + } // pass down the parentContext (NOT an initializer context, see above): ((MethodDeclaration)method).analyseCode(this.scope, parentContext, flowInfo.copy()); } @@ -771,6 +779,31 @@ private void internalAnalyseCode(FlowContext flowContext, FlowInfo flowInfo) { } } +private SimpleSetOfCharArray getJUnitMethodSourceValues() { + SimpleSetOfCharArray junitMethodSourceValues = new SimpleSetOfCharArray(); + for (AbstractMethodDeclaration methodDeclaration : this.methods) { + junitMethodSourceValues.add(getJUnitMethodSourceValue(methodDeclaration)); + } + return junitMethodSourceValues; +} + +private char[] getJUnitMethodSourceValue(AbstractMethodDeclaration methodDeclaration) { + if (methodDeclaration.annotations != null) { + for (Annotation annotation : methodDeclaration.annotations) { + if (annotation.resolvedType != null && annotation.resolvedType.id == TypeIds.T_OrgJunitJupiterParamsProviderMethodSource) { + for (MemberValuePair memberValuePair : annotation.memberValuePairs()) { + if (CharOperation.equals(memberValuePair.name, TypeConstants.VALUE)) { + return ((StringLiteral) memberValuePair.value).source; + } + } + // value member not specified (i.e. marker annotation): JUnit 5 defaults to the test method's name + return methodDeclaration.selector; + } + } + } + return CharOperation.NO_CHAR; +} + public final static int kind(int flags) { switch (flags & (ClassFileConstants.AccInterface|ClassFileConstants.AccAnnotation|ClassFileConstants.AccEnum)) { case ClassFileConstants.AccInterface : diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java index 90e9f6757c..48f8cbeef5 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java @@ -45,6 +45,8 @@ * bug 527554 - [18.3] Compiler support for JEP 286 Local-Variable Type * Ulrich Grave <ulrich.grave@gmx.de> - Contributions for * bug 386692 - Missing "unused" warning on "autowired" fields + * Pierre-Yves B. <pyvesdev@gmail.com> - Contribution for + * bug 542520 - [JUnit 5] Warning The method xxx from the type X is never used locally is shown when using MethodSource *******************************************************************************/ package org.eclipse.jdt.internal.compiler.lookup; @@ -889,6 +891,14 @@ public void computeId() { } return; } + if (CharOperation.equals(TypeConstants.JUNIT, this.compoundName[1])) { + if (CharOperation.equals(TypeConstants.METHOD_SOURCE, this.compoundName[5])) { + if (CharOperation.equals(TypeConstants.ORG_JUNIT_METHOD_SOURCE, this.compoundName)) { + this.id = TypeIds.T_OrgJunitJupiterParamsProviderMethodSource; + } + } + return; + } if (!CharOperation.equals(TypeConstants.JDT, this.compoundName[2]) || !CharOperation.equals(TypeConstants.ITYPEBINDING, this.compoundName[5])) return; if (CharOperation.equals(TypeConstants.ORG_ECLIPSE_JDT_CORE_DOM_ITYPEBINDING, this.compoundName)) diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java index 47e32057f4..f3387f47fc 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java @@ -29,6 +29,8 @@ * Bug 405104 - [1.8][compiler][codegen] Implement support for serializeable lambdas * Ulrich Grave <ulrich.grave@gmx.de> - Contributions for * bug 386692 - Missing "unused" warning on "autowired" fields + * Pierre-Yves B. <pyvesdev@gmail.com> - Contribution for + * bug 542520 - [JUnit 5] Warning The method xxx from the type X is never used locally is shown when using MethodSource *******************************************************************************/ package org.eclipse.jdt.internal.compiler.lookup; @@ -316,6 +318,9 @@ public interface TypeConstants { char[] JUNIT = "junit".toCharArray(); //$NON-NLS-1$ char[] FRAMEWORK = "framework".toCharArray(); //$NON-NLS-1$ + char[] JUPITER = "jupiter".toCharArray(); //$NON-NLS-1$ + char[] PARAMS = "params".toCharArray(); //$NON-NLS-1$ + char[] PROVIDER = "provider".toCharArray(); //$NON-NLS-1$ char[][] JUNIT_FRAMEWORK_ASSERT = new char[][] { JUNIT, FRAMEWORK, ASSERT_CLASS }; char[][] ORG_JUNIT_ASSERT = new char[][] { ORG, JUNIT, ASSERT_CLASS }; // ... methods: @@ -323,6 +328,9 @@ public interface TypeConstants { char[] ASSERT_NOTNULL = "assertNotNull".toCharArray(); //$NON-NLS-1$ char[] ASSERT_TRUE = "assertTrue".toCharArray(); //$NON-NLS-1$ char[] ASSERT_FALSE = "assertFalse".toCharArray(); //$NON-NLS-1$ + // ... annotations: + char[] METHOD_SOURCE = "MethodSource".toCharArray(); //$NON-NLS-1$ + char[][] ORG_JUNIT_METHOD_SOURCE = new char[][] { ORG, JUNIT, JUPITER, PARAMS, PROVIDER, METHOD_SOURCE }; char[] VALIDATE_CLASS = "Validate".toCharArray(); //$NON-NLS-1$ char[][] ORG_APACHE_COMMONS_LANG_VALIDATE = new char[][] { ORG, APACHE, COMMONS, LANG, VALIDATE_CLASS }; @@ -333,7 +341,6 @@ public interface TypeConstants { // ... methods: char[] IS_TRUE = "isTrue".toCharArray(); //$NON-NLS-1$ char[] NOT_NULL = "notNull".toCharArray(); //$NON-NLS-1$ - char[] PROVIDER = "provider".toCharArray(); //$NON-NLS-1$ char[][] COM_GOOGLE_COMMON_BASE_PRECONDITIONS = new char[][] { COM, GOOGLE, "common".toCharArray(), "base".toCharArray(), "Preconditions".toCharArray() }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java index cb3893e1b3..52c1428f80 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2018 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -18,10 +18,12 @@ * bug 400421 - [compiler] Null analysis for fields does not take @com.google.inject.Inject into account * bug 382069 - [null] Make the null analysis consider JUnit's assertNotNull similarly to assertions * Bug 410218 - Optional warning for arguments of "unexpected" types to Map#get(Object), Collection#remove(Object) et al. - * Jesper S Moller <jesper@selskabet.org> - Contributions for + * Jesper S Moller <jesper@selskabet.org> - Contributions for * Bug 412153 - [1.8][compiler] Check validity of annotations which may be repeatable * Ulrich Grave <ulrich.grave@gmx.de> - Contributions for * bug 386692 - Missing "unused" warning on "autowired" fields + * Pierre-Yves B. <pyvesdev@gmail.com> - Contribution for + * bug 542520 - [JUnit 5] Warning The method xxx from the type X is never used locally is shown when using MethodSource *******************************************************************************/ package org.eclipse.jdt.internal.compiler.lookup; @@ -141,6 +143,9 @@ public interface TypeIds { final int T_JavaUtilMap = 91; final int T_JavaUtilList = 92; + // @MethodSource + final int T_OrgJunitJupiterParamsProviderMethodSource = 93; + // If you add new type id, make sure to bump up T_LastWellKnownTypeId if there is a cross over. final int T_LastWellKnownTypeId = 128; |