下面列出了怎么用org.eclipse.jdt.core.dom.VariableDeclarationExpression的API类实例代码及写法,或者点击链接到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 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;
}
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 void clearAccessMode(FlowInfo info, List<? extends ASTNode> nodes) {
if (nodes == null || nodes.isEmpty() || info == null) {
return;
}
for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext();) {
Object node = iter.next();
Iterator<VariableDeclarationFragment> fragments = null;
if (node instanceof VariableDeclarationStatement) {
fragments = ((VariableDeclarationStatement) node).fragments().iterator();
} else if (node instanceof VariableDeclarationExpression) {
fragments = ((VariableDeclarationExpression) node).fragments().iterator();
}
if (fragments != null) {
while (fragments.hasNext()) {
clearAccessMode(info, fragments.next());
}
}
}
}
@Override
public boolean visit(TryStatement node) {
if (traverseNode(node)) {
fFlowContext.pushExcptions(node);
for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
iterator.next().accept(this);
}
node.getBody().accept(this);
fFlowContext.popExceptions();
List<CatchClause> catchClauses = node.catchClauses();
for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
iter.next().accept(this);
}
Block finallyBlock = node.getFinally();
if (finallyBlock != null) {
finallyBlock.accept(this);
}
}
return false;
}
@Override
public void endVisit(TryStatement node) {
if (skipNode(node)) {
return;
}
TryFlowInfo info = createTry();
setFlowInfo(node, info);
for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
info.mergeResources(getFlowInfo(iterator.next()), fFlowContext);
}
info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
for (Iterator<CatchClause> iter = node.catchClauses().iterator(); iter.hasNext();) {
CatchClause element = iter.next();
info.mergeCatch(getFlowInfo(element), fFlowContext);
}
info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
@Override
public boolean visit(final VariableDeclarationExpression it) {
final Procedure2<VariableDeclarationFragment, Integer> _function = (VariableDeclarationFragment frag, Integer counter) -> {
this.appendModifiers(it, it.modifiers());
this.appendToBuffer(this._aSTFlattenerUtils.handleVariableDeclaration(it.modifiers()));
this.appendSpaceToBuffer();
it.getType().accept(this);
this.appendSpaceToBuffer();
frag.accept(this);
int _size = it.fragments().size();
int _minus = (_size - 1);
boolean _lessThan = ((counter).intValue() < _minus);
if (_lessThan) {
this.appendToBuffer(",");
this.appendSpaceToBuffer();
}
};
IterableExtensions.<VariableDeclarationFragment>forEach(it.fragments(), _function);
return false;
}
private static CodeElementType extractVariableDeclarationType(org.eclipse.jdt.core.dom.VariableDeclaration variableDeclaration) {
if(variableDeclaration instanceof SingleVariableDeclaration) {
return CodeElementType.SINGLE_VARIABLE_DECLARATION;
}
else if(variableDeclaration instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration;
if(fragment.getParent() instanceof VariableDeclarationStatement) {
return CodeElementType.VARIABLE_DECLARATION_STATEMENT;
}
else if(fragment.getParent() instanceof VariableDeclarationExpression) {
return CodeElementType.VARIABLE_DECLARATION_EXPRESSION;
}
else if(fragment.getParent() instanceof FieldDeclaration) {
return CodeElementType.FIELD_DECLARATION;
}
}
return null;
}
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;
}
}
@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);
}
}
private RefactoringStatus checkSelection(VariableDeclaration decl) {
ASTNode parent= decl.getParent();
if (parent instanceof MethodDeclaration) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_method_parameter);
}
if (parent instanceof CatchClause) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_exceptions_declared);
}
if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == ForStatement.INITIALIZERS_PROPERTY) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_for_initializers);
}
if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_resource_in_try_with_resources);
}
if (decl.getInitializer() == null) {
String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_not_initialized, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier()));
return RefactoringStatus.createFatalErrorStatus(message);
}
return checkAssignments(decl);
}
private void clearAccessMode(FlowInfo info, List<? extends ASTNode> nodes) {
if (nodes== null || nodes.isEmpty() || info == null)
return;
for (Iterator<? extends ASTNode> iter= nodes.iterator(); iter.hasNext(); ) {
Object node= iter.next();
Iterator<VariableDeclarationFragment> fragments= null;
if (node instanceof VariableDeclarationStatement) {
fragments= ((VariableDeclarationStatement)node).fragments().iterator();
} else if (node instanceof VariableDeclarationExpression) {
fragments= ((VariableDeclarationExpression)node).fragments().iterator();
}
if (fragments != null) {
while (fragments.hasNext()) {
clearAccessMode(info, fragments.next());
}
}
}
}
private RefactoringStatus checkExpression() throws JavaModelException {
Expression selectedExpression= getSelectedExpression().getAssociatedExpression();
if (selectedExpression != null) {
final ASTNode parent= selectedExpression.getParent();
if (selectedExpression instanceof NullLiteral) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals);
} else if (selectedExpression instanceof ArrayInitializer) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer);
} else if (selectedExpression instanceof Assignment) {
if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment);
else
return null;
} else if (selectedExpression instanceof SimpleName) {
if ((((SimpleName) selectedExpression)).isDeclaration())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations);
if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression);
} else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources);
}
}
return null;
}
/**
* Returns the type node for the given declaration.
*
* @param declaration the declaration
* @return the type node or <code>null</code> if the given declaration represents a type
* inferred parameter in lambda expression
*/
public static Type getType(VariableDeclaration declaration) {
if (declaration instanceof SingleVariableDeclaration) {
return ((SingleVariableDeclaration)declaration).getType();
} else if (declaration instanceof VariableDeclarationFragment) {
ASTNode parent= ((VariableDeclarationFragment)declaration).getParent();
if (parent instanceof VariableDeclarationExpression)
return ((VariableDeclarationExpression)parent).getType();
else if (parent instanceof VariableDeclarationStatement)
return ((VariableDeclarationStatement)parent).getType();
else if (parent instanceof FieldDeclaration)
return ((FieldDeclaration)parent).getType();
else if (parent instanceof LambdaExpression)
return null;
}
Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$
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$
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean visit(VariableDeclarationExpression node) {
if (fAddFinalLocals && node.fragments().size() == 1) {
SimpleName name= ((VariableDeclarationFragment)node.fragments().get(0)).getName();
IBinding binding= name.resolveBinding();
if (binding == null)
return false;
if (fWrittenVariables.containsKey(binding))
return false;
ModifierChangeOperation op= createAddFinalOperation(name, node);
if (op == null)
return false;
fResult.add(op);
return false;
}
return false;
}
private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
if (decl == null)
return null;
IBinding binding= name.resolveBinding();
if (!canAddFinal(binding, decl))
return null;
if (decl instanceof SingleVariableDeclaration) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationExpression) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationFragment){
VariableDeclarationFragment frag= (VariableDeclarationFragment)decl;
decl= decl.getParent();
if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
List<VariableDeclarationFragment> list= new ArrayList<VariableDeclarationFragment>();
list.add(frag);
return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationExpression) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
}
}
return null;
}
protected TryStatement copyTryStatement(ASTRewrite sourceRewriter, AST ast, TryStatement tryStatementParent) {
TryStatement newTryStatement = ast.newTryStatement();
ListRewrite resourceRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.RESOURCES_PROPERTY);
List<VariableDeclarationExpression> resources = tryStatementParent.resources();
for(VariableDeclarationExpression expression : resources) {
resourceRewrite.insertLast(expression, null);
}
ListRewrite catchClauseRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
List<CatchClause> catchClauses = tryStatementParent.catchClauses();
for(CatchClause catchClause : catchClauses) {
catchClauseRewrite.insertLast(catchClause, null);
}
if(tryStatementParent.getFinally() != null) {
sourceRewriter.set(newTryStatement, TryStatement.FINALLY_PROPERTY, tryStatementParent.getFinally(), null);
}
return newTryStatement;
}
/**
* Generates the Assignment in an iterator based for, used in the first statement of an iterator
* based <code>for</code> loop body, to retrieve the next element of the {@link Iterable}
* instance.
*
* @param rewrite the current instance of {@link ASTRewrite}
* @param loopOverType the {@link ITypeBinding} of the loop variable
* @param loopVariableName the name of the loop variable
* @return an {@link Assignment}, which retrieves the next element of the {@link Iterable} using
* the active {@link Iterator}
*/
private Assignment getIteratorBasedForBodyAssignment(ASTRewrite rewrite, ITypeBinding loopOverType, SimpleName loopVariableName) {
AST ast= rewrite.getAST();
Assignment assignResolvedVariable= ast.newAssignment();
// left hand side
SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment();
resolvedVariableDeclarationFragment.setName(resolvedVariableName);
VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
// right hand side
MethodInvocation invokeIteratorNextExpression= ast.newMethodInvocation();
invokeIteratorNextExpression.setName(ast.newSimpleName("next")); //$NON-NLS-1$
SimpleName currentElementName= ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(currentElementName), LinkedPositionGroup.NO_STOP, currentElementName.getIdentifier());
invokeIteratorNextExpression.setExpression(currentElementName);
assignResolvedVariable.setRightHandSide(invokeIteratorNextExpression);
assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
return assignResolvedVariable;
}
@Override
public void write(ASTNode node) {
// Variable declaration statements & expressions are quite similar, so we handle them both
// here together
if (node instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) node;
writeVariableDeclaration(variableDeclarationStatement.modifiers(), variableDeclarationStatement.getType(),
variableDeclarationStatement.fragments());
copySpaceAndComments();
matchAndWrite(";");
} else {
VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;
writeVariableDeclaration(variableDeclarationExpression.modifiers(),
variableDeclarationExpression.getType(), variableDeclarationExpression.fragments());
}
}
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;
}
@Override
public void endVisit(VariableDeclarationExpression node) {
if (skipNode(node)) {
return;
}
GenericSequentialFlowInfo info = processSequential(node, node.getType());
process(info, node.fragments());
}
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
List<IVariableBinding> forInitializerVariables = new ArrayList<>(1);
for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = iter.next();
IVariableBinding binding = fragment.resolveBinding();
if (binding != null) {
forInitializerVariables.add(binding);
}
}
return forInitializerVariables;
}
private RefactoringStatus checkExpression() throws JavaModelException {
Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
if (selectedExpression != null) {
final ASTNode parent = selectedExpression.getParent();
if (selectedExpression instanceof NullLiteral) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals);
} else if (selectedExpression instanceof ArrayInitializer) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer);
} else if (selectedExpression instanceof Assignment) {
if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression)) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment);
} else {
return null;
}
} else if (selectedExpression instanceof SimpleName) {
if ((((SimpleName) selectedExpression)).isDeclaration()) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations);
}
if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression);
}
} else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources);
}
}
return null;
}
@Override
public void endVisit(VariableDeclarationExpression node) {
if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED && getFirstSelectedNode() == node) {
if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_resource_in_try_with_resources, JavaStatusContext.create(fCUnit, getSelection()));
}
}
checkTypeInDeclaration(node.getType());
super.endVisit(node);
}
private RefactoringStatus checkExpression() throws JavaModelException {
Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
if (selectedExpression != null) {
final ASTNode parent = selectedExpression.getParent();
if (selectedExpression instanceof NullLiteral) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals);
} else if (selectedExpression instanceof ArrayInitializer) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer);
} else if (selectedExpression instanceof Assignment) {
if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression)) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment);
} else {
return null;
}
} else if (selectedExpression instanceof SimpleName) {
if ((((SimpleName) selectedExpression)).isDeclaration()) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations);
}
if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression);
}
} else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources);
}
}
return null;
}
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
List<IVariableBinding> forInitializerVariables = new ArrayList<>(1);
for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = iter.next();
IVariableBinding binding = fragment.resolveBinding();
if (binding != null) {
forInitializerVariables.add(binding);
}
}
return forInitializerVariables;
}
@Override
public boolean visit(TryStatement node) {
fCurrentExceptions = new ArrayList<>(1);
fTryStack.push(fCurrentExceptions);
// visit try block
node.getBody().accept(this);
List<VariableDeclarationExpression> resources = node.resources();
for (Iterator<VariableDeclarationExpression> iterator = resources.iterator(); iterator.hasNext();) {
iterator.next().accept(this);
}
// Remove those exceptions that get catch by following catch blocks
List<CatchClause> catchClauses = node.catchClauses();
if (!catchClauses.isEmpty()) {
handleCatchArguments(catchClauses);
}
List<ITypeBinding> current = fTryStack.pop();
fCurrentExceptions = fTryStack.peek();
for (Iterator<ITypeBinding> iter = current.iterator(); iter.hasNext();) {
addException(iter.next(), node.getAST());
}
// visit catch and finally
for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
iter.next().accept(this);
}
if (node.getFinally() != null) {
node.getFinally().accept(this);
}
// return false. We have visited the body by ourselves.
return false;
}
@Override
public boolean visit(VariableDeclarationExpression node) {
if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
Type type = node.getType();
ITypeBinding resourceTypeBinding = type.resolveBinding();
if (resourceTypeBinding != null) {
IMethodBinding methodBinding = Bindings.findMethodInHierarchy(resourceTypeBinding, "close", new ITypeBinding[0]); //$NON-NLS-1$
if (methodBinding != null) {
addExceptions(methodBinding.getExceptionTypes(), node.getAST());
}
}
}
return super.visit(node);
}
@Override
public boolean visit(VariableDeclarationExpression node) {
if (!isSelected(node)) {
return false;
}
return super.visit(node);
}