use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.
the class FullConstraintCreator method getConstraintsForFieldDeclaringTypes.
//--------- private helpers ----------------//
private Collection<ITypeConstraint> getConstraintsForFieldDeclaringTypes(FieldDeclaration fd) {
Collection<ITypeConstraint> result = new ArrayList<ITypeConstraint>(fd.fragments().size());
for (Iterator<VariableDeclarationFragment> iter = fd.fragments().iterator(); iter.hasNext(); ) {
VariableDeclarationFragment varDecl = iter.next();
IVariableBinding binding = varDecl.resolveBinding();
Assert.isTrue(binding.isField());
result.addAll(Arrays.asList(fTypeConstraintFactory.createDefinesConstraint(fConstraintVariableFactory.makeDeclaringTypeVariable(binding), fConstraintVariableFactory.makeRawBindingVariable(binding.getDeclaringClass()))));
}
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project AutoRefactor by JnRouvignac.
the class TryWithResourceRefactoring method visit.
@Override
public boolean visit(TryStatement node) {
final List<Statement> tryStmts = asList(node.getBody());
if (tryStmts.size() >= 1 && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
if (innerTryStmt != null && !innerTryStmt.resources().isEmpty() && innerTryStmt.catchClauses().isEmpty()) {
return collapseTryStatements(node, innerTryStmt);
}
}
final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node), VariableDeclarationStatement.class);
if (previousDeclStmt == null) {
return VISIT_SUBTREE;
}
final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
final List<Statement> finallyStmts = asList(node.getFinally());
if (previousDeclFragment != null && finallyStmts.size() >= 1) {
final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
nodesToRemove.add(previousDeclStmt);
final Statement finallyStmt = finallyStmts.get(0);
nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);
final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
if (finallyEs != null) {
final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
return refactorToTryWithResources(node, newResource, nodesToRemove);
}
} else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1 && asList(finallyIs.getElseStatement()).isEmpty()) {
final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());
final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
return refactorToTryWithResources(node, newResource, nodesToRemove);
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project bndtools by bndtools.
the class AbstractBuildErrorDetailsHandler method createFieldMarkerData.
/**
* Create a marker on a Java Method
*
* @param javaProject
* @param className
* - the fully qualified class name (e.g java.lang.String)
* @param methodName
* @param methodSignature
* - signatures are in "internal form" e.g. (Ljava.lang.Integer;[Ljava/lang/String;Z)V
* @param markerAttributes
* - attributes that should be included in the marker, typically a message. The start and end points for
* the marker are added by this method.
* @param hasResolutions
* - true if the marker will have resolutions
* @return Marker Data that can be used to create an {@link IMarker}, or null if no location can be found
* @throws JavaModelException
*/
public static final MarkerData createFieldMarkerData(IJavaProject javaProject, final String className, final String fieldName, final Map<String, Object> markerAttributes, boolean hasResolutions) throws JavaModelException {
final CompilationUnit ast = createAST(javaProject, className);
if (ast == null)
return null;
ast.accept(new ASTVisitor() {
@Override
public boolean visit(FieldDeclaration fieldDecl) {
if (matches(ast, fieldDecl, fieldName)) {
// Create the marker attribs here
markerAttributes.put(IMarker.CHAR_START, fieldDecl.getStartPosition());
markerAttributes.put(IMarker.CHAR_END, fieldDecl.getStartPosition() + fieldDecl.getLength());
}
return false;
}
private boolean matches(@SuppressWarnings("unused") CompilationUnit ast, FieldDeclaration fieldDecl, String fieldName) {
@SuppressWarnings("unchecked") List<VariableDeclarationFragment> list = (List<VariableDeclarationFragment>) fieldDecl.getStructuralProperty(FieldDeclaration.FRAGMENTS_PROPERTY);
for (VariableDeclarationFragment vdf : list) {
if (fieldName.equals(vdf.getName().toString())) {
return true;
}
}
return false;
}
});
if (!markerAttributes.containsKey(IMarker.CHAR_START))
return null;
return new MarkerData(ast.getJavaElement().getResource(), markerAttributes, hasResolutions);
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project xtext-xtend by eclipse.
the class ASTFlattenerUtils method findDeclaredType.
private Type findDeclaredType(final ASTNode scope, final SimpleName simpleName) {
final ArrayList<Type> matchesFound = CollectionLiterals.<Type>newArrayList();
scope.accept(new ASTVisitor() {
@Override
public boolean visit(final VariableDeclarationFragment node) {
boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
if (_equals) {
final ASTNode parentNode = node.getParent();
boolean _matched = false;
if (parentNode instanceof VariableDeclarationStatement) {
_matched = true;
matchesFound.add(((VariableDeclarationStatement) parentNode).getType());
}
if (!_matched) {
if (parentNode instanceof FieldDeclaration) {
_matched = true;
matchesFound.add(((FieldDeclaration) parentNode).getType());
}
}
if (!_matched) {
if (parentNode instanceof VariableDeclarationExpression) {
_matched = true;
matchesFound.add(((VariableDeclarationExpression) parentNode).getType());
}
}
}
return false;
}
@Override
public boolean preVisit2(final ASTNode node) {
return matchesFound.isEmpty();
}
@Override
public boolean visit(final SingleVariableDeclaration node) {
boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
if (_equals) {
matchesFound.add(node.getType());
}
return false;
}
});
return IterableExtensions.<Type>head(matchesFound);
}
use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final PostfixExpression node) {
final AST dummyAST = AST.newAST(node.getAST().apiLevel());
final PostfixExpression.Operator pfOperator = node.getOperator();
Expression _operand = node.getOperand();
if ((_operand instanceof ArrayAccess)) {
Expression _operand_1 = node.getOperand();
final ArrayAccess pfOperand = ((ArrayAccess) _operand_1);
if ((Objects.equal(pfOperator, PostfixExpression.Operator.INCREMENT) || Objects.equal(pfOperator, PostfixExpression.Operator.DECREMENT))) {
final String arrayName = this.computeArrayName(pfOperand);
StringConcatenation _builder = new StringConcatenation();
_builder.append("_postIndx_");
_builder.append(arrayName);
final String idxName = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("_postVal_");
_builder_1.append(arrayName);
final String tempVarName = _builder_1.toString();
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("{ var ");
_builder_2.append(idxName);
_builder_2.append("=");
this.appendToBuffer(_builder_2.toString());
pfOperand.getIndex().accept(this);
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append(" ");
_builder_3.append("var ");
this.appendToBuffer(_builder_3.toString());
final VariableDeclarationFragment varDeclaration = dummyAST.newVariableDeclarationFragment();
varDeclaration.setName(dummyAST.newSimpleName(tempVarName));
ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, pfOperand);
final ArrayAccess arrayAccess = ((ArrayAccess) _copySubtree);
arrayAccess.setIndex(dummyAST.newSimpleName(idxName));
varDeclaration.setInitializer(arrayAccess);
varDeclaration.accept(this);
final InfixExpression infixOp = dummyAST.newInfixExpression();
infixOp.setLeftOperand(dummyAST.newSimpleName(tempVarName));
PostfixExpression.Operator _operator = node.getOperator();
boolean _equals = Objects.equal(_operator, PostfixExpression.Operator.DECREMENT);
if (_equals) {
infixOp.setOperator(InfixExpression.Operator.MINUS);
} else {
infixOp.setOperator(InfixExpression.Operator.PLUS);
}
infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
final Assignment assigment = dummyAST.newAssignment();
ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, pfOperand);
final ArrayAccess writeArray = ((ArrayAccess) _copySubtree_1);
writeArray.setIndex(dummyAST.newSimpleName(idxName));
assigment.setLeftHandSide(writeArray);
ASTNode _copySubtree_2 = ASTNode.copySubtree(dummyAST, infixOp);
assigment.setRightHandSide(((Expression) _copySubtree_2));
assigment.accept(this);
StringConcatenation _builder_4 = new StringConcatenation();
String _xifexpression = null;
boolean _needsReturnValue = this._aSTFlattenerUtils.needsReturnValue(node);
if (_needsReturnValue) {
_xifexpression = tempVarName;
}
_builder_4.append(_xifexpression);
_builder_4.append(" }");
this.appendToBuffer(_builder_4.toString());
return false;
}
}
node.getOperand().accept(this);
this.appendToBuffer(pfOperator.toString());
return false;
}
Aggregations