Eclipse plugin EclipseJDT设置项目

Eclipse plugin EclipseJDT设置项目,eclipse-plugin,eclipse-rcp,eclipse-pde,eclipse-jdt,Eclipse Plugin,Eclipse Rcp,Eclipse Pde,Eclipse Jdt,我是Eclipse JDT的初学者。我正在阅读一些教程,找到了一个创建java文件的好例子。在下面的示例中,他们将在哪个项目中创建包和java文件。我看不到任何指向任何项目名称的代码。如果我错了,请让我明白。我只是运行下面的示例。我看不到任何输出 AST-AST=AST.newAST(AST.JLS3); CompilationUnit=ast.newCompilationUnit(); PackageDeclaration PackageDeclaration=ast.newPackageDe

我是Eclipse JDT的初学者。我正在阅读一些教程,找到了一个创建java文件的好例子。在下面的示例中,他们将在哪个项目中创建包和java文件。我看不到任何指向任何项目名称的代码。如果我错了,请让我明白。我只是运行下面的示例。我看不到任何输出

AST-AST=AST.newAST(AST.JLS3);
CompilationUnit=ast.newCompilationUnit();
PackageDeclaration PackageDeclaration=ast.newPackageDeclaration();
packageDeclaration.setName(ast.newSimpleName(“示例”);
单位:setPackage(包装声明);
ImportDeclaration ImportDeclaration=ast.newImportDeclaration();
限定名称=
ast.newQualifiedName(
ast.newSimpleName(“java”),
ast.newSimpleName(“util”);
importDeclaration.setName(名称);
importDeclaration.setOnDemand(true);
unit.imports().add(importDeclaration);
TypeDeclaration type=ast.newTypeDeclaration();
type.setInterface(false);
type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_关键字));
type.setName(ast.newSimpleName(“HelloWorld”);
MethodDeclaration MethodDeclaration=ast.newMethodDeclaration();
methodDeclaration.setConstructor(false);
列表修饰符=methodDeclaration.modifiers();
add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_关键字));
add(ast.newModifier(Modifier.ModifierKeyword.STATIC_关键字));
methodDeclaration.setName(ast.newSimpleName(“main”));
setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
SingleVariableDeclaration variableDeclaration=ast.newSingleVariableDeclaration();
variableDeclaration.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName(“字符串”))));
variableDeclaration.setName(ast.newSimpleName(“args”);
methodDeclaration.parameters().add(variableDeclaration);
org.eclipse.jdt.core.dom.Block=ast.newBlock();
MethodInvocation MethodInvocation=ast.newMethodInvocation();
姓名=
ast.newQualifiedName(
ast.newSimpleName(“系统”),
ast.newSimpleName(“out”);
methodInvocation.setExpression(名称);
methodInvocation.setName(ast.newSimpleName(“println”);
InfixExpression InfixExpression=ast.newInfixExpression();
infixExpression.setOperator(infixExpression.Operator.PLUS);
StringLiteral=ast.newStringLiteral();
setLiteralValue(“Hello”);
infixExpression.setLeftOperator(文字);
literal=ast.newStringLiteral();
literal.setLiteralValue(“世界”);
infixExpression.SetRightOperator(文字);
methodInvocation.arguments().add(infixExpression);
ExpressionStatement ExpressionStatement=ast.newExpressionStatement(方法调用);
block.statements().add(expressionStatement);
方法声明:立根体(块);
type.bodyDeclarations().add(methodDeclaration);
unit.types().add(类型);
您可以看看。它使用Java模型,这意味着您需要创建一个插件使其工作

//创建一个名为“TESTJDT”的项目
IWorkspaceRoot root=ResourcesPlugin.getWorkspace().getRoot();
IProject project=root.getProject(“TESTJDT”);
project.create(空);
project.open(空);
//设置Java特性
IProjectDescription description=project.getDescription();
setnatureid(新字符串[]{JavaCore.NATURE_ID});
//创建项目
project.setDescription(description,null);
IJavaProject=JavaCore.create(项目);
//设置生成路径
IClasspathEntry[]构建路径={
JavaCore.newSourceEntry(project.getFullPath().append(“src”),
JavaRuntime.getDefaultJREContainerEntry()};
setRawClasspath(buildPath,project.getFullPath().append(
“bin”),无效);
//使用资源包创建文件夹
IFolder folder=project.getFolder(“src”);
folder.create(true、true、null);
//将文件夹添加到Java元素
IPackageFragmentRoot srcFolder=javaProject
.getPackageFragmentRoot(文件夹);
//创建包片段
IPackageFragment=srcFolder.createPackageFragment(
“com.programcreek”,true,null);
//初始化代码字符串并创建编译单元
String str=“package com.programcreek;”“+”\n”
+公共类测试{“+”\n“+”私有字符串名称
+“\n”+“}”;
ICompilationUnit cu=fragment.createCompilationUnit(“Test.java”,str,
假,空);
//创建一个字段
IType type=cu.getType(“测试”);
type.createField(“私有字符串年龄;”,null,true,null);