下面列出了org.eclipse.jdt.core.dom.VariableDeclarationExpression#fragments ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean visit(VariableDeclarationExpression node) {
ASTNode parent = node.getParent();
while(parent != null){
if(parent instanceof Block || parent instanceof ForStatement){
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 VarDeclarationExpr visit(VariableDeclarationExpression node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
VarDeclarationExpr varDeclarationExpr = new VarDeclarationExpr(startLine, endLine, node);
varDeclarationExpr.setDeclType(node.getType());
List<Vdf> vdfs = new ArrayList<>();
for(Object object : node.fragments()){
Vdf vdf = (Vdf) process((ASTNode) object);
vdf.setParent(varDeclarationExpr);
vdfs.add(vdf);
}
varDeclarationExpr.setVarDeclFrags(vdfs);
return varDeclarationExpr;
}
@Override
public void endVisit(VariableDeclarationExpression node) {
// Constrain the types of the child VariableDeclarationFragments to be equal to one
// another, since the initializers in a 'for' statement can only have one type.
// Pairwise constraints between adjacent variables is enough.
Type type= node.getType();
ConstraintVariable2 typeCv= getConstraintVariable(type);
if (typeCv == null)
return;
setConstraintVariable(node, typeCv);
List<VariableDeclarationFragment> fragments= node.fragments();
for (Iterator<VariableDeclarationFragment> iter= fragments.iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment= iter.next();
ConstraintVariable2 fragmentCv= getConstraintVariable(fragment);
fTCModel.createElementEqualsConstraints(typeCv, fragmentCv);
}
}
public boolean visit(VariableDeclarationExpression 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;
}
/**
* Looks for variables declared in for loops.
*/
@Override
public boolean visit(final VariableDeclarationExpression node) {
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
addBinding(node, frag.getName(), node.getType());
}
return true;
}
@Override
public boolean visit(final VariableDeclarationExpression node) {
final ASTNode parent = node.getParent();
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
variableScopes.put(parent, new Variable(frag.getName()
.getIdentifier(), node.getType().toString(),
ScopeType.SCOPE_LOCAL));
}
return false;
}
/**
* Looks for variables declared in for loops.
*/
@Override
public boolean visit(final VariableDeclarationExpression node) {
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
addBinding(node, frag.getName().getIdentifier());
}
return true;
}
/**
* Looks for variables declared in for loops.
*/
@Override
public boolean visit(final VariableDeclarationExpression node) {
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
addBinding(node, frag.getName().getIdentifier(), node.getType());
}
return true;
}
@Override
public boolean visit(final VariableDeclarationExpression node) {
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragment;
final IdentifierInformation vd = new IdentifierInformation(SHA,
file, vdf.getName().getIdentifier(), node.getType()
.toString(), getLineNumber(vdf),
getAstParentString(vdf));
identifiers.add(vd);
}
return super.visit(node);
}
@Override
public boolean visit(final VariableDeclarationExpression node) {
final ASTNode parent = node.getParent();
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
variableScopes.put(parent, new Variable(frag.getName()
.getIdentifier(), node.getType().toString(),
ScopeType.SCOPE_LOCAL));
}
return false;
}
/**
* Looks for variables declared in for loops.
*/
@Override
public boolean visit(final VariableDeclarationExpression node) {
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
addBinding(node, frag.getName().getIdentifier());
}
return true;
}
private IStatus checkIteratorCondition() {
List<Expression> initializers= getForStatement().initializers();
if (initializers.size() != 1)
return SEMANTIC_CHANGE_WARNING_STATUS;
Expression expression= initializers.get(0);
if (!(expression instanceof VariableDeclarationExpression))
return SEMANTIC_CHANGE_WARNING_STATUS;
VariableDeclarationExpression declaration= (VariableDeclarationExpression)expression;
List<VariableDeclarationFragment> variableDeclarationFragments= declaration.fragments();
if (variableDeclarationFragments.size() != 1)
return SEMANTIC_CHANGE_WARNING_STATUS;
VariableDeclarationFragment declarationFragment= variableDeclarationFragments.get(0);
Expression initializer= declarationFragment.getInitializer();
if (!(initializer instanceof MethodInvocation))
return SEMANTIC_CHANGE_WARNING_STATUS;
MethodInvocation methodInvocation= (MethodInvocation)initializer;
String methodName= methodInvocation.getName().getIdentifier();
if (!"iterator".equals(methodName) || methodInvocation.arguments().size() != 0) //$NON-NLS-1$
return SEMANTIC_CHANGE_WARNING_STATUS;
return StatusInfo.OK_STATUS;
}
private void handleResourceDeclarations(TryStatement tryStatement) {
List<VariableDeclarationExpression> resources= tryStatement.resources();
for (Iterator<VariableDeclarationExpression> iterator= resources.iterator(); iterator.hasNext();) {
iterator.next().accept(this);
}
//check if the exception is thrown as a result of resource#close()
boolean exitMarked= false;
for (VariableDeclarationExpression variable : resources) {
Type type= variable.getType();
IMethodBinding methodBinding= Bindings.findMethodInHierarchy(type.resolveBinding(), "close", new ITypeBinding[0]); //$NON-NLS-1$
if (methodBinding != null) {
ITypeBinding[] exceptionTypes= methodBinding.getExceptionTypes();
for (int j= 0; j < exceptionTypes.length; j++) {
if (matches(exceptionTypes[j])) { // a close() throws the caught exception
// mark name of resource
for (VariableDeclarationFragment fragment : (List<VariableDeclarationFragment>) variable.fragments()) {
SimpleName name= fragment.getName();
fResult.add(new OccurrenceLocation(name.getStartPosition(), name.getLength(), 0, fDescription));
}
if (!exitMarked) {
// mark exit position
exitMarked= true;
Block body= tryStatement.getBody();
int offset= body.getStartPosition() + body.getLength() - 1; // closing bracket of try block
fResult.add(new OccurrenceLocation(offset, 1, 0, Messages.format(SearchMessages.ExceptionOccurrencesFinder_occurrence_implicit_close_description,
BasicElementLabels.getJavaElementName(fException.getName()))));
}
}
}
}
}
}
public boolean visit(VariableDeclarationExpression expr) {
List modifiers = expr.modifiers();
for (int i = 0; i < modifiers.size(); i++) {
visit((Modifier) modifiers.get(i));
}
// Append Type
handleType(expr.getType());
// Visit Fragments
List fragments = expr.fragments();
for (int i = 0; i < fragments.size(); i++) {
visit((VariableDeclarationFragment) fragments.get(i));
}
return false;
}
@Override
public boolean visit(final VariableDeclarationExpression node) {
final ASTNode parent = node.getParent();
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
variableScopes.put(parent, new Variable(frag.getName()
.getIdentifier(), node.getType().toString(),
ScopeType.SCOPE_LOCAL));
}
return false;
}
/**
* Looks for variables declared in for loops.
*/
@Override
public boolean visit(final VariableDeclarationExpression node) {
for (final Object fragment : node.fragments()) {
final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
addBinding(node, frag.getName().getIdentifier());
}
return true;
}