### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: batch/org/eclipse/jdt/internal/compiler/batch/Main.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java,v retrieving revision 1.355 diff -u -r1.355 Main.java --- batch/org/eclipse/jdt/internal/compiler/batch/Main.java 29 Sep 2010 16:43:28 -0000 1.355 +++ batch/org/eclipse/jdt/internal/compiler/batch/Main.java 16 Oct 2010 20:06:34 -0000 @@ -3461,6 +3461,9 @@ } else if (token.equals("super")) { //$NON-NLS-1$ setSeverity(CompilerOptions.OPTION_ReportOverridingMethodWithoutSuperInvocation, severity, isEnabling); return; + } else if (token.equals("static-method")) { //$NON-NLS-1$ + setSeverity(CompilerOptions.OPTION_ReportMethodCanBeStatic, severity, isEnabling); + return; } break; case 't' : Index: batch/org/eclipse/jdt/internal/compiler/batch/messages.properties =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/messages.properties,v retrieving revision 1.933 diff -u -r1.933 messages.properties --- batch/org/eclipse/jdt/internal/compiler/batch/messages.properties 6 Oct 2010 13:57:36 -0000 1.933 +++ batch/org/eclipse/jdt/internal/compiler/batch/messages.properties 16 Oct 2010 20:06:37 -0000 @@ -309,6 +309,7 @@ \ semicolon unnecessary semicolon, empty statement\n\ \ serial + missing serialVersionUID\n\ \ specialParamHiding constructor or setter parameter hiding a field\n\ +\ static-method method can be declared as static\n\ \ static-access macro for indirectStatic and staticReceiver\n\ \ staticReceiver + non-static reference to static member\n\ \ super overriding a method without making a super invocation\n\ Index: compiler/org/eclipse/jdt/core/compiler/IProblem.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java,v retrieving revision 1.222 diff -u -r1.222 IProblem.java --- compiler/org/eclipse/jdt/core/compiler/IProblem.java 7 Sep 2010 13:39:18 -0000 1.222 +++ compiler/org/eclipse/jdt/core/compiler/IProblem.java 16 Oct 2010 20:06:37 -0000 @@ -401,6 +401,8 @@ int IndirectAccessToStaticMethod = Internal + MethodRelated + 119; /** @since 3.4 */ int MissingTypeInMethod = MethodRelated + 120; + /** @since 3.7 */ + int MethodCanBeStatic = Internal + MethodRelated + 121; // constructors /** @since 3.4 */ Index: compiler/org/eclipse/jdt/internal/compiler/ast/ASTNode.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ASTNode.java,v retrieving revision 1.103 diff -u -r1.103 ASTNode.java --- compiler/org/eclipse/jdt/internal/compiler/ast/ASTNode.java 10 May 2010 20:47:58 -0000 1.103 +++ compiler/org/eclipse/jdt/internal/compiler/ast/ASTNode.java 16 Oct 2010 20:06:37 -0000 @@ -146,6 +146,7 @@ // for type, method and field declarations public static final int HasLocalType = Bit2; // cannot conflict with AddAssertionMASK public static final int HasBeenResolved = Bit5; // field decl only (to handle forward references) + public static final int CanBeStatic = Bit9; // for expression public static final int ParenthesizedSHIFT = 21; // Bit22 -> Bit29 Index: compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java,v retrieving revision 1.148 diff -u -r1.148 MessageSend.java --- compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java 12 Aug 2010 16:58:28 -0000 1.148 +++ compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java 16 Oct 2010 20:06:37 -0000 @@ -61,6 +61,16 @@ flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits(); if (nonStatic) { this.receiver.checkNPE(currentScope, flowContext, flowInfo); + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 + if (this.receiver.isThis()) { + // accessing non-static method without an object + currentScope.resetEnclosingMethodStaticFlag(); + } + } else if (this.receiver.isThis()) { + if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) { + // explicit this receiver, not allowed in static context + currentScope.resetEnclosingMethodStaticFlag(); + } } if (this.arguments != null) { Index: compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java,v retrieving revision 1.147 diff -u -r1.147 QualifiedNameReference.java --- compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java 21 Sep 2010 14:02:57 -0000 1.147 +++ compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java 16 Oct 2010 20:06:44 -0000 @@ -80,6 +80,9 @@ currentScope.problemReporter().uninitializedBlankFinalField(lastFieldBinding, this); } } + if (!lastFieldBinding.isStatic()) { + currentScope.resetEnclosingMethodStaticFlag(); + } break; case Binding.LOCAL : // first binding is a local variable @@ -175,8 +178,8 @@ if (needValue || complyTo14) { manageSyntheticAccessIfNecessary(currentScope, (FieldBinding) this.binding, 0, flowInfo); } + FieldBinding fieldBinding = (FieldBinding) this.binding; if (this.indexOfFirstFieldBinding == 1) { // was an implicit reference to the first field binding - FieldBinding fieldBinding = (FieldBinding) this.binding; // check if reading a final blank field if (fieldBinding.isBlankFinal() && currentScope.needBlankFinalFieldInitializationCheck(fieldBinding)) { @@ -186,6 +189,9 @@ } } } + if (!fieldBinding.isStatic()) { + currentScope.resetEnclosingMethodStaticFlag(); + } break; case Binding.LOCAL : // reading a local variable LocalVariableBinding localBinding; Index: compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java,v retrieving revision 1.234 diff -u -r1.234 CompilerOptions.java --- compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java 6 Oct 2010 13:57:36 -0000 1.234 +++ compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java 16 Oct 2010 20:06:44 -0000 @@ -133,6 +133,7 @@ public static final String OPTION_ReportTasks = "org.eclipse.jdt.core.compiler.problem.tasks"; //$NON-NLS-1$ public static final String OPTION_ReportUnusedObjectAllocation = "org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation"; //$NON-NLS-1$ public static final String OPTION_IncludeNullInfoFromAsserts = "org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts"; //$NON-NLS-1$ + public static final String OPTION_ReportMethodCanBeStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic"; //$NON-NLS-1$ // Backward compatibility public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$ public static final String OPTION_ReportMissingAnnotation = "org.eclipse.jdt.core.compiler.problem.missingAnnotation"; //$NON-NLS-1$ @@ -238,6 +239,7 @@ public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2; public static final int Tasks = IrritantSet.GROUP2 | ASTNode.Bit3; public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4; + public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5; // Severity level for handlers /** @@ -375,6 +377,7 @@ "rawtypes", //$NON-NLS-1$ "serial", //$NON-NLS-1$ "static-access", //$NON-NLS-1$ + "static-method", //$NON-NLS-1$ "super", //$NON-NLS-1$ "synthetic-access", //$NON-NLS-1$ "unchecked", //$NON-NLS-1$ @@ -539,6 +542,8 @@ return OPTION_ReportDeadCode; case UnusedObjectAllocation: return OPTION_ReportUnusedObjectAllocation; + case MethodCanBeStatic : + return OPTION_ReportMethodCanBeStatic; } return null; } @@ -638,6 +643,7 @@ OPTION_ReportInvalidJavadoc, OPTION_ReportLocalVariableHiding, OPTION_ReportMethodWithConstructorName, + OPTION_ReportMethodCanBeStatic, OPTION_ReportMissingDeprecatedAnnotation, OPTION_ReportMissingJavadocComments, OPTION_ReportMissingJavadocTagDescription, @@ -739,6 +745,8 @@ return "fallthrough"; //$NON-NLS-1$ case OverridingMethodWithoutSuperInvocation : return "super"; //$NON-NLS-1$ + case MethodCanBeStatic : + return "static-method"; //$NON-NLS-1$ } return null; } @@ -796,6 +804,8 @@ return IrritantSet.SERIAL; if ("static-access".equals(warningToken)) //$NON-NLS-1$ return IrritantSet.STATIC_ACCESS; + if ("static-method".equals(warningToken)) //$NON-NLS-1$ + return IrritantSet.STATIC_METHOD; if ("synthetic-access".equals(warningToken)) //$NON-NLS-1$ return IrritantSet.SYNTHETIC_ACCESS; if ("super".equals(warningToken)) { //$NON-NLS-1$ @@ -921,6 +931,7 @@ optionsMap.put(OPTION_ReportTasks, getSeverityString(Tasks)); optionsMap.put(OPTION_ReportUnusedObjectAllocation, getSeverityString(UnusedObjectAllocation)); optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED); + optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic)); return optionsMap; } @@ -1339,6 +1350,7 @@ if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue); if ((optionValue = optionsMap.get(OPTION_ReportTasks)) != null) updateSeverity(Tasks, optionValue); if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue); + if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue); // Javadoc options if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) { @@ -1551,6 +1563,7 @@ buf.append("\n\t- dead code in trivial if statement: ").append(this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED); //$NON-NLS-1$ buf.append("\n\t- tasks severity: ").append(getSeverityString(Tasks)); //$NON-NLS-1$ buf.append("\n\t- unused object allocation: ").append(getSeverityString(UnusedObjectAllocation)); //$NON-NLS-1$ + buf.append("\n\t- method can be static: ").append(getSeverityString(MethodCanBeStatic)); //$NON-NLS-1$ return buf.toString(); } Index: compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java,v retrieving revision 1.10 diff -u -r1.10 IrritantSet.java --- compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java 18 May 2010 18:12:12 -0000 1.10 +++ compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java 16 Oct 2010 20:06:44 -0000 @@ -51,6 +51,7 @@ public static final IrritantSet RESTRICTION = new IrritantSet(CompilerOptions.ForbiddenReference); public static final IrritantSet SERIAL = new IrritantSet(CompilerOptions.MissingSerialVersion); public static final IrritantSet STATIC_ACCESS = new IrritantSet(CompilerOptions.IndirectStaticAccess); + public static final IrritantSet STATIC_METHOD = new IrritantSet(CompilerOptions.MethodCanBeStatic); public static final IrritantSet SYNTHETIC_ACCESS = new IrritantSet(CompilerOptions.AccessEmulation); public static final IrritantSet SUPER = new IrritantSet(CompilerOptions.OverridingMethodWithoutSuperInvocation); public static final IrritantSet UNUSED = new IrritantSet(CompilerOptions.UnusedLocalVariable); @@ -118,7 +119,10 @@ .set(CompilerOptions.UnusedTypeArguments) .set(CompilerOptions.RedundantSuperinterface) .set(CompilerOptions.DeadCode) - .set(CompilerOptions.UnusedObjectAllocation); + .set(CompilerOptions.UnusedObjectAllocation) + .set(CompilerOptions.MethodCanBeStatic); + STATIC_METHOD + .set(CompilerOptions.MethodCanBeStatic); String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$ if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$ UNCHECKED.set(CompilerOptions.RawTypeReference); Index: compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java,v retrieving revision 1.119 diff -u -r1.119 BlockScope.java --- compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java 13 Sep 2010 13:26:20 -0000 1.119 +++ compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java 16 Oct 2010 20:06:44 -0000 @@ -940,4 +940,12 @@ s += ((BlockScope) this.subscopes[i]).toString(tab + 1) + "\n"; //$NON-NLS-1$ return s; } +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +public void resetEnclosingMethodStaticFlag() { + MethodScope methodScope = methodScope(); + if (methodScope != null && methodScope.referenceContext instanceof MethodDeclaration) { + MethodDeclaration methodDeclaration= (MethodDeclaration) methodScope.referenceContext; + methodDeclaration.bits &= ~ASTNode.CanBeStatic; + } +} } Index: compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties,v retrieving revision 1.258 diff -u -r1.258 messages.properties --- compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 7 Sep 2010 13:39:18 -0000 1.258 +++ compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 16 Oct 2010 20:06:45 -0000 @@ -102,6 +102,7 @@ 118 = The method {1}({2}) from the type {0} is never used locally 119 = The static method {1}({2}) from the type {0} should be accessed directly 120 = The method {1}({2}) from the type {0} refers to the missing type {3} +121 = The method {1}({2}) from the type {0} can be declared as static 129 = The constructor {0}({1}) refers to the missing type {2} 130 = The constructor {0}({1}) is undefined #P org.eclipse.jdt.core.tests.compiler Index: src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java,v retrieving revision 1.35 diff -u -r1.35 ProblemTypeAndMethodTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java 21 Sep 2010 14:02:56 -0000 1.35 +++ src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java 16 Oct 2010 20:06:51 -0000 @@ -30,7 +30,7 @@ // Static initializer to specify tests subset using TESTS_* static variables // All specified tests which does not belong to the class are skipped... static { -// TESTS_NAMES = new String[] { "test127" }; +// TESTS_NAMES = new String[] { "test118" }; // TESTS_NUMBERS = new int[] { 113 }; // TESTS_RANGE = new int[] { 108, -1 }; } @@ -5887,4 +5887,543 @@ false, null); } + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +// QualifiedNameReference, SingleNameReference and MessageSend +// Can be static warning shown +public void test114() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + "public class X {\n" + + " public static int field1;\n" + + " public static int field2;\n" + + " public void bar(int i) {\n" + + " System.out.println(foo());\n" + + " foo();" + + " System.out.println(X.field1);\n" + + " System.out.println(field2);\n" + + " field2 = 1;\n" + + " }\n" + + " private static String foo() {\n" + + " return null;\n" + + " }\n" + + "}" + }, + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " public void bar(int i) {\n" + + " ^^^^^^^^^^\n" + + "The method bar(int) from the type X can be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +// FieldReference and MessageSend +// Can be static warning shown +public void test115() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + "public class X extends B{\n" + + " public static int field1;\n" + + " public static int field2;\n" + + " public void bar(int i) {\n" + + " System.out.println(foo());\n" + + " X.field2 = 2;\n" + + " System.out.println(field1);\n" + + " A a = new A();\n" + + " a.a1();\n" + + " }\n" + + " private static String foo() {\n" + + " return null;\n" + + " }\n" + + "}\n" + + "class A{\n" + + " public void a1() {\n" + + " }\n" + + "}\n" + + "class B{\n" + + " public void b1(){\n" + + " }\n" + + "}" + }, + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " public void bar(int i) {\n" + + " ^^^^^^^^^^\n" + + "The method bar(int) from the type X can be declared as static\n" + + "----------\n" + + "2. ERROR in X.java (at line 16)\n" + + " public void a1() {\n" + + " ^^^^\n" + + "The method a1() from the type A can be declared as static\n" + + "----------\n" + + "3. ERROR in X.java (at line 20)\n" + + " public void b1(){\n" + + " ^^^^\n" + + "The method b1() from the type B can be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +// MessageSend in different ways +public void test116a() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); + this.runNegativeTest( + new String[] { + "X.java", + "public class X extends B{\n" + + " public static int field1;\n" + + " public X xfield;\n" + + " public void bar1(int i) {\n" + + " baz();\n" + + " }\n" + + " public void bar2(int i) {\n" + + " this.baz();\n" + + " }\n" + + " public void bar3(int i) {\n" + + " this.xfield.baz();\n" + + " }\n" + + " public void bar4(int i) {\n" + + " xfield.baz();\n" + + " }\n" + + " public void bar5(int i) {\n" + + " X x = new X();\n" + + " x.baz();\n" + + " }\n" + + " public void bar6(int i) {\n" + + " A.xA.baz();\n" + + " }\n" + + " public void bar7(int i) {\n" + + " b1();\n" + + " }\n" + + " public void bar8(int i) {\n" + + " this.b1();\n" + + " }\n" + + " public void bar9(int i) {\n" + + " new X().b1();\n" + + " }\n" + + " public void baz() {\n" + + " }\n" + + "}\n" + + "class A{\n" + + " public static X xA;\n" + + "}\n" + + "class B{\n" + + " public void b1(){\n" + + " }\n" + + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 16)\n" + + " public void bar5(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar5(int) from the type X can be declared as static\n" + + "----------\n" + + "2. ERROR in X.java (at line 20)\n" + + " public void bar6(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar6(int) from the type X can be declared as static\n" + + "----------\n" + + "3. ERROR in X.java (at line 29)\n" + + " public void bar9(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar9(int) from the type X can be declared as static\n" + + "----------\n" + + "4. ERROR in X.java (at line 32)\n" + + " public void baz() {\n" + + " ^^^^^\n" + + "The method baz() from the type X can be declared as static\n" + + "----------\n" + + "5. ERROR in X.java (at line 39)\n" + + " public void b1(){\n" + + " ^^^^\n" + + "The method b1() from the type B can be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +// MessageSend in different ways, referencing a static method. +public void test116b() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); + this.runNegativeTest( + new String[] { + "X.java", + "public class X extends B{\n" + + " public static int field1;\n" + + " public static X xfield;\n" + + " public void bar1(int i) {\n" + + " baz();\n" + + " }\n" + + " public void bar2(int i) {\n" + + " this.baz();\n" + + " }\n" + + " public void bar3(int i) {\n" + + " this.xfield.baz();\n" + + " }\n" + + " public void bar4(int i) {\n" + + " xfield.baz();\n" + + " }\n" + + " public void bar5(int i) {\n" + + " X x = new X();\n" + + " x.baz();\n" + + " }\n" + + " public void bar6(int i) {\n" + + " A.xA.baz();\n" + + " }\n" + + " public void bar7(int i) {\n" + + " b1();\n" + + " }\n" + + " public void bar8(int i) {\n" + + " this.b1();\n" + + " }\n" + + " public void bar9(int i) {\n" + + " new X().b1();\n" + + " }\n" + + " public static void baz() {\n" + + " }\n" + + "}\n" + + "class A{\n" + + " public static X xA;\n" + + "}\n" + + "class B{\n" + + " public static void b1(){\n" + + " }\n" + + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " public void bar1(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar1(int) from the type X can be declared as static\n" + + "----------\n" + + "2. ERROR in X.java (at line 13)\n" + + " public void bar4(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar4(int) from the type X can be declared as static\n" + + "----------\n" + + "3. ERROR in X.java (at line 16)\n" + + " public void bar5(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar5(int) from the type X can be declared as static\n" + + "----------\n" + + "4. ERROR in X.java (at line 20)\n" + + " public void bar6(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar6(int) from the type X can be declared as static\n" + + "----------\n" + + "5. ERROR in X.java (at line 23)\n" + + " public void bar7(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar7(int) from the type X can be declared as static\n" + + "----------\n" + + "6. ERROR in X.java (at line 29)\n" + + " public void bar9(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar9(int) from the type X can be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +// Referring a field in different ways, accessing non-static field. +public void test117a() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); + this.runNegativeTest( + new String[] { + "X.java", + "public class X extends B{\n" + + " public int field1;\n" + + " public X xfield;\n" + + " public void bar1(int i) {\n" + + " field1 = 1;\n" + + " }\n" + + " public void bar2(int i) {\n" + + " this.field1 = 1;\n" + + " }\n" + + " public void bar3(int i) {\n" + + " System.out.println(field1);\n" + + " }\n" + + " public void bar4(int i) {\n" + + " System.out.println(this.field1);\n" + + " }\n" + + " public void bar5(int i) {\n" + + " X x = new X();\n" + + " x.field1 = 1;\n" + + " }\n" + + " public void bar6(int i) {\n" + + " A.xA.field1 = 1;\n" + + " }\n" + + " public void bar7(int i) {\n" + + " b1 = 1;\n" + + " }\n" + + " public void bar8(int i) {\n" + + " this.b1 = 1;\n" + + " }\n" + + " public void bar9(int i) {\n" + + " new X().b1 = 1;\n" + + " }\n" + + " public void bar10(int i) {\n" + + " this.xfield.field1 = 1;\n" + + " }\n" + + " public void bar11(int i) {\n" + + " System.out.println(this.xfield.field1);\n" + + " }\n" + + " public void bar12(int i) {\n" + + " System.out.println(new X().b1);\n" + + " }\n" + + " public void bar13(int i) {\n" + + " System.out.println(b1);\n" + + " }\n" + + " public void bar14(int i) {\n" + + " System.out.println(this.b1);\n" + + " }\n" + + " public void bar15(int i) {\n" + + " xfield.field1 = 1;\n" + + " }\n" + + " public void bar16(int i) {\n" + + " System.out.println(xfield.field1);\n" + + " }\n" + + " public void baz() {\n" + + " }\n" + + "}\n" + + "class A{\n" + + " public static X xA;\n" + + "}\n" + + "class B{\n" + + " public int b1;\n" + + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 16)\n" + + " public void bar5(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar5(int) from the type X can be declared as static\n" + + "----------\n" + + "2. ERROR in X.java (at line 20)\n" + + " public void bar6(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar6(int) from the type X can be declared as static\n" + + "----------\n" + + "3. ERROR in X.java (at line 29)\n" + + " public void bar9(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar9(int) from the type X can be declared as static\n" + + "----------\n" + + "4. ERROR in X.java (at line 38)\n" + + " public void bar12(int i) {\n" + + " ^^^^^^^^^^^^\n" + + "The method bar12(int) from the type X can be declared as static\n" + + "----------\n" + + "5. ERROR in X.java (at line 53)\n" + + " public void baz() {\n" + + " ^^^^^\n" + + "The method baz() from the type X can be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +// Referring a field in different ways, accessing non-static field. +public void test117b() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); + this.runNegativeTest( + new String[] { + "X.java", + "public class X extends B{\n" + + " public static int field1;\n" + + " public static X xfield;\n" + + " public void bar1(int i) {\n" + + " field1 = 1;\n" + + " }\n" + + " public void bar2(int i) {\n" + + " this.field1 = 1;\n" + + " }\n" + + " public void bar3(int i) {\n" + + " System.out.println(field1);\n" + + " }\n" + + " public void bar4(int i) {\n" + + " System.out.println(this.field1);\n" + + " }\n" + + " public void bar5(int i) {\n" + + " X x = new X();\n" + + " x.field1 = 1;\n" + + " }\n" + + " public void bar6(int i) {\n" + + " A.xA.field1 = 1;\n" + + " }\n" + + " public void bar7(int i) {\n" + + " b1 = 1;\n" + + " }\n" + + " public void bar8(int i) {\n" + + " this.b1 = 1;\n" + + " }\n" + + " public void bar9(int i) {\n" + + " new X().b1 = 1;\n" + + " }\n" + + " public void bar10(int i) {\n" + + " this.xfield.field1 = 1;\n" + + " }\n" + + " public void bar11(int i) {\n" + + " System.out.println(this.xfield.field1);\n" + + " }\n" + + " public void bar12(int i) {\n" + + " System.out.println(new X().b1);\n" + + " }\n" + + " public void bar13(int i) {\n" + + " System.out.println(b1);\n" + + " }\n" + + " public void bar14(int i) {\n" + + " System.out.println(this.b1);\n" + + " }\n" + + " public void bar15(int i) {\n" + + " xfield.field1 = 1;\n" + + " }\n" + + " public void bar16(int i) {\n" + + " System.out.println(xfield.field1);\n" + + " }\n" + + " public void baz() {\n" + + " }\n" + + "}\n" + + "class A{\n" + + " public static X xA;\n" + + "}\n" + + "class B{\n" + + " public static int b1;\n" + + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 4)\n" + + " public void bar1(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar1(int) from the type X can be declared as static\n" + + "----------\n" + + "2. ERROR in X.java (at line 10)\n" + + " public void bar3(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar3(int) from the type X can be declared as static\n" + + "----------\n" + + "3. ERROR in X.java (at line 16)\n" + + " public void bar5(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar5(int) from the type X can be declared as static\n" + + "----------\n" + + "4. ERROR in X.java (at line 20)\n" + + " public void bar6(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar6(int) from the type X can be declared as static\n" + + "----------\n" + + "5. ERROR in X.java (at line 23)\n" + + " public void bar7(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar7(int) from the type X can be declared as static\n" + + "----------\n" + + "6. ERROR in X.java (at line 29)\n" + + " public void bar9(int i) {\n" + + " ^^^^^^^^^^^\n" + + "The method bar9(int) from the type X can be declared as static\n" + + "----------\n" + + "7. ERROR in X.java (at line 38)\n" + + " public void bar12(int i) {\n" + + " ^^^^^^^^^^^^\n" + + "The method bar12(int) from the type X can be declared as static\n" + + "----------\n" + + "8. ERROR in X.java (at line 41)\n" + + " public void bar13(int i) {\n" + + " ^^^^^^^^^^^^\n" + + "The method bar13(int) from the type X can be declared as static\n" + + "----------\n" + + "9. ERROR in X.java (at line 47)\n" + + " public void bar15(int i) {\n" + + " ^^^^^^^^^^^^\n" + + "The method bar15(int) from the type X can be declared as static\n" + + "----------\n" + + "10. ERROR in X.java (at line 50)\n" + + " public void bar16(int i) {\n" + + " ^^^^^^^^^^^^\n" + + "The method bar16(int) from the type X can be declared as static\n" + + "----------\n" + + "11. ERROR in X.java (at line 53)\n" + + " public void baz() {\n" + + " ^^^^^\n" + + "The method baz() from the type X can be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 +// Verify that can be static is not raised on overriden or static methods. +public void test118() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); + this.runNegativeTest( + new String[] { + "X.java", + "public class X extends B implements A{\n" + + " public static int field1 = 1;\n" + + " public static void methodX(){\n" + + " }\n" + + " public void methodB() {\n" + // overriding method, dont warn + " System.out.println(X.field1);\n" + + " X.methodX();\n" + + " }\n" + + " public void methodA() {\n" + // implementing method, dont warn + " System.out.println();\n" + + " }\n" + + "}\n" + + "interface A{\n" + + " public abstract void methodA();\n" + + "}\n" + + "class B{\n" + + " public void methodB() {\n" + + " System.out.println();\n" + + " }\n" + + "}", + }, + "----------\n" + + "1. ERROR in X.java (at line 17)\n" + + " public void methodB() {\n" + + " ^^^^^^^^^\n" + + "The method methodB() from the type B can be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} } #P org.eclipse.jdt.doc.isv Index: guide/jdt_api_options.htm =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.doc.isv/guide/jdt_api_options.htm,v retrieving revision 1.77 diff -u -r1.77 jdt_api_options.htm --- guide/jdt_api_options.htm 6 Oct 2010 13:57:32 -0000 1.77 +++ guide/jdt_api_options.htm 16 Oct 2010 20:07:00 -0000 @@ -666,6 +666,21 @@ +Reporting that a method can be declared static (COMPILER_PB_METHOD_CAN_BE_STATIC) + + +When enabled, the compiler will issue an error or a warning if a method has +not be declared as static, even though it qualifies as one. +ERROR + + +WARNING + + +IGNORE + + + Reporting Method With Constructor Name (COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME)