Java Code Examples for org.eclipse.jdt.core.ICompilationUnit#createType()

The following examples show how to use org.eclipse.jdt.core.ICompilationUnit#createType() . 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: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test1() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test1.java");
	IType testClass = cu.createType("public class Test1 extends A implements B {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "a()", "b(Vector<Date>)", "c(Hashtable)" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "a", "b", "c", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[] { "java.util.Date", "java.util.Hashtable", "java.util.Vector" }, imports);
}
 
Example 2
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test2() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test2.java");
	IType testClass = cu.createType("public class Test2 extends C implements B {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "d(Hashtable)" }, overridableMethods);
	checkOverridableMethods(new String[] { "c(Hashtable)" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "c", "d", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[] { "java.util.Enumeration", "java.util.Hashtable" }, imports);
}
 
Example 3
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test3() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test3.java");
	IType testClass = cu.createType("public class Test3 extends D {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "c(Hashtable)", "d(Hashtable)" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "c", "d", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[] { "java.util.Hashtable", "java.util.Enumeration" }, imports);
}
 
Example 4
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test4() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test4.java");
	IType testClass = cu.createType("public class Test4 implements B, E {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "c(Hashtable)", "e()" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "c", "e", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[] { "java.util.Hashtable", "java.util.NoSuchElementException" }, imports);
}
 
Example 5
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	fCuA.delete(true, null);

	ICompilationUnit cu = fPackageP.getCompilationUnit("A.java");
	fClassA = cu.createType("public abstract class A {\n}\n", null, true, null);
	fClassA.createMethod("public abstract void a();\n", null, true, null);
	fClassA.createMethod("public abstract void b(java.util.Vector<java.util.Date> v);\n", null, true, null);

	cu = fPackageP.getCompilationUnit("B.java");
	fInterfaceB = cu.createType("public interface B {\n}\n", null, true, null);
	fInterfaceB.createMethod("void c(java.util.Hashtable h);\n", null, true, null);

	cu = fPackageP.getCompilationUnit("C.java");
	fClassC = cu.createType("public abstract class C {\n}\n", null, true, null);
	fClassC.createMethod("public void c(java.util.Hashtable h) {\n}\n", null, true, null);
	fClassC.createMethod("public abstract java.util.Enumeration d(java.util.Hashtable h) {\n}\n", null, true, null);

	cu = fPackageP.getCompilationUnit("D.java");
	fClassD = cu.createType("public abstract class D extends C {\n}\n", null, true, null);
	fClassD.createMethod("public abstract void c(java.util.Hashtable h);\n", null, true, null);

	cu = fPackageP.getCompilationUnit("E.java");
	fInterfaceE = cu.createType("public interface E {\n}\n", null, true, null);
	fInterfaceE.createMethod("void c(java.util.Hashtable h);\n", null, true, null);
	fInterfaceE.createMethod("void e() throws java.util.NoSuchElementException;\n", null, true, null);
}
 
Example 6
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCloneable() throws Exception {
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test4.java");
	IType testClass = cu.createType("public class Test4 implements Cloneable {\n}\n", null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "clone()" }, overridableMethods);

	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "equals", "clone", "toString", "finalize", "hashCode" }, methods);
}
 
