List of usage examples for org.eclipse.jdt.core.dom.rewrite ImportRewrite addImport
public String addImport(String qualifiedTypeName, ImportRewriteContext context)
From source file:com.google.gdt.eclipse.core.JavaASTUtils.java
License:Open Source License
/** * Generates the normalized form and adds the required imports for a given * {@link Type}.//from ww w . j av a 2 s . co m */ public static Type normalizeTypeAndAddImport(AST ast, Type type, ImportRewrite imports) { ITypeBinding binding = type.resolveBinding(); // Eliminate type variables in the generated type // TODO(): maybe leave the type variables, if we can verify that the type // parameters on the target type are exactly the same as those on the source // type (all names and type bounds are identical) if (JavaASTUtils.containsTypeVariable(type)) { binding = binding.getErasure(); } // Report the type binding to the import rewriter, which will record the // import and give us either a SimpleType or a QualifiedType to use. return imports.addImport(binding, ast); }
From source file:com.google.gwt.eclipse.core.clientbundle.ClientBundleResource.java
License:Open Source License
public MethodDeclaration createMethodDeclaration(IType clientBundle, ASTRewrite astRewrite, ImportRewrite importRewrite, boolean addComments) throws CoreException { AST ast = astRewrite.getAST();//from w w w . ja v a 2 s.c o m MethodDeclaration methodDecl = ast.newMethodDeclaration(); // Method is named after the resource it accesses methodDecl.setName(ast.newSimpleName(getMethodName())); // Method return type is a ResourcePrototype subtype ITypeBinding resourceTypeBinding = JavaASTUtils.resolveType(clientBundle.getJavaProject(), getReturnTypeName()); Type resourceType = importRewrite.addImport(resourceTypeBinding, ast); methodDecl.setReturnType2(resourceType); // Add @Source annotation if necessary String sourceAnnotationValue = getSourceAnnotationValue(clientBundle); if (sourceAnnotationValue != null) { // Build the annotation SingleMemberAnnotation sourceAnnotation = ast.newSingleMemberAnnotation(); sourceAnnotation.setTypeName(ast.newName("Source")); StringLiteral annotationValue = ast.newStringLiteral(); annotationValue.setLiteralValue(sourceAnnotationValue); sourceAnnotation.setValue(annotationValue); // Add the annotation to the method ChildListPropertyDescriptor modifiers = methodDecl.getModifiersProperty(); ListRewrite modifiersRewriter = astRewrite.getListRewrite(methodDecl, modifiers); modifiersRewriter.insertFirst(sourceAnnotation, null); } return methodDecl; }
From source file:org.eclim.plugin.jdt.command.include.ImportCommand.java
License:Open Source License
@Override public Object execute(CommandLine commandLine) throws Exception { String file = commandLine.getValue(Options.FILE_OPTION); String projectName = commandLine.getValue(Options.PROJECT_OPTION); String type = commandLine.getValue(Options.TYPE_OPTION); int offset = getOffset(commandLine); ICompilationUnit src = JavaUtils.getCompilationUnit(projectName, file); IProject project = src.getJavaProject().getProject(); TextEdit edits = null;//from w w w.j av a 2s. co m int oldLength = src.getBuffer().getLength(); if (type != null) { CompilationUnit astRoot = SharedASTProvider.getAST(src, SharedASTProvider.WAIT_YES, null); edits = new MultiTextEdit(); ImportRewrite importRewrite = StubUtility.createImportRewrite(astRoot, true); ImportRewriteContext context = new ContextSensitiveImportRewriteContext(astRoot, offset, importRewrite); String res = importRewrite.addImport(type, context); if (type.equals(res)) { return CodeGenerationMessages.AddImportsOperation_error_importclash; } TextEdit rewrite = importRewrite.rewriteImports(null); edits.addChild(rewrite); JavaModelUtil.applyEdit(src, edits, true, null); } else { ChooseImport query = new ChooseImport(project, type); try { AddImportsOperation op = new AddImportsOperation(src, offset, 1, query, true /* save */, true /* apply */); op.run(null); edits = op.getResultingEdit(); if (edits == null) { IStatus status = op.getStatus(); return status.getSeverity() != IStatus.OK ? status.getMessage() : null; } } catch (OperationCanceledException oce) { return query.choices; } } TextEdit groupingEdit = importGroupingEdit(src, edits.getOffset() + 1); if (groupingEdit != null) { JavaModelUtil.applyEdit(src, groupingEdit, true, null); } if (src.isWorkingCopy()) { src.commitWorkingCopy(false, null); } if (edits.getOffset() < offset) { offset += src.getBuffer().getLength() - oldLength; } return Position.fromOffset(ProjectUtils.getFilePath(projectName, file), null, offset, 0); }
From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java
License:Open Source License
private static CUCorrectionProposal createTypeRefChangeFullProposal(ICompilationUnit cu, ITypeBinding binding, ASTNode node, int relevance) throws CoreException { ASTRewrite rewrite = ASTRewrite.create(node.getAST()); String label = Messages.format( CorrectionMessages.UnresolvedElementsSubProcessor_change_full_type_description, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_DEFAULT)); Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance + 3, image);//from w w w. ja v a 2s. c om ImportRewrite imports = proposal.createImportRewrite((CompilationUnit) node.getRoot()); Type type = imports.addImport(binding, node.getAST()); rewrite.replace(node, type, null); return proposal; }
From source file:org.eclipse.gmf.internal.common.codegen.OrganizeImportsPostprocessor.java
License:Open Source License
private boolean addImport(String typeName, String fullName, ImportRewrite importRewrite, ImportRewrite.ImportRewriteContext context, Collection<String> importsAdded) { boolean resultIsOk = importRewrite.addImport(fullName, context).equals(typeName); if (resultIsOk && !importsAdded.contains(fullName)) { importsAdded.add(fullName);//from ww w . j av a 2 s . c om } return resultIsOk; }
From source file:org.eclipse.objectteams.otdt.internal.ui.text.correction.MappingProposalSubProcessor.java
License:Open Source License
static MethodSpec createMethodSpec(AST ast, ImportRewrite imports, IMethodBinding methodBinding, String[] argNames) {/*from w w w.j a v a2 s . c o m*/ List<SingleVariableDeclaration> args = new ArrayList<SingleVariableDeclaration>(); for (int i = 0; i < methodBinding.getParameterTypes().length; i++) { ITypeBinding paramType = methodBinding.getParameterTypes()[i]; args.add(ASTNodeCreator.createArgument(ast, 0/*modifiers*/, imports.addImport(paramType, ast), argNames[i], null)); } ITypeBinding providedReturnType = methodBinding.getReturnType(); Type returnType = imports.addImport(providedReturnType, ast); return ASTNodeCreator.createMethodSpec(ast, methodBinding.getName(), returnType, args, true); }
From source file:org.eclipse.objectteams.otdt.internal.ui.text.correction.MappingProposalSubProcessor.java
License:Open Source License
static FieldAccessSpec createFieldSpec(AST ast, ImportRewrite imports, String fieldName, ITypeBinding fieldType) {// w ww .j av a2 s . c o m return ASTNodeCreator.createFieldAccSpec(ast, fieldName, imports.addImport(fieldType, ast)); }
From source file:org.eclipse.objectteams.otdt.internal.ui.util.OTStubUtility.java
License:Open Source License
/** This method is inspired by createImplementationStub(..), but much leaner. */ public static CalloutMappingDeclaration createCallout(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, IMethodBinding binding, String type, CodeGenerationSettings settings) throws CoreException { AST ast = rewrite.getAST();/*w w w. j ava 2 s . c om*/ CalloutMappingDeclaration decl = ast.newCalloutMappingDeclaration(); decl.setSignatureFlag(true); // no modifiers MethodSpec spec = ast.newMethodSpec(); spec.setSignatureFlag(true); spec.setName(ast.newSimpleName(binding.getName())); // no type parameters spec.setReturnType2(imports.addImport(binding.getReturnType(), ast)); // this helps to reuse some code regarding methods: MethodDeclaration method = ast.newMethodDeclaration(); List parameters = getDefault().createParameters(unit.getJavaProject(), imports, null, ast, binding, method); spec.parameters().addAll(ASTNode.copySubtrees(ast, parameters)); // copy to re-parent decl.setRoleMappingElement(spec); decl.setBaseMappingElement((MethodSpec) ASTNode.copySubtree(ast, spec)); // no thrown exceptions String delimiter = StubUtility.getLineDelimiterUsed(unit); // no body if (settings.createComments) { // TODO(SH): this reuses the template for overridden methods, should actually define our own template. String string = CodeGeneration.getMethodComment(unit, type, method, binding, delimiter); if (string != null) { Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC); decl.setJavadoc(javadoc); } } // no override annotation return decl; }
From source file:org.eclipse.objectteams.otdt.internal.ui.util.OTStubUtility.java
License:Open Source License
/** This method is inspired by createImplementationStub(..), but much leaner. */ public static CallinMappingDeclaration createCallin(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, IMethodBinding binding, String type, ModifierKeyword callinModifier, CodeGenerationSettings settings) throws CoreException { AST ast = rewrite.getAST();/*from ww w. j a va2 s . c om*/ CallinMappingDeclaration decl = ast.newCallinMappingDeclaration(); decl.setBindingOperator(ast.newMethodBindingOperator(callinModifier, MethodBindingOperator.KIND_CALLIN)); // no regular modifiers MethodSpec spec = ast.newMethodSpec(); spec.setSignatureFlag(true); spec.setName(ast.newSimpleName(binding.getName())); // no type parameters spec.setReturnType2(imports.addImport(binding.getReturnType(), ast)); // this helps to reuse some code regarding methods: MethodDeclaration method = ast.newMethodDeclaration(); List parameters = getDefault().createParameters(unit.getJavaProject(), imports, null, ast, binding, method); spec.parameters().addAll(ASTNode.copySubtrees(ast, parameters)); // copy to re-parent decl.setRoleMappingElement(spec); decl.getBaseMappingElements().add(ASTNode.copySubtree(ast, spec)); // no thrown exceptions String delimiter = StubUtility.getLineDelimiterUsed(unit); // no body if (settings.createComments) { // TODO(SH): this reuses the template for overridden methods, should actually define our own template. String string = CodeGeneration.getMethodComment(unit, type, method, binding, delimiter); if (string != null) { Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC); decl.setJavadoc(javadoc); } } // no override annotation return decl; }
From source file:org.eclipse.objectteams.otdt.internal.ui.util.OTStubUtility.java
License:Open Source License
/** Stripped down version of createCallout for use after '<-' : */ public static MethodSpec createMethodSpec(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, IMethodBinding binding, boolean hasSignature) throws CoreException { AST ast = rewrite.getAST();//w ww . j ava 2 s . c o m MethodSpec spec = ast.newMethodSpec(); spec.setSignatureFlag(hasSignature); spec.setName(ast.newSimpleName(binding.getName())); // no type parameters spec.setReturnType2(imports.addImport(binding.getReturnType(), ast)); // this helps to reuse some code regarding methods: MethodDeclaration method = ast.newMethodDeclaration(); List parameters = getDefault().createParameters(unit.getJavaProject(), imports, null, ast, binding, method); spec.parameters().addAll(ASTNode.copySubtrees(ast, parameters)); // copy to re-parent return spec; }