Skip to content

Commit afa05c4

Browse files
authored
Bug 550255 - "The method X can be made static" ignores synchronized (eclipse-jdt#4269)
Don't report warning about "can be made static" if the method is synchronized. eclipse-jdt#4269
1 parent 70b248e commit afa05c4

File tree

6 files changed

+74
-45
lines changed

6 files changed

+74
-45
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void analyseCode(ClassScope classScope, FlowContext flowContext, FlowInfo
187187
this.scope.checkUnusedParameters(this.binding);
188188
// check if the method could have been static
189189
if (!this.binding.isStatic() && (this.bits & ASTNode.CanBeStatic) != 0 && !this.isDefaultMethod()) {
190-
if(!this.binding.isOverriding() && !this.binding.isImplementing()) {
190+
if(!this.binding.isOverriding() && !this.binding.isImplementing() && !this.binding.isSynchronized()) {
191191
if (this.binding.isPrivate() || this.binding.isFinal() || this.binding.declaringClass.isFinal()) {
192192
this.scope.problemReporter().methodCanBeDeclaredStatic(this);
193193
} else {

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/ClasspathJar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public List<Classpath> fetchLinkedJars(FileSystem.ClasspathSectionProblemReporte
7474
try (InputStream inputStream = this.zipFile.getInputStream(manifest)) {
7575
success = analyzer.analyzeManifestContents(inputStream);
7676
}
77-
List calledFileNames = analyzer.getCalledFileNames();
77+
List<String> calledFileNames = analyzer.getCalledFileNames();
7878
if (problemReporter != null) {
7979
if (!success || analyzer.getClasspathSectionsCount() == 1 && calledFileNames == null) {
8080
problemReporter.invalidClasspathSection(getPath());

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/ManifestAnalyzer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21-
@SuppressWarnings({"rawtypes", "unchecked"})
2221
public class ManifestAnalyzer {
2322
private static final int
2423
START = 0,
@@ -31,7 +30,7 @@ public class ManifestAnalyzer {
3130
private static final char[] CLASSPATH_HEADER_TOKEN =
3231
"Class-Path:".toCharArray(); //$NON-NLS-1$
3332
private int classpathSectionsCount;
34-
private ArrayList calledFilesNames;
33+
private ArrayList<String> calledFilesNames;
3534

3635
/**
3736
* Analyzes the manifest contents. The given input stream is read using a UTF-8 encoded reader.
@@ -180,7 +179,7 @@ public boolean analyzeManifestContents(char[] chars) {
180179
private boolean addCurrentTokenJarWhenNecessary(StringBuilder currentJarToken) {
181180
if (currentJarToken != null && currentJarToken.length() > 0) {
182181
if (this.calledFilesNames == null) {
183-
this.calledFilesNames = new ArrayList();
182+
this.calledFilesNames = new ArrayList<>();
184183
}
185184
this.calledFilesNames.add(currentJarToken.toString());
186185
currentJarToken.setLength(0);
@@ -194,7 +193,7 @@ private boolean addCurrentTokenJarWhenNecessary(StringBuilder currentJarToken) {
194193
public int getClasspathSectionsCount() {
195194
return this.classpathSectionsCount;
196195
}
197-
public List getCalledFileNames() {
196+
public List<String> getCalledFileNames() {
198197
return this.calledFilesNames;
199198
}
200199
}

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import junit.framework.Test;
5252
import org.eclipse.jdt.core.JavaCore;
5353
import org.eclipse.jdt.core.compiler.CharOperation;
54+
import org.eclipse.jdt.core.tests.junit.extension.TestCase;
5455
import org.eclipse.jdt.core.tests.util.AbstractCompilerTest;
5556
import org.eclipse.jdt.core.tests.util.Util;
5657
import org.eclipse.jdt.internal.compiler.batch.ClasspathDirectory;
@@ -64,11 +65,10 @@
6465
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
6566
import org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer;
6667

67-
@SuppressWarnings({ "unchecked", "rawtypes" })
6868
public class BatchCompilerTest extends AbstractBatchCompilerTest {
6969

7070
static {
71-
// TESTS_NAMES = new String[] { "test3445" };
71+
// TESTS_NAMES = new String[] { "testBug550255" };
7272
// TESTS_NUMBERS = new int[] { 306 };
7373
// TESTS_RANGE = new int[] { 298, -1 };
7474
}
@@ -84,7 +84,7 @@ public BatchCompilerTest(String name) {
8484
public static Test suite() {
8585
return buildMinimalComplianceTestSuite(testClass(), FIRST_SUPPORTED_JAVA_VERSION);
8686
}
87-
public static Class testClass() {
87+
public static Class<? extends TestCase> testClass() {
8888
return BatchCompilerTest.class;
8989
}
9090
static class StringMatcher extends Matcher {
@@ -8187,12 +8187,12 @@ public void test230_sourcepath_vs_classpath() throws IOException, InterruptedExc
81878187
assertTrue(this.verifier.getExecutionOutput().startsWith("2")); // skip trailing newline
81888188
// 2 means we selected src2
81898189
// recompile and run result using various levels of javac
8190-
Iterator javacCompilersIterator = javacCompilers.iterator();
8190+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
81918191
String specialOptions = commonOptions + " -Xprefer:source ";
81928192
String sourceFileNames[] = new String[] {sourceFilePath};
81938193
File outputDir = new File(OUTPUT_DIR);
81948194
while (javacCompilersIterator.hasNext()) {
8195-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8195+
JavacCompiler javacCompiler = javacCompilersIterator.next();
81968196
assertTrue(javacCompiler.compile(
81978197
outputDir, /* directory */
81988198
commonOptions /* options */,
@@ -8270,11 +8270,11 @@ public void test231_sourcepath_vs_classpath() throws IOException, InterruptedExc
82708270
false /* shouldFlushOutputDirectory */,
82718271
null /* progress */);
82728272
if (RUN_JAVAC) {
8273-
Iterator javacCompilersIterator = javacCompilers.iterator();
8273+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
82748274
String sourceFileNames[] = new String[] {sourceFilePath};
82758275
File outputDir = new File(OUTPUT_DIR);
82768276
while (javacCompilersIterator.hasNext()) {
8277-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8277+
JavacCompiler javacCompiler = javacCompilersIterator.next();
82788278
assertFalse(javacCompiler.compile(
82798279
outputDir /* directory */,
82808280
commonOptions /* options */,
@@ -8329,11 +8329,11 @@ public void test232_repeated_classpath() throws IOException, InterruptedExceptio
83298329
if (RUN_JAVAC) {
83308330
// javac skips all but the last classpath entry (which results into an
83318331
// error in the split case here)
8332-
Iterator javacCompilersIterator = javacCompilers.iterator();
8332+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
83338333
String sourceFileNames[] = new String[] {sourceFilePath};
83348334
File outputDir = new File(OUTPUT_DIR);
83358335
while (javacCompilersIterator.hasNext()) {
8336-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8336+
JavacCompiler javacCompiler = javacCompilersIterator.next();
83378337
assertTrue(javacCompiler.compile(
83388338
outputDir /* directory */,
83398339
combinedClasspathOptions /* options */,
@@ -8381,12 +8381,12 @@ public void test233_repeated_sourcepath() throws IOException, InterruptedExcepti
83818381
true /* shouldFlushOutputDirectory */,
83828382
null /* progress */);
83838383
if (RUN_JAVAC) {
8384-
Iterator javacCompilersIterator = javacCompilers.iterator();
8384+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
83858385
String sourceFileNamesZ[] = new String[] {sourceFilePathZ};
83868386
String sourceFileNamesW[] = new String[] {sourceFilePathW};
83878387
File outputDir = new File(OUTPUT_DIR);
83888388
while (javacCompilersIterator.hasNext()) {
8389-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8389+
JavacCompiler javacCompiler = javacCompilersIterator.next();
83908390
// succeeds because it picks src2 up
83918391
assertTrue(javacCompiler.compile(
83928392
outputDir /* directory */,
@@ -8436,11 +8436,11 @@ public void test234_sourcepath_vs_classpath() throws IOException, InterruptedExc
84368436
// in contrast with test#232 when src1 is on the classpath, javac fails
84378437
// to find src1/X.java; this is because -sourcepath inhibits source files
84388438
// search in classpath directories
8439-
Iterator javacCompilersIterator = javacCompilers.iterator();
8439+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
84408440
String sourceFileNames[] = new String[] {sourceFilePath};
84418441
File outputDir = new File(OUTPUT_DIR);
84428442
while (javacCompilersIterator.hasNext()) {
8443-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8443+
JavacCompiler javacCompiler = javacCompilersIterator.next();
84448444
assertFalse(javacCompiler.compile(
84458445
outputDir, /* directory */
84468446
commonOptions /* options */,
@@ -8518,11 +8518,11 @@ public void test235_classpath() throws IOException, InterruptedException {
85188518
// javac passes, using the most recent file amongst source and class files
85198519
// present on the classpath
85208520
if (RUN_JAVAC) {
8521-
Iterator javacCompilersIterator = javacCompilers.iterator();
8521+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
85228522
String sourceFileNames[] = new String[] {sourceFilePath};
85238523
File outputDir = new File(OUTPUT_DIR);
85248524
while (javacCompilersIterator.hasNext()) {
8525-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8525+
JavacCompiler javacCompiler = javacCompilersIterator.next();
85268526
assertTrue(javacCompiler.compile(
85278527
outputDir /* directory */,
85288528
commonOptions /* options */,
@@ -8590,11 +8590,11 @@ public void test236_classpath() throws IOException, InterruptedException {
85908590
this.verifier.execute("Y", new String[] {OUTPUT_DIR + File.separator + "bin"});
85918591
assertTrue(this.verifier.getExecutionOutput().startsWith("1")); // skip trailing newline
85928592
if (RUN_JAVAC) {
8593-
Iterator javacCompilersIterator = javacCompilers.iterator();
8593+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
85948594
String sourceFileNames[] = new String[] {sourceFilePath};
85958595
File outputDir = new File(OUTPUT_DIR);
85968596
while (javacCompilersIterator.hasNext()) {
8597-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8597+
JavacCompiler javacCompiler = javacCompilersIterator.next();
85988598
assertTrue(javacCompiler.compile(
85998599
outputDir /* directory */,
86008600
commonOptions /* options */,
@@ -8657,11 +8657,11 @@ public void test237_classpath() throws IOException, InterruptedException {
86578657
this.verifier.execute("Y", new String[] {OUTPUT_DIR + File.separator + "bin"});
86588658
assertTrue(this.verifier.getExecutionOutput().startsWith("1")); // skip trailing newline
86598659
if (RUN_JAVAC) {
8660-
Iterator javacCompilersIterator = javacCompilers.iterator();
8660+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
86618661
String sourceFileNames[] = new String[] {sourceFilePath};
86628662
File outputDir = new File(OUTPUT_DIR);
86638663
while (javacCompilersIterator.hasNext()) {
8664-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8664+
JavacCompiler javacCompiler = javacCompilersIterator.next();
86658665
assertTrue(javacCompiler.compile(
86668666
outputDir /* directory */,
86678667
commonOptions /* options */,
@@ -8717,11 +8717,11 @@ public void test238_classpath() throws IOException, InterruptedException {
87178717
this.verifier.execute("Y", new String[] {OUTPUT_DIR + File.separator + "bin"});
87188718
assertTrue(this.verifier.getExecutionOutput().startsWith("1")); // skip trailing newline
87198719
if (RUN_JAVAC) {
8720-
Iterator javacCompilersIterator = javacCompilers.iterator();
8720+
Iterator<JavacCompiler> javacCompilersIterator = javacCompilers.iterator();
87218721
String sourceFileNames[] = new String[] {sourceFilePath};
87228722
File outputDir = new File(OUTPUT_DIR);
87238723
while (javacCompilersIterator.hasNext()) {
8724-
JavacCompiler javacCompiler = (JavacCompiler) javacCompilersIterator.next();
8724+
JavacCompiler javacCompiler = javacCompilersIterator.next();
87258725
assertTrue(javacCompiler.compile(
87268726
outputDir /* directory */,
87278727
commonOptions /* options */,
@@ -10103,8 +10103,8 @@ public void test291_jar_ref_in_jar() throws Exception {
1010310103
"Class-Path: \r\n" +
1010410104
"\r\n"
1010510105
));
10106-
List calledFileNames = analyzer.getCalledFileNames();
10107-
String actual = calledFileNames == null ? "<null>" : Util.toString((String[]) calledFileNames.toArray(new String[calledFileNames.size()]), false/*don't add extra new lines*/);
10106+
List<String> calledFileNames = analyzer.getCalledFileNames();
10107+
String actual = calledFileNames == null ? "<null>" : Util.toString(calledFileNames.toArray(new String[calledFileNames.size()]), false/*don't add extra new lines*/);
1010810108
assertStringEquals(
1010910109
"<null>",
1011010110
actual,
@@ -13326,4 +13326,35 @@ public static void main(String[] args) {
1332613326
"",
1332713327
true);
1332813328
}
13329+
public void testBug550255() {
13330+
this.runConformTest(
13331+
new String[] {
13332+
"X.java",
13333+
"public class X {\n" +
13334+
" public static int field1;\n" +
13335+
" public static int field2;\n" +
13336+
" private synchronized String foo() {\n" +
13337+
" return null;\n" +
13338+
" }\n" +
13339+
" public synchronized void foo1() {\n" +
13340+
" System.out.println(1);\n" +
13341+
" }\n" +
13342+
" public final void foo2() {\n" +
13343+
" System.out.println(2);\n" +
13344+
" }\n" +
13345+
"}\n",
13346+
},
13347+
"\"" + OUTPUT_DIR + File.separator + "X.java\""
13348+
+ " -sourcepath \"" + OUTPUT_DIR + "\""
13349+
+ " -warn:all-static-method -proc:none -d \"" + OUTPUT_DIR + "\"",
13350+
"",
13351+
"----------\n" +
13352+
"1. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 10)\n"
13353+
+ " public final void foo2() {\n"
13354+
+ " ^^^^^^\n"
13355+
+ "The method foo2() from the type X can be declared as static\n"
13356+
+ "----------\n"
13357+
+ "1 problem (1 warning)\n",
13358+
true);
13359+
}
1332913360
}

0 commit comments

Comments
 (0)