下面列出了org.eclipse.jdt.core.dom.VariableDeclarationStatement#getType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean visit(VariableDeclarationStatement node) {
ASTNode parent = node.getParent();
while(parent != null){
if(parent instanceof Block){
break;
}
parent = parent.getParent();
}
if(parent != null) {
int start = _unit.getLineNumber(node.getStartPosition());
int end = _unit.getLineNumber(parent.getStartPosition() + parent.getLength());
for (Object o : node.fragments()) {
VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
Pair<String, Type> pair = new Pair<String, Type>(vdf.getName().getFullyQualifiedName(), node.getType());
Pair<Integer, Integer> range = new Pair<Integer, Integer>(start, end);
_tmpVars.put(pair, range);
}
}
return true;
}
private static Type extractType(org.eclipse.jdt.core.dom.VariableDeclaration variableDeclaration) {
Type returnedVariableType = null;
if(variableDeclaration instanceof SingleVariableDeclaration) {
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)variableDeclaration;
returnedVariableType = singleVariableDeclaration.getType();
}
else if(variableDeclaration instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration;
if(fragment.getParent() instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)fragment.getParent();
returnedVariableType = variableDeclarationStatement.getType();
}
else if(fragment.getParent() instanceof VariableDeclarationExpression) {
VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression)fragment.getParent();
returnedVariableType = variableDeclarationExpression.getType();
}
else if(fragment.getParent() instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
returnedVariableType = fieldDeclaration.getType();
}
}
return returnedVariableType;
}
private RefactoringStatus checkTempTypeForLocalTypeUsage(){
VariableDeclarationStatement vds= getTempDeclarationStatement();
if (vds == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
Type type= vds.getType();
ITypeBinding binding= type.resolveBinding();
if (binding == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_cannot_promote);
IMethodBinding declaringMethodBinding= getMethodDeclaration().resolveBinding();
ITypeBinding[] methodTypeParameters= declaringMethodBinding == null ? new ITypeBinding[0] : declaringMethodBinding.getTypeParameters();
LocalTypeAndVariableUsageAnalyzer analyzer= new LocalTypeAndVariableUsageAnalyzer(methodTypeParameters);
type.accept(analyzer);
boolean usesLocalTypes= ! analyzer.getUsageOfEnclosingNodes().isEmpty();
fTempTypeUsesClassTypeVariables= analyzer.getClassTypeVariablesUsed();
if (usesLocalTypes)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_uses_type_declared_locally);
return null;
}
private static Type extractType(VariableDeclaration variableDeclaration) {
Type returnedVariableType = null;
if(variableDeclaration instanceof SingleVariableDeclaration) {
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)variableDeclaration;
returnedVariableType = singleVariableDeclaration.getType();
}
else if(variableDeclaration instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration;
if(fragment.getParent() instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)fragment.getParent();
returnedVariableType = variableDeclarationStatement.getType();
}
else if(fragment.getParent() instanceof VariableDeclarationExpression) {
VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression)fragment.getParent();
returnedVariableType = variableDeclarationExpression.getType();
}
else if(fragment.getParent() instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
returnedVariableType = fieldDeclaration.getType();
}
}
return returnedVariableType;
}
public boolean visit(VariableDeclarationStatement node) {
if(isAnonymousClass(node)){
return true;
}
for (Object o : node.fragments()) {
VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
Type type = node.getType();
if(vdf.getExtraDimensions() > 0){
AST ast = AST.newAST(AST.JLS8);
type = ast.newArrayType((Type) ASTNode.copySubtree(ast, type), vdf.getExtraDimensions());
}
map.put(vdf.getName().toString(), type);
}
return true;
}