use of org.eclipse.jdt.core.dom.ASTVisitor in project flux by eclipse.
the class ASTNodes method getLeftMostSimpleName.
public static SimpleName getLeftMostSimpleName(Name name) {
if (name instanceof SimpleName) {
return (SimpleName) name;
} else {
final SimpleName[] result = new SimpleName[1];
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(QualifiedName qualifiedName) {
Name left = qualifiedName.getQualifier();
if (left instanceof SimpleName)
result[0] = (SimpleName) left;
else
left.accept(this);
return false;
}
};
name.accept(visitor);
return result[0];
}
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project flux by eclipse.
the class ASTNodes method getTypeName.
/**
* Returns the simple name of the type, followed by array dimensions.
* Skips qualifiers, type arguments, and type annotations.
* <p>
* Does <b>not</b> work for WildcardTypes, etc.!
*
* @param type a type that has a simple name
* @return the simple name, followed by array dimensions
* @see #getSimpleNameIdentifier(Name)
* @since 3.10
*/
public static String getTypeName(Type type) {
final StringBuffer buffer = new StringBuffer();
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(PrimitiveType node) {
buffer.append(node.getPrimitiveTypeCode().toString());
return false;
}
@Override
public boolean visit(SimpleType node) {
buffer.append(getSimpleNameIdentifier(node.getName()));
return false;
}
@Override
public boolean visit(QualifiedType node) {
buffer.append(node.getName().getIdentifier());
return false;
}
@Override
public boolean visit(NameQualifiedType node) {
buffer.append(node.getName().getIdentifier());
return false;
}
@Override
public boolean visit(ParameterizedType node) {
node.getType().accept(this);
return false;
}
@Override
public void endVisit(ArrayType node) {
for (int i = 0; i < node.dimensions().size(); i++) {
// $NON-NLS-1$
buffer.append("[]");
}
}
};
type.accept(visitor);
return buffer.toString();
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project flux by eclipse.
the class RenameService method computeReferences.
public JSONArray computeReferences(String username, String resourcePath, int offset, int length) {
try {
ICompilationUnit unit = liveEditUnits.getLiveEditUnit(username, resourcePath);
if (unit != null) {
final ASTParser parser = ASTParser.newParser(AST.JLS4);
// Parse the class as a compilation unit.
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
// Return the compiled class as a compilation unit
final ASTNode compilationUnit = parser.createAST(null);
final ASTNode nameNode = NodeFinder.perform(compilationUnit, offset, length);
final List<ASTNode> nodes = new ArrayList<ASTNode>();
if (nameNode instanceof SimpleName) {
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(SimpleName node) {
if (node.getIdentifier().equals(((SimpleName) nameNode).getIdentifier())) {
nodes.add(node);
}
return super.visit(node);
}
});
}
JSONArray references = new JSONArray();
for (ASTNode astNode : nodes) {
JSONObject nodeObject = new JSONObject();
nodeObject.put("offset", astNode.getStartPosition());
nodeObject.put("length", astNode.getLength());
references.put(nodeObject);
}
return references;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project sts4 by spring-projects.
the class ActiveProfilesProvider method getLiveHoverHints.
@Override
public Collection<Range> getLiveHoverHints(Annotation annotation, TextDocument doc, SpringBootApp[] runningApps) {
if (runningApps.length > 0) {
Builder<Range> ranges = ImmutableList.builder();
nameRange(doc, annotation).ifPresent(ranges::add);
Set<String> allActiveProfiles = getAllActiveProfiles(runningApps);
annotation.accept(new ASTVisitor() {
@Override
public boolean visit(StringLiteral node) {
String value = ASTUtils.getLiteralValue(node);
if (value != null && allActiveProfiles.contains(value)) {
rangeOf(doc, node).ifPresent(ranges::add);
}
return true;
}
});
return ranges.build();
}
return ImmutableList.of();
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project sts4 by spring-projects.
the class AbstractSourceLinks method findTypeRegion.
protected Region findTypeRegion(CompilationUnit cu, String fqName) {
if (cu == null) {
return null;
}
int[] values = new int[] { 0, -1 };
int lastDotIndex = fqName.lastIndexOf('.');
String packageName = fqName.substring(0, lastDotIndex);
String typeName = fqName.substring(lastDotIndex + 1);
if (packageName.equals(cu.getPackage().getName().getFullyQualifiedName())) {
Stack<String> visitedType = new Stack<>();
cu.accept(new ASTVisitor() {
private boolean visitDeclaration(AbstractTypeDeclaration node) {
visitedType.push(node.getName().getIdentifier());
if (values[1] < 0) {
if (String.join("$", visitedType.toArray(new String[visitedType.size()])).equals(typeName)) {
values[0] = node.getName().getStartPosition();
values[1] = node.getName().getLength();
}
}
return values[1] < 0;
}
@Override
public boolean visit(TypeDeclaration node) {
return visitDeclaration(node);
}
@Override
public boolean visit(AnnotationTypeDeclaration node) {
return visitDeclaration(node);
}
@Override
public void endVisit(AnnotationTypeDeclaration node) {
visitedType.pop();
super.endVisit(node);
}
@Override
public void endVisit(TypeDeclaration node) {
visitedType.pop();
super.endVisit(node);
}
});
}
return values[1] < 0 ? null : new Region(values[0], values[1]);
}
Aggregations