本文整理汇总了Java中org.eclipse.jdt.internal.corext.dom.ASTNodes类的典型用法代码示例。如果您正苦于以下问题:Java ASTNodes类的具体用法?Java ASTNodes怎么用?Java ASTNodes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ASTNodes类属于org.eclipse.jdt.internal.corext.dom包,在下文中一共展示了ASTNodes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createDeclaration
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private VariableDeclarationStatement createDeclaration(
IVariableBinding binding, Expression intilizer) {
VariableDeclaration original =
ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
fragment.setInitializer(intilizer);
VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
result.setType(
ASTNodeFactory.newType(
fAST,
original,
fImportRewriter,
new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
return result;
}
示例2: getVariableNameSuggestions
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private static String[] getVariableNameSuggestions(int variableKind, IJavaProject project, Type expectedType, Collection<String> excluded, boolean evaluateDefault) {
int dim= 0;
if (expectedType.isArrayType()) {
ArrayType arrayType= (ArrayType)expectedType;
dim= arrayType.getDimensions();
expectedType= arrayType.getElementType();
}
if (expectedType.isParameterizedType()) {
expectedType= ((ParameterizedType)expectedType).getType();
}
String typeName= ASTNodes.getTypeName(expectedType);
if (typeName.length() > 0) {
return getVariableNameSuggestions(variableKind, project, typeName, dim, excluded, evaluateDefault);
}
return EMPTY;
}
示例3: isLeftHandSideOfAssignment
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
static boolean isLeftHandSideOfAssignment(ASTNode node) {
Assignment assignment = (Assignment) ASTNodes.getParent(node, ASTNode.ASSIGNMENT);
if (assignment != null) {
Expression leftHandSide = assignment.getLeftHandSide();
if (leftHandSide == node) {
return true;
}
if (ASTNodes.isParent(node, leftHandSide)) {
switch (leftHandSide.getNodeType()) {
case ASTNode.SIMPLE_NAME:
return true;
case ASTNode.FIELD_ACCESS:
return node == ((FieldAccess) leftHandSide).getName();
case ASTNode.QUALIFIED_NAME:
return node == ((QualifiedName) leftHandSide).getName();
case ASTNode.SUPER_FIELD_ACCESS:
return node == ((SuperFieldAccess) leftHandSide).getName();
default:
return false;
}
}
}
return false;
}
示例4: doAddField
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private ASTRewrite doAddField(CompilationUnit astRoot) {
SimpleName node = fOriginalNode;
boolean isInDifferentCU = false;
ASTNode newTypeDecl = astRoot.findDeclaringNode(fSenderBinding);
if (newTypeDecl == null) {
astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
isInDifferentCU = true;
}
ImportRewrite imports = createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext =
new ContextSensitiveImportRewriteContext(
ASTResolving.findParentBodyDeclaration(node), imports);
if (newTypeDecl != null) {
AST ast = newTypeDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(node.getIdentifier()));
Type type = evaluateVariableType(ast, imports, importRewriteContext, fSenderBinding);
FieldDeclaration newDecl = ast.newFieldDeclaration(fragment);
newDecl.setType(type);
newDecl
.modifiers()
.addAll(ASTNodeFactory.newModifiers(ast, evaluateFieldModifiers(newTypeDecl)));
if (fSenderBinding.isInterface() || fVariableKind == CONST_FIELD) {
fragment.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0));
}
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> decls =
ASTNodes.<BodyDeclaration>getChildListProperty(newTypeDecl, property);
int maxOffset = isInDifferentCU ? -1 : node.getStartPosition();
int insertIndex = findFieldInsertIndex(decls, newDecl, maxOffset);
ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
listRewriter.insertAt(newDecl, insertIndex, null);
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
getLinkedProposalModel(), rewrite, newDecl.modifiers(), fSenderBinding.isInterface());
addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
if (!isInDifferentCU) {
addLinkedPosition(rewrite.track(node), true, KEY_NAME);
}
addLinkedPosition(rewrite.track(fragment.getName()), false, KEY_NAME);
if (fragment.getInitializer() != null) {
addLinkedPosition(rewrite.track(fragment.getInitializer()), false, KEY_INITIALIZER);
}
return rewrite;
}
return null;
}
示例5: canAddFinal
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private static boolean canAddFinal(IBinding binding, ASTNode declNode) {
if (!(binding instanceof IVariableBinding)) return false;
IVariableBinding varbinding = (IVariableBinding) binding;
int modifiers = varbinding.getModifiers();
if (Modifier.isFinal(modifiers)
|| Modifier.isVolatile(modifiers)
|| Modifier.isTransient(modifiers)) return false;
ASTNode parent = ASTNodes.getParent(declNode, VariableDeclarationExpression.class);
if (parent != null && ((VariableDeclarationExpression) parent).fragments().size() > 1)
return false;
if (varbinding.isField() && !Modifier.isPrivate(modifiers)) return false;
if (varbinding.isParameter()) {
ASTNode varDecl = declNode.getParent();
if (varDecl instanceof MethodDeclaration) {
MethodDeclaration declaration = (MethodDeclaration) varDecl;
if (declaration.getBody() == null) return false;
}
}
return true;
}
示例6: doAddLocal
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private ASTRewrite doAddLocal() {
Expression expression = ((ExpressionStatement) fNodeToAssign).getExpression();
AST ast = fNodeToAssign.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
String[] varNames = suggestLocalVariableNames(fTypeBinding, expression);
for (int i = 0; i < varNames.length; i++) {
addLinkedPositionProposal(KEY_NAME, varNames[i], null);
}
VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
newDeclFrag.setName(ast.newSimpleName(varNames[0]));
newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression));
Type type = evaluateType(ast);
if (ASTNodes.isControlStatementBody(fNodeToAssign.getLocationInParent())) {
Block block = ast.newBlock();
block.statements().add(rewrite.createMoveTarget(fNodeToAssign));
rewrite.replace(fNodeToAssign, block, null);
}
if (needsSemicolon(expression)) {
VariableDeclarationStatement varStatement = ast.newVariableDeclarationStatement(newDeclFrag);
varStatement.setType(type);
rewrite.replace(expression, varStatement, null);
} else {
// trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement
VariableDeclarationExpression varExpression =
ast.newVariableDeclarationExpression(newDeclFrag);
varExpression.setType(type);
rewrite.replace(expression, varExpression, null);
}
addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME);
addLinkedPosition(rewrite.track(type), false, KEY_TYPE);
setEndPosition(rewrite.track(fNodeToAssign)); // set cursor after expression statement
return rewrite;
}
示例7: getArgument
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private static String getArgument(TagElement curr) {
List<? extends ASTNode> fragments = curr.fragments();
if (!fragments.isEmpty()) {
Object first = fragments.get(0);
if (first instanceof Name) {
return ASTNodes.getSimpleNameIdentifier((Name) first);
} else if (first instanceof TextElement && TagElement.TAG_PARAM.equals(curr.getTagName())) {
String text = ((TextElement) first).getText();
if ("<".equals(text) && fragments.size() >= 3) { // $NON-NLS-1$
Object second = fragments.get(1);
Object third = fragments.get(2);
if (second instanceof Name
&& third instanceof TextElement
&& ">".equals(((TextElement) third).getText())) { // $NON-NLS-1$
return '<' + ASTNodes.getSimpleNameIdentifier((Name) second) + '>';
}
} else if (text.startsWith(String.valueOf('<'))
&& text.endsWith(String.valueOf('>'))
&& text.length() > 2) {
return text.substring(1, text.length() - 1);
}
}
}
return null;
}
示例8: getFullTypeName
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
protected String getFullTypeName() {
ASTNode node = getNode();
while (true) {
node = node.getParent();
if (node instanceof AbstractTypeDeclaration) {
String typeName = ((AbstractTypeDeclaration) node).getName().getIdentifier();
if (getNode() instanceof LambdaExpression) {
return Messages.format(
RefactoringCoreMessages.ChangeSignatureRefactoring_lambda_expression, typeName);
}
return typeName;
} else if (node instanceof ClassInstanceCreation) {
ClassInstanceCreation cic = (ClassInstanceCreation) node;
return Messages.format(
RefactoringCoreMessages.ChangeSignatureRefactoring_anonymous_subclass,
BasicElementLabels.getJavaElementName(ASTNodes.asString(cic.getType())));
} else if (node instanceof EnumConstantDeclaration) {
EnumDeclaration ed = (EnumDeclaration) node.getParent();
return Messages.format(
RefactoringCoreMessages.ChangeSignatureRefactoring_anonymous_subclass,
BasicElementLabels.getJavaElementName(ASTNodes.asString(ed.getName())));
}
}
}
示例9: endVisit
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
@Override
public void endVisit(ReturnStatement node) {
Expression expression = node.getExpression();
if (expression == null) return;
ConstraintVariable2 expressionCv = getConstraintVariable(expression);
if (expressionCv == null) return;
MethodDeclaration methodDeclaration =
(MethodDeclaration) ASTNodes.getParent(node, ASTNode.METHOD_DECLARATION);
if (methodDeclaration == null) return;
IMethodBinding methodBinding = methodDeclaration.resolveBinding();
if (methodBinding == null) return;
ReturnTypeVariable2 returnTypeCv = fTCModel.makeReturnTypeVariable(methodBinding);
if (returnTypeCv == null) return;
fTCModel.createElementEqualsConstraints(returnTypeCv, expressionCv);
}
示例10: addExtractCheckedLocalProposal
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
/**
* Fix for {@link IProblem#NullableFieldReference}
*
* @param context context
* @param problem problem to be fixed
* @param proposals accumulator for computed proposals
*/
public static void addExtractCheckedLocalProposal(
IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit compilationUnit = context.getASTRoot();
ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
SimpleName name = findProblemFieldName(selectedNode, problem.getProblemId());
if (name == null) return;
ASTNode method = ASTNodes.getParent(selectedNode, MethodDeclaration.class);
if (method == null) method = ASTNodes.getParent(selectedNode, Initializer.class);
if (method == null) return;
proposals.add(new ExtractToNullCheckedLocalProposal(cu, compilationUnit, name, method));
}
示例11: isIntroduceFactoryAvailable
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
public static boolean isIntroduceFactoryAvailable(final JavaTextSelection selection)
throws JavaModelException {
final IJavaElement[] elements = selection.resolveElementAtOffset();
if (elements.length == 1 && elements[0] instanceof IMethod)
return isIntroduceFactoryAvailable((IMethod) elements[0]);
// there's no IMethod for the default constructor
if (!Checks.isAvailable(selection.resolveEnclosingElement())) return false;
ASTNode node = selection.resolveCoveringNode();
if (node == null) {
ASTNode[] selectedNodes = selection.resolveSelectedNodes();
if (selectedNodes != null && selectedNodes.length == 1) {
node = selectedNodes[0];
if (node == null) return false;
} else {
return false;
}
}
if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) return true;
node = ASTNodes.getNormalizedNode(node);
if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) return true;
return false;
}
示例12: addFieldDeclaration
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private VariableDeclarationFragment addFieldDeclaration(
ASTRewrite rewrite, ASTNode newTypeDecl, int modifiers, Expression expression) {
if (fExistingFragment != null) {
return fExistingFragment;
}
ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> decls = ASTNodes.getBodyDeclarations(newTypeDecl);
AST ast = newTypeDecl.getAST();
String[] varNames = suggestFieldNames(fTypeBinding, expression, modifiers);
for (int i = 0; i < varNames.length; i++) {
addLinkedPositionProposal(KEY_NAME, varNames[i], null);
}
String varName = varNames[0];
VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
newDeclFrag.setName(ast.newSimpleName(varName));
FieldDeclaration newDecl = ast.newFieldDeclaration(newDeclFrag);
Type type = evaluateType(ast);
newDecl.setType(type);
newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers));
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(
getLinkedProposalModel(), rewrite, newDecl.modifiers(), false);
int insertIndex = findFieldInsertIndex(decls, fNodeToAssign.getStartPosition());
rewrite.getListRewrite(newTypeDecl, property).insertAt(newDecl, insertIndex, null);
return newDeclFrag;
}
示例13: visit
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
@Override
public boolean visit(SimpleName node) {
addReferencesToName(node);
IBinding binding = node.resolveBinding();
if (binding instanceof ITypeBinding) {
ITypeBinding type = (ITypeBinding) binding;
if (type.isTypeVariable()) {
addTypeVariableReference(type, node);
}
} else if (binding instanceof IVariableBinding) {
IVariableBinding vb = (IVariableBinding) binding;
if (vb.isField() && !isStaticallyImported(node)) {
Name topName = ASTNodes.getTopMostName(node);
if (node == topName || node == ASTNodes.getLeftMostSimpleName(topName)) {
StructuralPropertyDescriptor location = node.getLocationInParent();
if (location != SingleVariableDeclaration.NAME_PROPERTY
&& location != VariableDeclarationFragment.NAME_PROPERTY) {
fImplicitReceivers.add(node);
}
}
} else if (!vb.isField()) {
// we have a local. Check if it is a parameter.
ParameterData data = fParameters.get(binding);
if (data != null) {
ASTNode parent = node.getParent();
if (parent instanceof Expression) {
int precedence = OperatorPrecedence.getExpressionPrecedence((Expression) parent);
if (precedence != Integer.MAX_VALUE) {
data.setOperatorPrecedence(precedence);
}
}
}
}
}
return true;
}
示例14: match
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
@Override
public boolean match(SimpleName candidate, Object s) {
if (!(s instanceof SimpleName)) return false;
SimpleName snippet = (SimpleName) s;
if (candidate.isDeclaration() != snippet.isDeclaration()) return false;
IBinding cb = candidate.resolveBinding();
IBinding sb = snippet.resolveBinding();
if (cb == null || sb == null) return false;
IVariableBinding vcb = ASTNodes.getVariableBinding(candidate);
IVariableBinding vsb = ASTNodes.getVariableBinding(snippet);
if (vcb == null || vsb == null) return Bindings.equals(cb, sb);
if (!vcb.isField() && !vsb.isField() && Bindings.equals(vcb.getType(), vsb.getType())) {
SimpleName mapped = fMatch.getMappedName(vsb);
if (mapped != null) {
IVariableBinding mappedBinding = ASTNodes.getVariableBinding(mapped);
if (!Bindings.equals(vcb, mappedBinding)) return false;
}
fMatch.addLocal(vsb, candidate);
return true;
}
return Bindings.equals(cb, sb);
}
示例15: isLeftHandSideOfAssignment
import org.eclipse.jdt.internal.corext.dom.ASTNodes; //导入依赖的package包/类
private static boolean isLeftHandSideOfAssignment(ASTNode node) {
Assignment assignment = (Assignment) ASTNodes.getParent(node, ASTNode.ASSIGNMENT);
if (assignment != null) {
Expression leftHandSide = assignment.getLeftHandSide();
if (leftHandSide == node) {
return true;
}
if (ASTNodes.isParent(node, leftHandSide)) {
switch (leftHandSide.getNodeType()) {
case ASTNode.SIMPLE_NAME:
return true;
case ASTNode.FIELD_ACCESS:
return node == ((FieldAccess) leftHandSide).getName();
case ASTNode.QUALIFIED_NAME:
return node == ((QualifiedName) leftHandSide).getName();
case ASTNode.SUPER_FIELD_ACCESS:
return node == ((SuperFieldAccess) leftHandSide).getName();
default:
return false;
}
}
}
return false;
}