List of usage examples for org.eclipse.jdt.core.dom ImportDeclaration setName
public void setName(Name name)
From source file:br.com.objectos.way.core.code.jdt.ASTTest.java
License:Apache License
@SuppressWarnings("unchecked") public void stackoverflow_answer() { AST ast = AST.newAST(AST.JLS8);/*from w ww. ja v a 2 s . c om*/ CompilationUnit cu = ast.newCompilationUnit(); PackageDeclaration p1 = ast.newPackageDeclaration(); p1.setName(ast.newSimpleName("foo")); cu.setPackage(p1); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] { "java", "util", "Set" })); cu.imports().add(id); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("Foo")); TypeParameter tp = ast.newTypeParameter(); tp.setName(ast.newSimpleName("X")); td.typeParameters().add(tp); cu.types().add(td); MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); md.setName(ast.newSimpleName("bar")); SingleVariableDeclaration var = ast.newSingleVariableDeclaration(); var.setType(ast.newSimpleType(ast.newSimpleName("String"))); var.setName(ast.newSimpleName("a")); md.parameters().add(var); td.bodyDeclarations().add(md); Block block = ast.newBlock(); md.setBody(block); MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("x")); ExpressionStatement e = ast.newExpressionStatement(mi); block.statements().add(e); System.out.println(cu); }
From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java
License:Apache License
private static void merge(CompilationUnit unit, String pkgName, String typeName, String auth, String dbName, List<String> tableCreators) { unit.recordModifications();/*from w ww .java 2 s . c om*/ AST ast = unit.getAST(); TypeDeclaration type = (TypeDeclaration) unit.types().get(0); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName("cn.ieclipse.aorm.Session")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.content.UriMatcher")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.database.sqlite.SQLiteDatabase")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.database.sqlite.SQLiteOpenHelper")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("java.net.Uri")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.content.ContentValue")); unit.imports().add(id); VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("AUTH")); StringLiteral sl = ast.newStringLiteral(); sl.setLiteralValue(auth); vdf.setInitializer(sl); FieldDeclaration fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))); fd.setType(ast.newSimpleType(ast.newSimpleName("String"))); int i = 0; type.bodyDeclarations().add(i++, fd); // URI = Uri.parse("content://" + AUTH); vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("URI")); MethodInvocation mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("Uri")); mi.setName(ast.newSimpleName("parse")); InfixExpression fix = ast.newInfixExpression(); fix.setOperator(InfixExpression.Operator.PLUS); sl = ast.newStringLiteral(); sl.setLiteralValue("content://"); fix.setLeftOperand(sl); fix.setRightOperand(ast.newSimpleName("AUTH")); mi.arguments().add(fix); vdf.setInitializer(mi); fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))); fd.setType(ast.newSimpleType(ast.newSimpleName("Uri"))); type.bodyDeclarations().add(i++, fd); // private mOpenHelper; vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("mOpenHelper")); fd = ast.newFieldDeclaration(vdf); fd.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD)); fd.setType(ast.newSimpleType(ast.newName("SQLiteOpenHelper"))); type.bodyDeclarations().add(i++, fd); // private static session; vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("session")); fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PRIVATE | Modifier.STATIC))); fd.setType(ast.newSimpleType(ast.newName("Session"))); type.bodyDeclarations().add(i++, fd); // public static Session getSession(){ // return session; // } MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC))); md.setReturnType2(ast.newSimpleType(ast.newName("Session"))); md.setName(ast.newSimpleName("getSession")); Block methodBlock = ast.newBlock(); ReturnStatement returnStmt = ast.newReturnStatement(); returnStmt.setExpression(ast.newSimpleName("session")); methodBlock.statements().add(returnStmt); md.setBody(methodBlock); type.bodyDeclarations().add(i, md); // modify onCreate rewriteOnCreate(unit, dbName, tableCreators); }
From source file:com.android.icu4j.srcgen.RunWithAnnotator.java
License:Apache License
private void appendImport(CompilationUnit cu, ASTRewrite rewriter, Name name) { ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY); AST ast = cu.getAST();//from w ww.ja v a 2s. c o m ImportDeclaration importDeclaration = ast.newImportDeclaration(); importDeclaration.setName(name); lrw.insertLast(importDeclaration, null); }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaSrcDir_Files.java
License:Open Source License
/** * @flowerModelElementId _IEIOA-rfEd6QaI9daHlzjA *//*from w w w.j a v a 2s.c om*/ @SuppressWarnings("unchecked") @Override protected void addImportToCurrentCompilationUnit(String fullyQualifiedTypeName, CompilationUnit currentCompilationUnit) { AST ast = currentCompilationUnit.getAST(); ImportDeclaration importDeclaration = ast.newImportDeclaration(); try { importDeclaration.setName(ast.newName(fullyQualifiedTypeName)); currentCompilationUnit.imports().add(importDeclaration); } catch (Exception e) { // @author Sorin // Usually this exception it thrown when from a generic type name we try to create an ast to insert it into the imports declarations. // Mostly this happens when the documentation for example is changed, and the forwarding mechanism will pass through all the features // which try to add an import declaration but this is not really needed. } }
From source file:com.google.gdt.eclipse.designer.builders.GwtBuilder.java
License:Open Source License
private static void addImport(CompilationUnit compilationUnit, String qualifiedName) { AST ast = compilationUnit.getAST();/*from w w w. j a va2s .c o m*/ List<ImportDeclaration> imports = DomGenerics.imports(compilationUnit); // check for existing ImportDeclaration for (ImportDeclaration importDeclaration : imports) { if (importDeclaration.getName().toString().equals(qualifiedName)) { return; } } // add new ImportDeclaration ImportDeclaration importDeclaration = ast.newImportDeclaration(); importDeclaration.setName(ast.newName(qualifiedName)); imports.add(importDeclaration); }
From source file:com.gowan.plugin.handlers.JUnit3Handler.java
License:Open Source License
/** * /* w w w .ja v a2 s.c o m*/ * @param unit * @throws JavaModelException */ private void createAST(ICompilationUnit unit) throws JavaModelException { CompilationUnit parse = parse(unit); JUnit3Visitor visitor = new JUnit3Visitor(); parse.accept(visitor); IDocument doc = new Document(unit.getSource()); AST ast = parse.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); JUnit3 junit = visitor.getJUnit3(); TypeDeclaration td = (TypeDeclaration) parse.types().get(0); ITrackedNodePosition tdLocation = rewrite.track(td); if (junit.getKlass() != null) { rewrite.replace(td.getSuperclassType(), null, null); } else { return; // Skip if the class does not extend junit.framework.TestCase } // imports ImportDeclaration afterImport = ast.newImportDeclaration(); afterImport.setName(ast.newName(AFTER)); ImportDeclaration beforeImport = ast.newImportDeclaration(); beforeImport.setName(ast.newName(BEFORE)); ImportDeclaration testImport = ast.newImportDeclaration(); testImport.setName(ast.newName(TEST)); ListRewrite lrw = rewrite.getListRewrite(parse, CompilationUnit.IMPORTS_PROPERTY); if (junit.getTestCaseImport() != null) { lrw.remove(junit.getTestCaseImport(), null); lrw.insertLast(afterImport, null); lrw.insertLast(beforeImport, null); lrw.insertLast(testImport, null); } if (junit.getSetUp() != null) { transformSetUp(ast, rewrite, junit); } if (junit.getTearDown() != null) { transformTearDown(ast, rewrite, junit); } if (junit.getTest() != null && !junit.getTest().isEmpty()) { transformTest(ast, rewrite, junit); } TextEdit edits = rewrite.rewriteAST(doc, null); unit.applyTextEdit(edits, null); }
From source file:com.halware.nakedide.eclipse.ext.annot.utils.AstUtils.java
License:Open Source License
public static ImportDeclaration createImportStatement(AST ast, String annotationFullyQualifiedName) { ImportDeclaration newImportDeclaration = ast.newImportDeclaration(); newImportDeclaration.setName(ast.newName(annotationFullyQualifiedName)); return newImportDeclaration; }
From source file:com.idega.eclipse.ejbwizards.BeanCreator.java
License:Open Source License
protected void writeImports(AST ast, CompilationUnit unit, Set imports) { Iterator iter = imports.iterator(); while (iter.hasNext()) { String importName = (String) iter.next(); if (importName != null) { ImportDeclaration importDeclaration = ast.newImportDeclaration(); importDeclaration.setName(ast.newName(importName)); importDeclaration.setOnDemand(false); unit.imports().add(importDeclaration); }/*from ww w. j a v a 2 s. co m*/ } }
From source file:com.motorola.studio.android.generatecode.JavaCodeModifier.java
License:Apache License
@SuppressWarnings("unchecked") private void createImport(String name, CompilationUnit compUnit, boolean onDemand) { AST ast = compUnit.getAST();/*w w w. ja v a 2 s .c o m*/ ImportDeclaration importDeclaration = ast.newImportDeclaration(); importDeclaration.setName(ast.newName(name)); importDeclaration.setOnDemand(onDemand); compUnit.imports().add(importDeclaration); }
From source file:com.motorola.studio.android.model.java.ActivityClass.java
License:Apache License
/** * Adds the onCreate method to the activity class */// w w w .j ava2 s . c om @SuppressWarnings("unchecked") private void addOnCreateMethod() { // Adds import for Bundle ImportDeclaration intentImport = ast.newImportDeclaration(); intentImport.setName(ast.newName(BUNDLE_CLASS)); compUnit.imports().add(intentImport); MethodDeclaration onCreateMethod = ast.newMethodDeclaration(); onCreateMethod.modifiers().add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD)); onCreateMethod.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); onCreateMethod.setName(ast.newSimpleName(ONCREATE_METHOD_NAME)); addMethodParameter(onCreateMethod, "savedInstanceState", ast.newSimpleType(ast.newSimpleName(getName(BUNDLE_CLASS)))); addEmptyBlock(onCreateMethod); addSuperInvocation(onCreateMethod); classDecl.bodyDeclarations().add(onCreateMethod); // Adds JavaDoc to the method addComment(onCreateMethod, CodeUtilsNLS.MODEL_ActivityClass_OnCreateMethodDescription); addMethodReference(onCreateMethod, ACTIVITY_SUPERCLASS, ONCREATE_METHOD_NAME, new Type[] { ast.newSimpleType(ast.newSimpleName(getName(BUNDLE_CLASS))) }); }