eclipse JDT设置项目

时间:2013-04-15 12:38:10

标签: eclipse-plugin eclipse-rcp eclipse-pde eclipse-jdt

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

        AST ast = AST.newAST(AST.JLS3);
        CompilationUnit unit = ast.newCompilationUnit();
        PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
        packageDeclaration.setName(ast.newSimpleName("example"));
        unit.setPackage(packageDeclaration);
        ImportDeclaration importDeclaration = ast.newImportDeclaration();
        QualifiedName name = 
            ast.newQualifiedName(
                ast.newSimpleName("java"),
                ast.newSimpleName("util"));
        importDeclaration.setName(name);
        importDeclaration.setOnDemand(true);
        unit.imports().add(importDeclaration);
        TypeDeclaration type = ast.newTypeDeclaration();
        type.setInterface(false);
        type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        type.setName(ast.newSimpleName("HelloWorld"));
        MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
        methodDeclaration.setConstructor(false);
        List modifiers = methodDeclaration.modifiers();
        modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
        methodDeclaration.setName(ast.newSimpleName("main"));
        methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
        SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("String"))));
        variableDeclaration.setName(ast.newSimpleName("args"));
        methodDeclaration.parameters().add(variableDeclaration);
        org.eclipse.jdt.core.dom.Block block = ast.newBlock();
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        name = 
            ast.newQualifiedName(
                ast.newSimpleName("System"),
                ast.newSimpleName("out"));
        methodInvocation.setExpression(name);
        methodInvocation.setName(ast.newSimpleName("println")); 
        InfixExpression infixExpression = ast.newInfixExpression();
        infixExpression.setOperator(InfixExpression.Operator.PLUS);
        StringLiteral literal = ast.newStringLiteral();
        literal.setLiteralValue("Hello");
        infixExpression.setLeftOperand(literal);
        literal = ast.newStringLiteral();
        literal.setLiteralValue(" world");
        infixExpression.setRightOperand(literal);
        methodInvocation.arguments().add(infixExpression);
        ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
        block.statements().add(expressionStatement);
        methodDeclaration.setBody(block);
        type.bodyDeclarations().add(methodDeclaration);
        unit.types().add(type);

1 个答案:

答案 0 :(得分:0)

您可以查看this。它使用Java Model,这意味着您需要创建一个插件才能使其工作。

// create a project with name "TESTJDT"
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("TESTJDT");
project.create(null);
project.open(null);

//set the Java nature
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] { JavaCore.NATURE_ID });

//create the project
project.setDescription(description, null);
IJavaProject javaProject = JavaCore.create(project);

//set the build path
IClasspathEntry[] buildPath = {
        JavaCore.newSourceEntry(project.getFullPath().append("src")),
                JavaRuntime.getDefaultJREContainerEntry() };

javaProject.setRawClasspath(buildPath, project.getFullPath().append(
                "bin"), null);

//create folder by using resources package
IFolder folder = project.getFolder("src");
folder.create(true, true, null);

//Add folder to Java element
IPackageFragmentRoot srcFolder = javaProject
                .getPackageFragmentRoot(folder);

//create package fragment
IPackageFragment fragment = srcFolder.createPackageFragment(
        "com.programcreek", true, null);

//init code string and create compilation unit
String str = "package com.programcreek;" + "\n"
    + "public class Test  {" + "\n" + " private String name;"
    + "\n" + "}";

        ICompilationUnit cu = fragment.createCompilationUnit("Test.java", str,
                false, null);

//create a field
IType type = cu.getType("Test");

type.createField("private String age;", null, true, null);