AST in JDT
来源:互联网 发布:java软件开发技术 编辑:程序博客网 时间:2024/05/19 20:59
如何创建一个边一单元CompilationUnit:
ASTParser parser = ASTParser.newParser(AST.JLS2);parser.setSource("".toCharArray());CompilationUnit unit = (CompilationUnit) parser.createAST(null); unit.recordModifications();AST ast = unit.getAST();注意:null代表不需要ProgressMonitor
以下代码抄自eclipse.jdt的help:
AST ast = AST.newAST(AST.JLS3); //支持JDT5.0的
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);
- AST in JDT
- jdt中的ast工具
- Eclipse JDT--AST入门
- Eclipse JDT--AST入门
- Eclipse JDT--AST入门
- JDT AST学习笔记
- 使用Eclipse JDT 构建AST
- 利用Eclipse JDT抽取Java AST
- Java Model in JDT
- Atitit org.eclipse.jdt 的ast 架构 Eclipse JDT API spec
- AST
- ast
- AST
- The Eclipse JDT Core jar is not in the classpath
- Drools执行报错 The Eclipse JDT Core jar is not in the classpath .
- Plug-in org.eclipse.jdt.ui was unable to load class
- Plug-in org.eclipse.jdt.ui was unable to load class
- Plug-in org.eclipse.jdt.ui was unable to load class
- 4款免费软件让你在Windows下访问Linux文件系统
- Android上如何查看CPU和内存信息
- Spring 3.0 注解注入详解
- Android异常总结---W/System.err(661): java.lang.IllegalStateException: End of document.
- 智能指针(三):unique_ptr使用简介
- AST in JDT
- 算法题目练习
- 最长公共上升子序列(LCIS)的O(n^2)算法
- C语言中的位运算和逻辑运算
- Eclipse更改默认字符集 设置UTF-8
- iOS Cookbook1 Objective-C 字符串
- 腾讯马拉松-为什么时光不能倒流-hdu4510
- hdu 3068(扩展KMP)
- 黑马程序员 面试技巧<二>--->交通灯管理系统