下面列出了org.eclipse.jdt.core.dom.ASTNode#PARAMETERIZED_TYPE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
public boolean visit(AnonymousClassDeclaration node) {
ASTNode name;
ASTNode parent = node.getParent();
switch (parent.getNodeType()) {
case ASTNode.CLASS_INSTANCE_CREATION:
name = ((ClassInstanceCreation) parent).getType();
if (name.getNodeType() == ASTNode.PARAMETERIZED_TYPE) {
name = ((ParameterizedType) name).getType();
}
break;
case ASTNode.ENUM_CONSTANT_DECLARATION:
name = ((EnumConstantDeclaration) parent).getName();
break;
default:
return true;
}
if (found(node, name) && this.resolveBinding)
this.foundBinding = node.resolveBinding();
return true;
}
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;
}
}
/**
* 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();
}
}
}
private static void getFullyQualifiedName(Type type, StringBuffer buffer) {
switch (type.getNodeType()) {
case ASTNode.ARRAY_TYPE:
ArrayType arrayType = (ArrayType) type;
getFullyQualifiedName(arrayType.getElementType(), buffer);
for (int i = 0, length = arrayType.getDimensions(); i < length; i++) {
buffer.append('[');
buffer.append(']');
}
break;
case ASTNode.PARAMETERIZED_TYPE:
ParameterizedType parameterizedType = (ParameterizedType) type;
getFullyQualifiedName(parameterizedType.getType(), buffer);
buffer.append('<');
Iterator iterator = parameterizedType.typeArguments().iterator();
boolean isFirst = true;
while (iterator.hasNext()) {
if (!isFirst)
buffer.append(',');
else
isFirst = false;
Type typeArgument = (Type) iterator.next();
getFullyQualifiedName(typeArgument, buffer);
}
buffer.append('>');
break;
case ASTNode.PRIMITIVE_TYPE:
buffer.append(((PrimitiveType) type).getPrimitiveTypeCode().toString());
break;
case ASTNode.QUALIFIED_TYPE:
buffer.append(((QualifiedType) type).getName().getFullyQualifiedName());
break;
case ASTNode.SIMPLE_TYPE:
buffer.append(((SimpleType) type).getName().getFullyQualifiedName());
break;
case ASTNode.WILDCARD_TYPE:
buffer.append('?');
WildcardType wildcardType = (WildcardType) type;
Type bound = wildcardType.getBound();
if (bound == null) return;
if (wildcardType.isUpperBound()) {
buffer.append(" extends "); //$NON-NLS-1$
} else {
buffer.append(" super "); //$NON-NLS-1$
}
getFullyQualifiedName(bound, buffer);
break;
}
}