本文整理匯總了Java中org.eclipse.jdt.core.IPackageFragment.createCompilationUnit方法的典型用法代碼示例。如果您正苦於以下問題:Java IPackageFragment.createCompilationUnit方法的具體用法?Java IPackageFragment.createCompilationUnit怎麽用?Java IPackageFragment.createCompilationUnit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.eclipse.jdt.core.IPackageFragment
的用法示例。
在下文中一共展示了IPackageFragment.createCompilationUnit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testStaticMethodInInterface4
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testStaticMethodInInterface4() throws Exception {
StringBuilder buf = new StringBuilder();
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("interface I {\n");
buf.append(" int i= n();\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("I.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("interface I {\n");
buf.append(" int i= n();\n");
buf.append("\n");
buf.append(" static int n() {\n");
buf.append(" return 0;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Create method 'n()'", buf.toString());
assertCodeActions(cu, e1);
}
示例2: testVarInArray
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testVarInArray() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo(Object[] arr) {\n");
buf.append(" for (int i = 0; i > arr.lenght; i++) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo(Object[] arr) {\n");
buf.append(" for (int i = 0; i > arr.length; i++) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Change to 'length'", buf.toString());
assertCodeActionExists(cu, e1);
}
示例3: createDummyClass
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
public static IFile createDummyClass(IJavaProject project) throws CoreException, IOException {
String clazz = "import org.graphwalker.core.machine.ExecutionContext ; public class Dummy extends org.gw4e.core.machine.ExecutionContext {}";
IFolder folder = project.getProject().getFolder("src/test/java");
IPackageFragmentRoot srcFolder = project.getPackageFragmentRoot(folder);
IPackageFragment pkg = srcFolder.getPackageFragment("");
ICompilationUnit cu = pkg.createCompilationUnit("Dummy.java", clazz, false, new NullProgressMonitor());
cu.save(new NullProgressMonitor (), true);
return (IFile) cu.getResource();
}
示例4: createDummyClassWitherror
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
public static IFile createDummyClassWitherror(IJavaProject project) throws CoreException, IOException {
String clazz = "import org.graphwalker.core.machine.ExecutionContext ; public class Dummy1 extends org.gw4e.core.machine.ExecutionContext {}";
IFolder folder = project.getProject().getFolder("src/test/java");
IPackageFragmentRoot srcFolder = project.getPackageFragmentRoot(folder);
IPackageFragment pkg = srcFolder.getPackageFragment("");
ICompilationUnit cu = pkg.createCompilationUnit("Dummy.java", clazz, false, new NullProgressMonitor());
cu.save(new NullProgressMonitor (), true);
return (IFile) cu.getResource();
}
示例5: testVarInInitializer
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testVarInInitializer() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" private int i= k;\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" private int k;\n");
buf.append(" private int i= k;\n");
buf.append("}\n");
Expected e1 = new Expected("Create field 'k'", buf.toString());
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" private static final int k = 0;\n");
buf.append(" private int i= k;\n");
buf.append("}\n");
Expected e2 = new Expected("Create constant 'k'", buf.toString());
assertCodeActions(cu, e1, e2);
}
示例6: testMethodInForInit
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testMethodInForInit() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" for (int i= 0, j= goo(3); i < 0; i++) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" for (int i= 0, j= goo(3); i < 0; i++) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private int goo(int i) {\n");
buf.append(" return 0;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Create method 'goo(int)'", buf.toString());
assertCodeActionExists(cu, e1);
}
示例7: testMissingMethodComment1
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testMissingMethodComment1() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.io.IOException;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" public <A> void foo(int a) throws IOException {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.io.IOException;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" /**\n");
buf.append(" * @param <A>\n");
buf.append(" * @param a\n");
buf.append(" * @throws IOException\n");
buf.append(" */\n");
buf.append(" public <A> void foo(int a) throws IOException {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add Javadoc comment", buf.toString());
assertCodeActions(cu, e1);
}
示例8: testClassInstanceCreationInGenericType
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testClassInstanceCreationInGenericType() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" A<String> a= new A<String>(i);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class A<T> {\n");
buf.append("}\n");
pack1.createCompilationUnit("A.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" A<String> a= new A<String>();\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Remove argument to match 'A<String>()'", buf.toString());
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class A<T> {\n");
buf.append("\n");
buf.append(" public A(int i) {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e2 = new Expected("Create constructor 'A<T>(int)'", buf.toString());
assertCodeActions(cu, e1, e2);
}
示例9: testClassInstanceCreation
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testClassInstanceCreation() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" A a= new A(i);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append("}\n");
pack1.createCompilationUnit("A.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void foo(int i) {\n");
buf.append(" A a= new A();\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Remove argument to match 'A()'", buf.toString());
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append("\n");
buf.append(" public A(int i) {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e2 = new Expected("Create constructor 'A(int)'", buf.toString());
assertCodeActions(cu, e1, e2);
}
示例10: testMissingReturn2
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testMissingReturn2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" /**\n");
buf.append(" */\n");
buf.append(" public int foo() {\n");
buf.append(" return 1;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" /**\n");
buf.append(" * @return \n");
buf.append(" */\n");
buf.append(" public int foo() {\n");
buf.append(" return 1;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@return' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
assertCodeActions(cu, e1, e2);
}
示例11: testIndirectProtectedMethod
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
/**
* Visibility: fix for public or protected not appropriate.
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=65876#c5
*
* @throws Exception
* if anything goes wrong
* @since 3.9
*/
@Test
@Ignore("Requires ModifierCorrectionSubProcessor")
public void testIndirectProtectedMethod() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
IPackageFragment pack2 = fSourceFolder.createPackageFragment("test2", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append(" protected void method() {\n");
buf.append(" }\n");
buf.append("}\n");
pack1.createCompilationUnit("A.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test2;\n");
buf.append("import test1.A;\n");
buf.append("public class B extends A {\n");
buf.append(" private void bMethod() {\n");
buf.append(" A a = new A();\n");
buf.append(" a.method();\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack2.createCompilationUnit("B.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append(" public void method() {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add Javadoc comment", buf.toString());
assertCodeActions(cu, e1);
}
示例12: testTypeMismatchWithTypeInSamePackage
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testTypeMismatchWithTypeInSamePackage() throws Exception {
// test for bug 198586
IPackageFragment pack2 = fSourceFolder.createPackageFragment("test2", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {}\n");
pack2.createCompilationUnit("E.java", buf.toString(), false, null);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {}\n");
pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class Test {\n");
buf.append(" test2.E e2= new Object();\n");
buf.append(" E e1;\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class Test {\n");
buf.append(" test2.E e2= (test2.E) new Object();\n");
buf.append(" E e1;\n");
buf.append("}\n");
Expected e1 = new Expected("Add cast to 'E'", buf.toString());
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class Test {\n");
buf.append(" Object e2= new Object();\n");
buf.append(" E e1;\n");
buf.append("}\n");
Expected e2 = new Expected("Change type of 'e2' to 'Object'", buf.toString());
assertCodeActions(cu, e1, e2);
}
示例13: testMissingFieldComment
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testMissingFieldComment() throws Exception {
Map<String, String> original = fJProject1.getOptions(false);
HashMap<String, String> newOptions = new HashMap<>(original);
// newOptions.put(JavaCore.COMPILER_PB_MISSING_JAVADOC_COMMENTS,
// JavaCore.ERROR);
// newOptions.put(JavaCore.COMPILER_PB_MISSING_JAVADOC_COMMENTS_VISIBILITY,
// JavaCore.PUBLIC);
fJProject1.setOptions(newOptions);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" public static final int COLOR= 1;\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" /**\n");
buf.append(" *\n");
buf.append(" */\n");
buf.append(" public static final int COLOR= 1;\n");
buf.append("}\n");
Expected e1 = new Expected("Add Javadoc comment", buf.toString());
assertCodeActions(cu, e1);
}
示例14: SourceViewer
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
public SourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler,
boolean showAnnotationsOverview, int styles, IAnnotationAccess annotationAccess, ISharedTextColors sharedColors,
IDocument document)
{
super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, SWT.BOLD);
int id = currentId++;
filename = VIEWER_CLASS_NAME + id++ + ".java";
this.sharedColors=sharedColors;
this.annotationAccess=annotationAccess;
this.fOverviewRuler=overviewRuler;
oldAnnotations= new HashMap<ProjectionAnnotation, Position>();
IJavaProject javaProject = JavaCore.create(BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject());
try
{
IPackageFragmentRoot[] ipackageFragmentRootList=javaProject.getPackageFragmentRoots();
IPackageFragmentRoot ipackageFragmentRoot=null;
for(IPackageFragmentRoot tempIpackageFragmentRoot:ipackageFragmentRootList)
{
if(tempIpackageFragmentRoot.getKind()==IPackageFragmentRoot.K_SOURCE
&& StringUtils.equals(PathConstant.TEMP_BUILD_PATH_SETTINGS_FOLDER,tempIpackageFragmentRoot.getPath().removeFirstSegments(1).toString()))
{
ipackageFragmentRoot=tempIpackageFragmentRoot;
break;
}
}
IPackageFragment compilationUnitPackage= ipackageFragmentRoot.createPackageFragment(HYDROGRAPH_COMPILATIONUNIT_PACKAGE, true, new NullProgressMonitor());
compilatioUnit= compilationUnitPackage.createCompilationUnit(filename,document.get(),true, new NullProgressMonitor());
}
catch (Exception exception) {
LOGGER.warn("Exception occurred while initializing source viewer", exception);
} finally {
if (javaProject != null) {
try {
javaProject.close();
} catch (JavaModelException javaModelException) {
LOGGER.warn("Exception occurred while closing java-project", javaModelException);
}
}
}
initializeViewer(document);
updateContents();
}
示例15: testParameterMismatchChangeMethodTypeInGeneric
import org.eclipse.jdt.core.IPackageFragment; //導入方法依賴的package包/類
@Test
public void testParameterMismatchChangeMethodTypeInGeneric() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E<T> {\n");
buf.append(" public void goo(Vector<String> v) {\n");
buf.append(" }\n");
buf.append(" public int foo() {\n");
buf.append(" goo(this.foo());\n");
buf.append(" return 9;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E<T> {\n");
buf.append(" public void goo(Vector<String> v) {\n");
buf.append(" }\n");
buf.append(" public Vector<String> foo() {\n");
buf.append(" goo(this.foo());\n");
buf.append(" return 9;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Change return type of 'foo(..)' to 'Vector<String>'", buf.toString());
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E<T> {\n");
buf.append(" public void goo(int i) {\n");
buf.append(" }\n");
buf.append(" public int foo() {\n");
buf.append(" goo(this.foo());\n");
buf.append(" return 9;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e2 = new Expected("Change method 'goo(Vector<String>)' to 'goo(int)'", buf.toString());
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E<T> {\n");
buf.append(" public void goo(Vector<String> v) {\n");
buf.append(" }\n");
buf.append(" public int foo() {\n");
buf.append(" goo(this.foo());\n");
buf.append(" return 9;\n");
buf.append(" }\n");
buf.append(" private void goo(int foo) {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e3 = new Expected("Create method 'goo(int)'", buf.toString());
assertCodeActions(cu, e1, e2, e3);
}