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

The following examples show how to use org.eclipse.jdt.core.compiler.IProblem#NonStaticFieldFromStaticInvocation . 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: StaticReferenceQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNonStaticFieldFromStaticInvocation() throws Exception {
	// Problem we are testing
	int problem = IProblem.NonStaticFieldFromStaticInvocation;

	IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class A {\n");
	buf.append("	int x = 0;"); // is not static
	buf.append("	public static void main(String[] args) {\n");
	buf.append("		System.out.println(x);\n"); // referencing in static context
	buf.append("	}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack.createCompilationUnit("A.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class A {\n");
	buf.append("	static int x = 0;"); // added static
	buf.append("	public static void main(String[] args) {\n");
	buf.append("		System.out.println(x);\n");
	buf.append("	}\n");
	buf.append("}\n");

	Expected e1 = new Expected("Change 'x' to 'static'", buf.toString());

	Range selection = CodeActionUtil.getRange(cu, "FOOBAR");
	assertCodeActions(cu, selection, e1);
}
 
Example 2
Source File: StaticReferenceQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInstanceMethodDuringConstructorInvocation() throws Exception {
	// Problem we are testing
	int problem = IProblem.NonStaticFieldFromStaticInvocation;

	IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class A {\n");
	buf.append("	A (long x) {\n");
	buf.append("		this(test());\n"); // referencing in static context
	buf.append("	}\n");
	buf.append("	\n");
	buf.append("	int test() {\n");
	buf.append("		return 0;\n");
	buf.append("	}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack.createCompilationUnit("A.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class A {\n");
	buf.append("	A (long x) {\n");
	buf.append("		this(test());\n"); // referencing in static context
	buf.append("	}\n");
	buf.append("	\n");
	buf.append("	static int test() {\n");
	buf.append("		return 0;\n");
	buf.append("	}\n");
	buf.append("}\n");

	Expected e1 = new Expected("Change 'test()' to 'static'", buf.toString());

	Range selection = CodeActionUtil.getRange(cu, "FOOBAR");
	assertCodeActions(cu, selection, e1);
}
 
Example 3
Source File: StaticReferenceQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInstanceFieldDuringConstructorInvocation() throws Exception {
	// Problem we are testing
	int problem = IProblem.NonStaticFieldFromStaticInvocation;

	IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class A {\n");
	buf.append("	int i;");
	buf.append("	A () {\n");
	buf.append("		this(i);\n"); // referencing in static context
	buf.append("	}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack.createCompilationUnit("A.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class A {\n");
	buf.append("	static int i;");
	buf.append("	A () {\n");
	buf.append("		this(i);\n"); // referencing in static context
	buf.append("	}\n");
	buf.append("}\n");

	Expected e1 = new Expected("Change 'i' to 'static'", buf.toString());

	Range selection = CodeActionUtil.getRange(cu, "FOOBAR");
	assertCodeActions(cu, selection, e1);
}