下面列出了org.eclipse.jdt.core.dom.ASTNode#VARIABLE_DECLARATION_STATEMENT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
switch (declNode.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.FIELD_DECLARATION:
return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
case ASTNode.SINGLE_VARIABLE_DECLARATION:
return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_DECLARATION:
return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_CONSTANT_DECLARATION:
return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
default:
throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
}
}
private static Type getType(ASTNode node) {
switch(node.getNodeType()){
case ASTNode.SINGLE_VARIABLE_DECLARATION:
return ((SingleVariableDeclaration) node).getType();
case ASTNode.FIELD_DECLARATION:
return ((FieldDeclaration) node).getType();
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
return ((VariableDeclarationStatement) node).getType();
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return ((VariableDeclarationExpression) node).getType();
case ASTNode.METHOD_DECLARATION:
return ((MethodDeclaration)node).getReturnType2();
case ASTNode.PARAMETERIZED_TYPE:
return ((ParameterizedType)node).getType();
default:
Assert.isTrue(false);
return null;
}
}
/**
* The selection corresponds to a ParameterizedType (return type of method)
* @param pt the type
* @return the message
*/
private String parameterizedTypeSelected(ParameterizedType pt) {
ASTNode parent= pt.getParent();
if (parent.getNodeType() == ASTNode.METHOD_DECLARATION){
fMethodBinding= ((MethodDeclaration)parent).resolveBinding();
fParamIndex= -1;
fEffectiveSelectionStart= pt.getStartPosition();
fEffectiveSelectionLength= pt.getLength();
setOriginalType(pt.resolveBinding());
} else if (parent.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION){
return singleVariableDeclarationSelected((SingleVariableDeclaration)parent);
} else if (parent.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT){
return variableDeclarationStatementSelected((VariableDeclarationStatement)parent);
} else if (parent.getNodeType() == ASTNode.FIELD_DECLARATION){
return fieldDeclarationSelected((FieldDeclaration)parent);
} else {
return nodeTypeNotSupported();
}
return null;
}
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
switch (declNode.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.FIELD_DECLARATION:
return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
case ASTNode.SINGLE_VARIABLE_DECLARATION:
return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_DECLARATION:
return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_CONSTANT_DECLARATION:
return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
default:
throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
}
}
private static boolean isSingleDeclaration(ASTNode node) {
int type= node.getNodeType();
if (type == ASTNode.SINGLE_VARIABLE_DECLARATION)
return true;
if (type == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
node= node.getParent();
if (node.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT) {
VariableDeclarationStatement vs= (VariableDeclarationStatement)node;
return vs.fragments().size() == 1;
}
}
return false;
}
private static boolean isMultiDeclarationFragment(ASTNode node) {
int nodeType= node.getNodeType();
if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
node= node.getParent();
if (node.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT) {
VariableDeclarationStatement vs= (VariableDeclarationStatement)node;
return vs.fragments().size() > 1;
}
}
return false;
}
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
if (fTypeKind == K_ANNOTATION) {
return null;
}
AST ast= node.getAST();
node= ASTNodes.getNormalizedNode(node);
ASTNode parent= node.getParent();
switch (parent.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
}
break;
case ASTNode.THROW_STATEMENT :
return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
case ASTNode.SINGLE_VARIABLE_DECLARATION:
if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
}
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
case ASTNode.FIELD_DECLARATION:
return null; // no guessing for LHS types, cannot be a supertype of a known type
case ASTNode.PARAMETERIZED_TYPE:
return null; // Inheritance doesn't help: A<X> z= new A<String>(); ->
}
ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
if (binding != null && !binding.isRecovered()) {
return binding;
}
return null;
}
/**
* Creates a placeholder node of the given type. <code>null</code> if the type is not supported
* @param nodeType Type of the node to create. Use the type constants in {@link NodeInfoStore}.
* @return Returns a place holder node.
*/
public final ASTNode newPlaceholderNode(int nodeType) {
try {
ASTNode node= this.ast.createInstance(nodeType);
switch (node.getNodeType()) {
case ASTNode.FIELD_DECLARATION:
((FieldDeclaration) node).fragments().add(this.ast.newVariableDeclarationFragment());
break;
case ASTNode.MODIFIER:
((Modifier) node).setKeyword(Modifier.ModifierKeyword.ABSTRACT_KEYWORD);
break;
case ASTNode.TRY_STATEMENT :
((TryStatement) node).setFinally(this.ast.newBlock()); // have to set at least a finally block to be legal code
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION :
((VariableDeclarationExpression) node).fragments().add(this.ast.newVariableDeclarationFragment());
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT :
((VariableDeclarationStatement) node).fragments().add(this.ast.newVariableDeclarationFragment());
break;
case ASTNode.PARAMETERIZED_TYPE :
((ParameterizedType) node).typeArguments().add(this.ast.newWildcardType());
break;
}
return node;
} catch (IllegalArgumentException e) {
return null;
}
}
private Statement convertStatement(org.eclipse.jdt.core.dom.Statement statement) {
switch (statement.getNodeType()) {
case ASTNode.ASSERT_STATEMENT:
return convert((org.eclipse.jdt.core.dom.AssertStatement) statement);
case ASTNode.BLOCK:
return convert((org.eclipse.jdt.core.dom.Block) statement);
case ASTNode.BREAK_STATEMENT:
return convert((org.eclipse.jdt.core.dom.BreakStatement) statement);
case ASTNode.CONSTRUCTOR_INVOCATION:
return convert((org.eclipse.jdt.core.dom.ConstructorInvocation) statement);
case ASTNode.CONTINUE_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ContinueStatement) statement);
case ASTNode.DO_STATEMENT:
return convert((org.eclipse.jdt.core.dom.DoStatement) statement);
case ASTNode.EMPTY_STATEMENT:
return new EmptyStatement(getSourcePosition(statement));
case ASTNode.EXPRESSION_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ExpressionStatement) statement);
case ASTNode.FOR_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ForStatement) statement);
case ASTNode.ENHANCED_FOR_STATEMENT:
return convert((org.eclipse.jdt.core.dom.EnhancedForStatement) statement);
case ASTNode.IF_STATEMENT:
return convert((org.eclipse.jdt.core.dom.IfStatement) statement);
case ASTNode.LABELED_STATEMENT:
return convert((org.eclipse.jdt.core.dom.LabeledStatement) statement);
case ASTNode.RETURN_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ReturnStatement) statement);
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return convert((org.eclipse.jdt.core.dom.SuperConstructorInvocation) statement);
case ASTNode.SWITCH_STATEMENT:
return convert((org.eclipse.jdt.core.dom.SwitchStatement) statement);
case ASTNode.SYNCHRONIZED_STATEMENT:
return convert((org.eclipse.jdt.core.dom.SynchronizedStatement) statement);
case ASTNode.THROW_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ThrowStatement) statement);
case ASTNode.TRY_STATEMENT:
return convert((org.eclipse.jdt.core.dom.TryStatement) statement);
case ASTNode.TYPE_DECLARATION_STATEMENT:
return convert((org.eclipse.jdt.core.dom.TypeDeclarationStatement) statement);
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
return convert((org.eclipse.jdt.core.dom.VariableDeclarationStatement) statement);
case ASTNode.WHILE_STATEMENT:
return convert((org.eclipse.jdt.core.dom.WhileStatement) statement);
default:
throw internalCompilerError(
"Unexpected type for Statement: %s", statement.getClass().getName());
}
}
/**
* Determines what kind of ASTNode has been selected.
* @param node the node
* @return A non-null String containing an error message
* is returned if the ChangeTypeRefactoring refactoring cannot be applied to the selected ASTNode.
* A return value of null indicates a valid selection.
*/
private String determineSelection(ASTNode node) {
if (node == null) {
return RefactoringCoreMessages.ChangeTypeRefactoring_invalidSelection;
} else {
if (DEBUG) System.out.println("node nodeType= " + node.getClass().getName()); //$NON-NLS-1$
if (DEBUG) System.out.println("parent nodeType= " + node.getParent().getClass().getName()); //$NON-NLS-1$
if (DEBUG) System.out.println("GrandParent nodeType= " + node.getParent().getParent().getClass().getName()); //$NON-NLS-1$
ASTNode parent= node.getParent();
ASTNode grandParent= parent.getParent();
if (grandParent == null)
return nodeTypeNotSupported();
// adjustment needed if part of a parameterized type is selected
if (grandParent.getNodeType() == ASTNode.PARAMETERIZED_TYPE){
node= grandParent;
}
// adjustment needed if part of a qualified name is selected
ASTNode current= null;
if (node.getNodeType() == ASTNode.QUALIFIED_NAME){
current= node;
while (current.getNodeType() == ASTNode.QUALIFIED_NAME){
current= current.getParent();
}
if (current.getNodeType() != ASTNode.SIMPLE_TYPE){
return nodeTypeNotSupported();
}
node= current.getParent();
} else if (parent.getNodeType() == ASTNode.QUALIFIED_NAME){
current= parent;
while (current.getNodeType() == ASTNode.QUALIFIED_NAME){
current= current.getParent();
}
if (current.getNodeType() != ASTNode.SIMPLE_TYPE){
return nodeTypeNotSupported();
}
node= current.getParent();
}
fObject= node.getAST().resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
switch (node.getNodeType()) {
case ASTNode.SIMPLE_NAME :
return simpleNameSelected((SimpleName)node);
case ASTNode.VARIABLE_DECLARATION_STATEMENT :
return variableDeclarationStatementSelected((VariableDeclarationStatement) node);
case ASTNode.FIELD_DECLARATION :
return fieldDeclarationSelected((FieldDeclaration) node);
case ASTNode.SINGLE_VARIABLE_DECLARATION :
return singleVariableDeclarationSelected((SingleVariableDeclaration) node);
case ASTNode.PARAMETERIZED_TYPE:
return parameterizedTypeSelected((ParameterizedType) node);
default :
return nodeTypeNotSupported();
}
}
}
/**
* Adds a SuppressWarnings proposal if possible and returns whether parent nodes should be processed or not (and with what relevance).
*
* @param cu the compilation unit
* @param node the node on which to add a SuppressWarning token
* @param warningToken the warning token to add
* @param relevance the proposal's relevance
* @param proposals collector to which the proposal should be added
* @return <code>0</code> if no further proposals should be added to parent nodes, or the relevance of the next proposal
*
* @since 3.6
*/
private static int addSuppressWarningsProposalIfPossible(ICompilationUnit cu, ASTNode node, String warningToken, int relevance, Collection<ICommandAccess> proposals) {
ChildListPropertyDescriptor property;
String name;
boolean isLocalVariable= false;
switch (node.getNodeType()) {
case ASTNode.SINGLE_VARIABLE_DECLARATION:
property= SingleVariableDeclaration.MODIFIERS2_PROPERTY;
name= ((SingleVariableDeclaration) node).getName().getIdentifier();
isLocalVariable= true;
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
property= VariableDeclarationStatement.MODIFIERS2_PROPERTY;
name= getFirstFragmentName(((VariableDeclarationStatement) node).fragments());
isLocalVariable= true;
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
property= VariableDeclarationExpression.MODIFIERS2_PROPERTY;
name= getFirstFragmentName(((VariableDeclarationExpression) node).fragments());
isLocalVariable= true;
break;
case ASTNode.TYPE_DECLARATION:
property= TypeDeclaration.MODIFIERS2_PROPERTY;
name= ((TypeDeclaration) node).getName().getIdentifier();
break;
case ASTNode.ANNOTATION_TYPE_DECLARATION:
property= AnnotationTypeDeclaration.MODIFIERS2_PROPERTY;
name= ((AnnotationTypeDeclaration) node).getName().getIdentifier();
break;
case ASTNode.ENUM_DECLARATION:
property= EnumDeclaration.MODIFIERS2_PROPERTY;
name= ((EnumDeclaration) node).getName().getIdentifier();
break;
case ASTNode.FIELD_DECLARATION:
property= FieldDeclaration.MODIFIERS2_PROPERTY;
name= getFirstFragmentName(((FieldDeclaration) node).fragments());
break;
// case ASTNode.INITIALIZER: not used, because Initializer cannot have annotations
case ASTNode.METHOD_DECLARATION:
property= MethodDeclaration.MODIFIERS2_PROPERTY;
name= ((MethodDeclaration) node).getName().getIdentifier() + "()"; //$NON-NLS-1$
break;
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
property= AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY;
name= ((AnnotationTypeMemberDeclaration) node).getName().getIdentifier() + "()"; //$NON-NLS-1$
break;
case ASTNode.ENUM_CONSTANT_DECLARATION:
property= EnumConstantDeclaration.MODIFIERS2_PROPERTY;
name= ((EnumConstantDeclaration) node).getName().getIdentifier();
break;
default:
return relevance;
}
String label= Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_suppress_warnings_label, new String[] { warningToken, BasicElementLabels.getJavaElementName(name) });
ASTRewriteCorrectionProposal proposal= new SuppressWarningsProposal(warningToken, label, cu, node, property, relevance);
proposals.add(proposal);
return isLocalVariable ? relevance - 1 : 0;
}