本文整理汇总了Java中org.eclipse.jdt.core.dom.rewrite.ImportRewrite.addImport方法的典型用法代码示例。如果您正苦于以下问题:Java ImportRewrite.addImport方法的具体用法?Java ImportRewrite.addImport怎么用?Java ImportRewrite.addImport使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.dom.rewrite.ImportRewrite
的用法示例。
在下文中一共展示了ImportRewrite.addImport方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNewCastTypeNode
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) {
AST ast= rewrite.getAST();
ImportRewriteContext context= new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite);
if (fCastType != null) {
return importRewrite.addImport(fCastType, ast,context, TypeLocation.CAST);
}
ASTNode node= fNodeToCast;
ASTNode parent= node.getParent();
if (parent instanceof CastExpression) {
node= parent;
parent= parent.getParent();
}
while (parent instanceof ParenthesizedExpression) {
node= parent;
parent= parent.getParent();
}
if (parent instanceof MethodInvocation) {
MethodInvocation invocation= (MethodInvocation) node.getParent();
if (invocation.getExpression() == node) {
IBinding targetContext= ASTResolving.getParentMethodOrTypeBinding(node);
ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext);
if (bindings.length > 0) {
ITypeBinding first= getCastFavorite(bindings, fNodeToCast.resolveTypeBinding());
Type newTypeNode= importRewrite.addImport(first, ast, context, TypeLocation.CAST);
return newTypeNode;
}
}
}
Type newCastType= ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
return newCastType;
}
示例2: newType
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
private static Type newType(LambdaExpression lambdaExpression, VariableDeclarationFragment declaration, AST ast, ImportRewrite importRewrite, ImportRewriteContext context) {
IMethodBinding method= lambdaExpression.resolveMethodBinding();
if (method != null) {
ITypeBinding[] parameterTypes= method.getParameterTypes();
int index= lambdaExpression.parameters().indexOf(declaration);
ITypeBinding typeBinding= parameterTypes[index];
if (importRewrite != null) {
return importRewrite.addImport(typeBinding, ast, context);
} else {
String qualifiedName= typeBinding.getQualifiedName();
if (qualifiedName.length() > 0) {
return newType(ast, qualifiedName);
}
}
}
// fall-back
return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
示例3: newReturnType
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
/**
* Returns the new type node representing the return type of <code>lambdaExpression</code>
* including the extra dimensions.
*
* @param lambdaExpression the lambda expression
* @param ast the AST to create the return type with
* @param importRewrite the import rewrite to use, or <code>null</code>
* @param context the import rewrite context, or <code>null</code>
* @return a new type node created with the given AST representing the return type of
* <code>lambdaExpression</code>
*
* @since 3.10
*/
public static Type newReturnType(LambdaExpression lambdaExpression, AST ast, ImportRewrite importRewrite, ImportRewriteContext context) {
IMethodBinding method= lambdaExpression.resolveMethodBinding();
if (method != null) {
ITypeBinding returnTypeBinding= method.getReturnType();
if (importRewrite != null) {
return importRewrite.addImport(returnTypeBinding, ast);
} else {
String qualifiedName= returnTypeBinding.getQualifiedName();
if (qualifiedName.length() > 0) {
return newType(ast, qualifiedName);
}
}
}
// fall-back
return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
示例4: newCreationType
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
/**
* Create a Type suitable as the creationType in a ClassInstanceCreation expression.
* @param ast The AST to create the nodes for.
* @param typeBinding binding representing the given class type
* @param importRewrite the import rewrite to use
* @param importContext the import context used to determine which (null) annotations to consider
* @return a Type suitable as the creationType in a ClassInstanceCreation expression.
*/
public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
if (typeBinding.isParameterizedType()) {
Type baseType= newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
IAnnotationBinding[] typeAnnotations= importContext.removeRedundantTypeAnnotations(typeBinding.getTypeAnnotations(), TypeLocation.NEW, typeBinding);
for (IAnnotationBinding typeAnnotation : typeAnnotations) {
((AnnotatableType)baseType).annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
}
ParameterizedType parameterizedType= ast.newParameterizedType(baseType);
for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
typeArgument= StubUtility2.replaceWildcardsAndCaptures(typeArgument);
parameterizedType.typeArguments().add(importRewrite.addImport(typeArgument, ast, importContext, TypeLocation.TYPE_ARGUMENT));
}
return parameterizedType;
} else {
return importRewrite.addImport(typeBinding, ast, importContext, TypeLocation.NEW);
}
}
示例5: getRewrite
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode= fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode= null;
CompilationUnit newRoot= fAstRoot;
if (boundNode != null) {
declNode= boundNode; // is same CU
} else {
newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode= newRoot.findDeclaringNode(fBinding.getKey());
}
ImportRewrite imports= createImportRewrite(newRoot);
if (declNode instanceof TypeDeclaration) {
AST ast= declNode.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(declNode, imports);
Type newInterface= imports.addImport(fNewInterface, ast, importRewriteContext, TypeLocation.OTHER);
ListRewrite listRewrite= rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
listRewrite.insertLast(newInterface, null);
return rewrite;
}
return null;
}
示例6: evaluateVariableType
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
private Type evaluateVariableType(AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext, TypeLocation location) {
if (fOriginalNode.getParent() instanceof MethodInvocation) {
MethodInvocation parent= (MethodInvocation) fOriginalNode.getParent();
if (parent.getExpression() == fOriginalNode) {
// _x_.foo() -> guess qualifier type by looking for a type with method 'foo'
ITypeBinding[] bindings= ASTResolving.getQualifierGuess(fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext);
if (bindings.length > 0) {
return imports.addImport(bindings[0], ast, importRewriteContext, location);
}
}
}
ITypeBinding binding= ASTResolving.guessBindingForReference(fOriginalNode);
if (binding != null) {
if (binding.isWildcardType()) {
binding= ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast);
if (binding == null) {
// only null binding applies
binding= ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
}
}
return imports.addImport(binding, ast, importRewriteContext, location);
}
// no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is
Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode);
if (type != null) {
return type;
}
if (fVariableKind == CONST_FIELD) {
return ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$
}
return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
示例7: getAmbiguousTypeReferenceProposals
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
public static void getAmbiguousTypeReferenceProposals(IInvocationContext context, IProblemLocation problem,
Collection<CUCorrectionProposal> proposals) throws CoreException {
final ICompilationUnit cu= context.getCompilationUnit();
int offset= problem.getOffset();
int len= problem.getLength();
IJavaElement[] elements= cu.codeSelect(offset, len);
for (int i= 0; i < elements.length; i++) {
IJavaElement curr= elements[i];
if (curr instanceof IType) {
String qualifiedTypeName= ((IType) curr).getFullyQualifiedName('.');
CompilationUnit root= context.getASTRoot();
String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importexplicit_description, BasicElementLabels.getJavaElementName(qualifiedTypeName));
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, ASTRewrite.create(root.getAST()), IProposalRelevance.IMPORT_EXPLICIT);
ImportRewrite imports= proposal.createImportRewrite(root);
imports.addImport(qualifiedTypeName);
proposals.add(proposal);
}
}
}
示例8: rewriteReference
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
private RefactoringStatus rewriteReference(ASTRewrite astRewrite,
ImportRewrite importRewrite, SimpleName name,
String fullyQualifiedTypeName) {
final RefactoringStatus status = new RefactoringStatus();
// get the node to replace.
final Name nodeToReplace = Util.getTopmostName(name);
// If its in a case statement.
if (Util.isContainedInCaseLabel(name)) {
if (!nodeToReplace.isSimpleName()) // Need to remove prefix.
astRewrite.replace(nodeToReplace, name, null);
return status;
}
// Make a copy of the simple name.
final AST ast = name.getAST();
final SimpleName nameCopy = (SimpleName) ASTNode.copySubtree(ast, name);
final String typeName = importRewrite.addImport(fullyQualifiedTypeName);
final QualifiedName newNameNode = ast.newQualifiedName(
ast.newName(typeName), nameCopy);
astRewrite.replace(nodeToReplace, newNameNode, null);
return status;
}
开发者ID:ponder-lab,项目名称:Constants-to-Enum-Eclipse-Plugin,代码行数:27,代码来源:ConvertConstantsToEnumRefactoring.java
示例9: newReturnType
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
/**
* Returns the new type node representing the return type of <code>lambdaExpression</code>
* including the extra dimensions.
*
* @param lambdaExpression the lambda expression
* @param ast the AST to create the return type with
* @param importRewrite the import rewrite to use, or <code>null</code>
* @param context the import rewrite context, or <code>null</code>
* @return a new type node created with the given AST representing the return type of <code>
* lambdaExpression</code>
* @since 3.10
*/
public static Type newReturnType(
LambdaExpression lambdaExpression,
AST ast,
ImportRewrite importRewrite,
ImportRewriteContext context) {
IMethodBinding method = lambdaExpression.resolveMethodBinding();
if (method != null) {
ITypeBinding returnTypeBinding = method.getReturnType();
if (importRewrite != null) {
return importRewrite.addImport(returnTypeBinding, ast);
} else {
String qualifiedName = returnTypeBinding.getQualifiedName();
if (qualifiedName.length() > 0) {
return newType(ast, qualifiedName);
}
}
}
// fall-back
return ast.newSimpleType(ast.newSimpleName("Object")); // $NON-NLS-1$
}
示例10: addImportToSourcePackageTypes
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
private void addImportToSourcePackageTypes(ICompilationUnit movedUnit, IProgressMonitor pm)
throws CoreException {
List<ICompilationUnit> cuList = Arrays.asList(fCus);
IType[] allCuTypes = movedUnit.getAllTypes();
IType[] referencedTypes = ReferenceFinderUtil.getTypesReferencedIn(allCuTypes, pm);
ImportRewrite importEdit = getImportRewrite(movedUnit);
importEdit.setFilterImplicitImports(false);
IPackageFragment srcPack = (IPackageFragment) movedUnit.getParent();
for (int i = 0; i < referencedTypes.length; i++) {
IType iType = referencedTypes[i];
if (!iType.exists()) continue;
if (!JavaModelUtil.isSamePackage(iType.getPackageFragment(), srcPack)) continue;
if (cuList.contains(iType.getCompilationUnit())) continue;
importEdit.addImport(iType.getFullyQualifiedName('.'));
}
}
示例11: rewriteAST
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model)
throws CoreException {
AST ast = cuRewrite.getRoot().getAST();
ListRewrite listRewrite =
cuRewrite
.getASTRewrite()
.getListRewrite(fBodyDeclaration, fBodyDeclaration.getModifiersProperty());
TextEditGroup group = createTextEditGroup(fMessage, cuRewrite);
if (!checkExisting(fBodyDeclaration.modifiers(), listRewrite, group)) return;
if (hasNonNullDefault(fBodyDeclaration.resolveBinding()))
return; // should be safe, as in this case checkExisting() should've already produced a
// change (remove existing
// annotation).
Annotation newAnnotation = ast.newMarkerAnnotation();
ImportRewrite importRewrite = cuRewrite.getImportRewrite();
String resolvableName = importRewrite.addImport(fAnnotationToAdd);
newAnnotation.setTypeName(ast.newName(resolvableName));
listRewrite.insertLast(
newAnnotation,
group); // null annotation is last modifier, directly preceding the return type
}
示例12: normalizeTypeAndAddImport
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
/**
* Generates the normalized form and adds the required imports for a given
* {@link Type}.
*/
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);
}
示例13: updateTypeReferences
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
private void updateTypeReferences(ASTRewrite rewriter, CallContext context) {
ImportRewrite importer = context.importer;
for (Iterator<SimpleName> iter = fAnalyzer.getTypesToImport().iterator(); iter.hasNext(); ) {
Name element = iter.next();
ITypeBinding binding = ASTNodes.getTypeBinding(element);
if (binding != null && !binding.isLocal()) {
// We have collected names not types. So we have to import
// the declaration type if we reference a parameterized type
// since we have an entry for every name node (e.g. one for
// Vector and one for Integer in Vector<Integer>.
if (binding.isParameterizedType()) {
binding = binding.getTypeDeclaration();
}
String s = importer.addImport(binding);
if (!ASTNodes.asString(element).equals(s)) {
rewriter.replace(element, rewriter.createStringPlaceholder(s, ASTNode.SIMPLE_NAME), null);
}
}
}
}
示例14: getImportRewrite
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
private ImportRewrite getImportRewrite(CompilationUnit astRoot,
String typeToImport) {
if (typeToImport != null) {
final ImportRewrite importRewrite = CodeStyleConfiguration
.createImportRewrite(astRoot, true);
importRewrite.addImport(typeToImport);
return importRewrite;
}
return null;
}
示例15: computeEdits
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; //导入方法依赖的package包/类
@Override
public TextEdit computeEdits(int offset, LinkedPosition position, char trigger, int stateMask, LinkedModeModel model) throws CoreException {
ImportRewrite impRewrite= StubUtility.createImportRewrite(fCompilationUnit, true);
String replaceString= impRewrite.addImport(fTypeProposal);
MultiTextEdit composedEdit= new MultiTextEdit();
composedEdit.addChild(new ReplaceEdit(position.getOffset(), position.getLength(), replaceString));
composedEdit.addChild(impRewrite.rewriteImports(null));
return composedEdit;
}