List 5 生成Spuer Invocation
// super(parent,style)
- SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
- constructorBlock.statements().add(superConstructorInvocation);
- Expression exp = ast.newSimpleName("parent");
- superConstructorInvocation.arguments().add(exp);
- superConstructorInvocation.arguments().add(ast.newSimpleName("style"));
List 6 生成ClassInstanceCreation
// GridLayout gridLayout = new GridLayout();
- VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
- vdf.setName(ast.newSimpleName("gridLayout"));
- ClassInstanceCreation cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("GridLayout"));
- vdf.setInitializer(cc);
- VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
- vds.setType(ast.newSimpleType(ast.newSimpleName("GridLayout")));
- constructBlock.statements().add(vds);
// Label label = new Label(this,SWT.NONE);
- VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
- vdf.setName(ast.newSimpleName("label"));
- cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("Label"));
- cc.arguments().add(ast.newThisExpression());
- cc.arguments().add(ast.newName(getSimpleNames("SWT.NONE")));
- vdf.setInitializer(cc);
- VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
- vds.setType(ast.newSimpleType(ast.newSimpleName("Label")));
- constructBlock.statements().add(vds);
List 7生成MethodInvocation
// setLayout(gridLayout);
- MethodInvocation mi = ast.newMethodInvocation();
- mi.setName(ast.newSimpleName("setLayout"));
- mi.arguments().add(ast.newSimpleName("gridLayout"));
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
// label.setText("Press the button to close:");
- mi = ast.newMethodInvocation();
- mi.setExpression(ast.newSimpleName("label"));
- mi.setName(ast.newSimpleName("setText"));
- StringLiteral sl = ast.newStringLiteral();
- sl.setLiteralValue("Press the button to close:");
- mi.arguments().add(sl);
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
// label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
java 代码
- mi = ast.newMethodInvocation();
- mi.setExpression(ast.newSimpleName("label"));
- mi.setName(ast.newSimpleName("setLayoutData"));
-
- cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("GridData"));
- cc.arguments().add(ast.newName(getSimpleNames("GridData.HORIZONTAL_ALIGN_CENTER")));
- mi.arguments().add(cc);
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
// Button button = new Button(this,SWT.PUSH);
java 代码
- vdf = ast.newVariableDeclarationFragment();
- vdf.setName(ast.newSimpleName("button"));
- vds = ast.newVariableDeclarationStatement(vdf);
- vds.setType(ast.newSimpleType(ast.newSimpleName("Button")));
- constructorBlock.statements().add(vds);
-
- cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("Button"));
- vdf.setInitializer(cc);
- cc.arguments().add(ast.newThisExpression());
- cc.arguments().add(ast.newName(getSimpleNames("SWT.PUSH")));
// button.addSelectionListener(new SelectionAdapter() {});
java 代码
- mi = ast.newMethodInvocation();
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
- mi.setExpression(ast.newSimpleName("button"));
- mi.setName(ast.newSimpleName("addSelectionListener"));
-
- ClassInstanceCreation ci = ast.newClassInstanceCreation();
- ci.setName(ast.newSimpleName("SelectionAdapter"));
- mi.arguments().add(ci);
- AnonymousClassDeclaration cd = ast.newAnonymousClassDeclaration();
- ci.setAnonymousClassDeclaration(cd);
// public void setup(){super.setUp()}
- MethodDeclaration methodConstructor = ast.newMethodDeclaration();
methodConstructor.modifiers().add(
ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
methodConstructor.setName(ast.newSimpleName("setUp"));
- Block constructorBlock = ast.newBlock();
methodConstructor.setBody(constructorBlock);
- SuperMethodInvocation smi = ast.newSuperMethodInvocation();
smi.setName(ast.newSimpleName("setUp"));
ExpressionStatement es = ast.newExpressionStatement(smi);
constructorBlock.statements().add(es);
- // set exception
List targetExNames = methodConstructor.thrownExceptions();
Name targetExName = ast.newName(Exception.class.getName());
targetExNames.add(targetExName);
- methodConstructor.setJavadoc(JavaDocHelper.getJavaDoc(ast,
JavaDocHelper.setUpDoc, null, null));
classType.bodyDeclarations().add(methodConstructor);
posted on 2008-07-14 13:21
Daniel 阅读(315)
评论(0) 编辑 收藏 所属分类:
Eclipse的相关