use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class JavadocContentAccess2 method handleContentElements.
private void handleContentElements(List<? extends ASTNode> nodes, boolean skipLeadingWhitespace) {
ASTNode previousNode = null;
for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext(); ) {
ASTNode child = iter.next();
if (previousNode != null) {
int previousEnd = previousNode.getStartPosition() + previousNode.getLength();
int childStart = child.getStartPosition();
if (previousEnd > childStart) {
// should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
Exception exception = new Exception(//$NON-NLS-1$
"Illegal ASTNode positions: previousEnd=" + previousEnd + ", childStart=" + //$NON-NLS-1$
childStart + ", element=" + //$NON-NLS-1$
fElement.getHandleIdentifier() + ", Javadoc:\n" + //$NON-NLS-1$
fSource);
LOG.error(exception.getMessage(), exception);
} else if (previousEnd != childStart) {
// Need to preserve whitespace before a node that's not
// directly following the previous node (e.g. on a new line)
// due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=206518 :
String textWithStars = fSource.substring(previousEnd, childStart);
String text = removeDocLineIntros(textWithStars);
fBuf.append(text);
}
}
previousNode = child;
if (child instanceof TextElement) {
String text = ((TextElement) child).getText();
if (skipLeadingWhitespace) {
//$NON-NLS-1$ //$NON-NLS-2$
text = text.replaceFirst("^\\s+", "");
}
// workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=233481 :
//$NON-NLS-1$ //$NON-NLS-2$
text = text.replaceAll("(\r\n?|\n)([ \t]*\\*)", "$1");
handleText(text);
} else if (child instanceof TagElement) {
handleInlineTagElement((TagElement) child);
} else {
// This is unexpected. Fail gracefully by just copying the source.
int start = child.getStartPosition();
String text = fSource.substring(start, start + child.getLength());
fBuf.append(removeDocLineIntros(text));
}
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class SemanticHighlightings method getBinding.
/**
* Extracts the binding from the token's simple name.
* Works around bug 62605 to return the correct constructor binding in a ClassInstanceCreation.
*
* @param token
* the token to extract the binding from
* @return the token's binding, or <code>null</code>
*/
private static IBinding getBinding(SemanticToken token) {
ASTNode node = token.getNode();
ASTNode normalized = ASTNodes.getNormalizedNode(node);
if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
// work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605
return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding();
}
return token.getBinding();
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class InferTypeArgumentsRefactoring method rewriteCastVariable.
private static ASTNode rewriteCastVariable(CastVariable2 castCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel) {
//, List positionGroups) {
ASTNode node = castCv.getRange().getNode(rewrite.getRoot());
ConstraintVariable2 expressionVariable = castCv.getExpressionVariable();
ConstraintVariable2 methodReceiverCv = tCModel.getMethodReceiverCv(expressionVariable);
if (methodReceiverCv != null) {
TType chosenReceiverType = InferTypeArgumentsConstraintsSolver.getChosenType(methodReceiverCv);
if (chosenReceiverType == null)
return null;
else if (!InferTypeArgumentsTCModel.isAGenericType(chosenReceiverType))
return null;
else if (hasUnboundElement(methodReceiverCv, tCModel))
return null;
}
CastExpression castExpression = (CastExpression) node;
Expression expression = castExpression.getExpression();
ASTNode nodeToReplace;
if (castExpression.getParent() instanceof ParenthesizedExpression)
nodeToReplace = castExpression.getParent();
else
nodeToReplace = castExpression;
Expression newExpression = (Expression) rewrite.getASTRewrite().createMoveTarget(expression);
rewrite.getASTRewrite().replace(nodeToReplace, newExpression, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_removeCast));
rewrite.getImportRemover().registerRemovedNode(nodeToReplace);
return newExpression;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class SourceProvider method replaceParameterWithExpression.
private void replaceParameterWithExpression(ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
Expression[] arguments = context.arguments;
try {
ITextFileBuffer buffer = RefactoringFileBuffers.acquire(context.compilationUnit);
for (int i = 0; i < arguments.length; i++) {
Expression expression = arguments[i];
String expressionString = null;
if (expression instanceof SimpleName) {
expressionString = ((SimpleName) expression).getIdentifier();
} else {
try {
expressionString = buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
} catch (BadLocationException exception) {
JavaPlugin.log(exception);
continue;
}
}
ParameterData parameter = getParameterData(i);
List<SimpleName> references = parameter.references();
for (Iterator<SimpleName> iter = references.iterator(); iter.hasNext(); ) {
ASTNode element = iter.next();
Expression newExpression = (Expression) rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
AST ast = rewriter.getAST();
ITypeBinding explicitCast = ASTNodes.getExplicitCast(expression, (Expression) element);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
newExpression = createParenthesizedExpression(newExpression, ast);
}
cast.setExpression(newExpression);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(expression, importRewrite);
cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
expression = newExpression = cast;
}
if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
newExpression = createParenthesizedExpression(newExpression, ast);
}
rewriter.replace(element, newExpression, null);
}
}
} finally {
RefactoringFileBuffers.release(context.compilationUnit);
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class SourceProvider method isDangligIf.
public boolean isDangligIf() {
List<Statement> statements = fDeclaration.getBody().statements();
if (statements.size() != 1)
return false;
ASTNode p = statements.get(0);
while (true) {
if (p instanceof IfStatement) {
return ((IfStatement) p).getElseStatement() == null;
} else {
ChildPropertyDescriptor childD;
if (p instanceof WhileStatement) {
childD = WhileStatement.BODY_PROPERTY;
} else if (p instanceof ForStatement) {
childD = ForStatement.BODY_PROPERTY;
} else if (p instanceof EnhancedForStatement) {
childD = EnhancedForStatement.BODY_PROPERTY;
} else if (p instanceof DoStatement) {
childD = DoStatement.BODY_PROPERTY;
} else if (p instanceof LabeledStatement) {
childD = LabeledStatement.BODY_PROPERTY;
} else {
return false;
}
Statement body = (Statement) p.getStructuralProperty(childD);
if (body instanceof Block) {
return false;
} else {
p = body;
}
}
}
}
Aggregations