Java Code Examples for org.eclipse.jdt.core.compiler.IProblem#NonStaticAccessToStaticMethod

The following examples show how to use org.eclipse.jdt.core.compiler.IProblem#NonStaticAccessToStaticMethod . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaticAccessQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testNonStaticAccessToStaticMethod() throws Exception {
	int problem = IProblem.NonStaticAccessToStaticMethod;
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

	// Referenced from: https://www.intertech.com/Blog/a-static-method-should-be-accessed-in-a-static-way/
	StringBuilder bufA = new StringBuilder();
	bufA.append("package test1;\n");
	bufA.append("public class HelloWorld {\n");
	bufA.append("  public static void sayHello() {\n");
	bufA.append("    System.out.println(\"Hello!\");\n");
	bufA.append("  }\n");
	bufA.append("  public static void main(String[] args) {\n");
	bufA.append("    HelloWorld hw = new HelloWorld();\n");
	bufA.append("    hw.sayHello();\n");
	bufA.append("  }\n");
	bufA.append("}");
	ICompilationUnit cu = pack1.createCompilationUnit("HelloWorld.java", bufA.toString(), false, null);

	bufA = new StringBuilder();
	bufA.append("package test1;\n");
	bufA.append("public class HelloWorld {\n");
	bufA.append("  public void sayHello() {\n");
	bufA.append("    System.out.println(\"Hello!\");\n");
	bufA.append("  }\n");
	bufA.append("  public static void main(String[] args) {\n");
	bufA.append("    HelloWorld hw = new HelloWorld();\n");
	bufA.append("    hw.sayHello();\n");
	bufA.append("  }\n");
	bufA.append("}");

	Expected e1 = new Expected("Remove 'static' modifier of 'sayHello()'", bufA.toString());

	bufA = new StringBuilder();
	bufA.append("package test1;\n");
	bufA.append("public class HelloWorld {\n");
	bufA.append("  public static void sayHello() {\n");
	bufA.append("    System.out.println(\"Hello!\");\n");
	bufA.append("  }\n");
	bufA.append("  public static void main(String[] args) {\n");
	bufA.append("    HelloWorld hw = new HelloWorld();\n");
	bufA.append("    HelloWorld.sayHello();\n");
	bufA.append("  }\n");
	bufA.append("}");

	Expected e2 = new Expected("Change access to static using 'HelloWorld' (declaring type)", bufA.toString());
	assertCodeActions(cu, e1, e2);

}
 
Example 2
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isNonStaticAccess(IProblemLocation problem) {
	return (problem.getProblemId() == IProblem.NonStaticAccessToStaticField
			|| problem.getProblemId() == IProblem.NonStaticAccessToStaticMethod
			|| problem.getProblemId() == IProblem.NonStaticOrAlienTypeReceiver);
}