Example 7
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug119171() throws Exception {
	StringBuilder buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("import java.util.Properties;\n");
	buf.append("public interface F {\n");
	buf.append("    public void b(Properties p);\n");
	buf.append("}\n");
	fPackageP.createCompilationUnit("F.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("public class Properties {\n");
	buf.append("    public int get() {return 0;}\n");
	buf.append("}\n");
	fPackageP.createCompilationUnit("Properties.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("public class Test5 implements F {\n");
	buf.append("    public void foo() {\n");
	buf.append("        Properties p= new Properties();\n");
	buf.append("        p.get();\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = fPackageP.getCompilationUnit("Test5.java");
	IType testClass = cu.createType(buf.toString(), null, true, null);
	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	addAndApplyOverridableMethods(testClass, overridableMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "foo", "b", "clone", "equals", "finalize", "hashCode", "toString" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[0], imports);
}
 
Example 8
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug297183() throws Exception {
	StringBuilder buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("interface Shape {\r\n");
	buf.append("  int getX();\r\n");
	buf.append("  int getY();\r\n");
	buf.append("  int getEdges();\r\n");
	buf.append("  int getArea();\r\n");
	buf.append("}\r\n");
	fPackageP.createCompilationUnit("Shape.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("interface Circle extends Shape {\r\n");
	buf.append("  int getR();\r\n");
	buf.append("}\r\n");
	buf.append("\r\n");
	fPackageP.createCompilationUnit("Circle.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package P;\n");
	buf.append("public class DefaultCircle implements Circle {\n");
	buf.append("}\n");
	ICompilationUnit cu = fPackageP.getCompilationUnit("DefaultCircle.java");
	IType testClass = cu.createType(buf.toString(), null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "getX()", "getY()", "getEdges()", "getArea()", "getR()" }, overridableMethods);
	List<OverridableMethod> unimplementedMethods = overridableMethods.stream().filter((method) -> method.unimplemented).collect(Collectors.toList());
	addAndApplyOverridableMethods(testClass, unimplementedMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "getX", "getY", "getEdges", "getArea", "getR" }, methods);

	IImportDeclaration[] imports = cu.getImports();
	checkImports(new String[0], imports);
}
 
Example 9
Source File: OverrideMethodsTestCase.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug480682() throws Exception {
	StringBuilder buf = new StringBuilder();
	buf.append("public class Test480682 extends Base {\n");
	buf.append("}\n");
	buf.append("abstract class Base implements I {\n");
	buf.append("    @Override\n");
	buf.append("    public final void method1() {}\n");
	buf.append("}\n");
	buf.append("interface I {\n");
	buf.append("    void method1();\n");
	buf.append("    void method2();\n");
	buf.append("}\n");

	ICompilationUnit cu = fPackageP.createCompilationUnit("Test480682.java", buf.toString(), true, null);
	IType testClass = cu.createType(buf.toString(), null, true, null);

	List<OverridableMethod> overridableMethods = getOverridableMethods(testClass);
	checkUnimplementedMethods(new String[] { "method2()" }, overridableMethods);
	checkUnoverridableMethods(new String[] { "method1()" }, overridableMethods);

	List<OverridableMethod> unimplementedMethods = overridableMethods.stream().filter((method) -> method.unimplemented).collect(Collectors.toList());
	addAndApplyOverridableMethods(testClass, unimplementedMethods);

	IMethod[] methods = testClass.getMethods();
	checkMethods(new String[] { "method2" }, methods);
}
 
Example 10
Source File: GenerateGetterAndSetterTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private IType createNewType(String fQName) throws JavaModelException {
	final String pkg = fQName.substring(0, fQName.lastIndexOf('.'));
	final String typeName = fQName.substring(fQName.lastIndexOf('.') + 1);
	final IPackageFragment fragment = fRoot.createPackageFragment(pkg, true, null);
	final ICompilationUnit unit = fragment.getCompilationUnit(typeName + ".java");
	return unit.createType("public class " + typeName + " {\n}\n", null, true, null);
}
 
Example 11
Source File: ImportOrganizeTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void test1() throws Exception {
	requireJUnitSources();
	ICompilationUnit cu= (ICompilationUnit) javaProject.findElement(new Path("junit/runner/BaseTestRunner.java"));
	assertNotNull("BaseTestRunner.java", cu);
	IPackageFragmentRoot root= (IPackageFragmentRoot)cu.getParent().getParent();
	IPackageFragment pack= root.createPackageFragment("mytest", true, null);
	ICompilationUnit colidingCU= pack.getCompilationUnit("TestListener.java");
	colidingCU.createType("public abstract class TestListener {\n}\n", null, true, null);
	String[] order= new String[0];
	IChooseImportQuery query= createQuery("BaseTestRunner", new String[] { "junit.framework.TestListener" }, new int[] { 2 });
	OrganizeImportsOperation op = createOperation(cu, order, false, true, true, query);
	TextEdit edit = op.createTextEdit(new NullProgressMonitor());
	IDocument document = new Document(cu.getSource());
	edit.apply(document);
	try {
		cu.becomeWorkingCopy(new NullProgressMonitor());
		cu.getBuffer().setContents(document.get());
		cu.reconcile(ICompilationUnit.NO_AST, true, null, new NullProgressMonitor());
		//@formatter:off
		assertImports(cu, new String[] {
			"java.io.BufferedReader",
			"java.io.File",
			"java.io.FileInputStream",
			"java.io.FileOutputStream",
			"java.io.IOException",
			"java.io.InputStream",
			"java.io.PrintWriter",
			"java.io.StringReader",
			"java.io.StringWriter",
			"java.lang.reflect.InvocationTargetException",
			"java.lang.reflect.Method",
			"java.lang.reflect.Modifier",
			"java.text.NumberFormat",
			"java.util.Properties",
			"junit.framework.AssertionFailedError",
			"junit.framework.Test",
			"junit.framework.TestListener",
			"junit.framework.TestSuite"
		});
		//@formatter:on
	} finally {
		cu.discardWorkingCopy();
	}
}
 
Example 12
Source File: ImportOrganizeTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void test1WithOrder() throws Exception {
	requireJUnitSources();
	ICompilationUnit cu = (ICompilationUnit) javaProject.findElement(new Path("junit/runner/BaseTestRunner.java"));
	assertNotNull("BaseTestRunner.java is missing", cu);
	IPackageFragmentRoot root= (IPackageFragmentRoot)cu.getParent().getParent();
	IPackageFragment pack= root.createPackageFragment("mytest", true, null);
	ICompilationUnit colidingCU= pack.getCompilationUnit("TestListener.java");
	colidingCU.createType("public abstract class TestListener {\n}\n", null, true, null);
	String[] order= new String[] { "junit", "java.text", "java.io", "java" };
	IChooseImportQuery query= createQuery("BaseTestRunner", new String[] { "junit.framework.TestListener" }, new int[] { 2 });
	OrganizeImportsOperation op = createOperation(cu, order, false, true, true, query);
	TextEdit edit = op.createTextEdit(new NullProgressMonitor());
	IDocument document = new Document(cu.getSource());
	edit.apply(document);
	try {
		cu.becomeWorkingCopy(new NullProgressMonitor());
		cu.getBuffer().setContents(document.get());
		cu.reconcile(ICompilationUnit.NO_AST, true, null, new NullProgressMonitor());
		//@formatter:off
		assertImports(cu, new String[] {
			"junit.framework.AssertionFailedError",
			"junit.framework.Test",
			"junit.framework.TestListener",
			"junit.framework.TestSuite",
			"java.text.NumberFormat",
			"java.io.BufferedReader",
			"java.io.File",
			"java.io.FileInputStream",
			"java.io.FileOutputStream",
			"java.io.IOException",
			"java.io.InputStream",
			"java.io.PrintWriter",
			"java.io.StringReader",
			"java.io.StringWriter",
			"java.lang.reflect.InvocationTargetException",
			"java.lang.reflect.Method",
			"java.lang.reflect.Modifier",
			"java.util.Properties"
		});
		//@formatter:on
	} finally {
		cu.discardWorkingCopy();
	}
}