use of org.eclipse.jdt.core.dom.ImportDeclaration in project eap-additional-testsuite by jboss-set.
the class SourceParser method parse.
public static void parse(String str) throws IOException {
fields.clear();
methods.clear();
imports.clear();
types.clear();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(readFileToString(str).toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
Set names = new HashSet();
int blockCount = 0;
public boolean visit(FieldDeclaration node) {
String name = node.fragments().get(0).toString().split("=")[0].trim();
String type = node.getType().toString();
if (!node.modifiers().toString().contains("private")) {
fields.put(name, type);
}
ArrayList<String> types0 = new ArrayList<>();
String type2 = null;
do {
if (type.contains("[")) {
type = type.replaceAll("\\[\\]", "");
if (type.contains("[")) {
type = type.substring(0, type.indexOf("["));
}
}
if (type.contains("<")) {
String type3 = type;
type = type.substring(0, type.indexOf("<"));
if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
type2 = null;
} else if (type3.indexOf("<") >= 0 && type3.indexOf(">") >= 0 && type3.indexOf("<") < type3.indexOf(">")) {
type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
if (type2.contains(",")) {
if (type2.substring(0, type2.indexOf(",")).contains("<")) {
types0.add(type2);
} else {
types0.add(type2.substring(0, type2.indexOf(",")));
types0.add(type2.substring(type2.indexOf(",") + 1));
}
} else {
types0.add(type2);
}
}
}
types.addAll(Arrays.asList(type.split(" extends ")));
if (types0.size() != 0) {
type = types0.remove(0);
} else {
type = null;
}
} while (type != null);
return true;
}
public boolean visit(MethodDeclaration node) {
if (node.getName().getIdentifier() != null) {
MethodInfo2 mf = new MethodInfo2();
mf.name = node.getName().toString();
if (node.getReturnType2() != null) {
mf.returnType = node.getReturnType2().toString();
} else {
mf.returnType = null;
}
List params = node.parameters();
ArrayList<String> types = new ArrayList<>();
// System.out.println("params : " + params.toString());
for (Object s : params) {
String type = ((SingleVariableDeclaration) s).getType().toString();
if (type.startsWith("class "))
type = type.replaceFirst("class ", "");
types.add(type);
}
// System.out.println("sourceTypes : " + types.toString());
mf.paramTypes = types;
methods.put(mf.name, mf);
}
return true;
}
public boolean visit(ImportDeclaration node) {
imports.add(node.getName().toString());
return true;
}
public boolean visit(PackageDeclaration node) {
packageName = node.getName().toString();
return true;
}
});
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.
the class UnusedCodeFix method createRemoveUnusedImportFix.
public static UnusedCodeFix createRemoveUnusedImportFix(CompilationUnit compilationUnit, IProblemLocation problem) {
if (isUnusedImport(problem)) {
ImportDeclaration node = getImportDeclaration(problem, compilationUnit);
if (node != null) {
String label = FixMessages.UnusedCodeFix_RemoveImport_description;
RemoveImportOperation operation = new RemoveImportOperation(node);
Map<String, String> options = new Hashtable<String, String>();
options.put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, CleanUpOptions.TRUE);
return new UnusedCodeFix(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, options);
}
}
return null;
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.
the class ScopeAnalyzer method getUsedVariableNames.
public Collection<String> getUsedVariableNames(int offset, int length) {
HashSet<String> result = new HashSet<String>();
IBinding[] bindingsBefore = getDeclarationsInScope(offset, VARIABLES);
for (int i = 0; i < bindingsBefore.length; i++) {
result.add(bindingsBefore[i].getName());
}
IBinding[] bindingsAfter = getDeclarationsAfter(offset + length, VARIABLES);
for (int i = 0; i < bindingsAfter.length; i++) {
result.add(bindingsAfter[i].getName());
}
List<ImportDeclaration> imports = fRoot.imports();
for (int i = 0; i < imports.size(); i++) {
ImportDeclaration decl = imports.get(i);
if (decl.isStatic() && !decl.isOnDemand()) {
result.add(ASTNodes.getSimpleNameIdentifier(decl.getName()));
}
}
return result;
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.
the class ReorgCorrectionsSubProcessor method importNotFoundProposals.
public static void importNotFoundProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
ImportDeclaration importDeclaration = (ImportDeclaration) ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
if (importDeclaration == null) {
return;
}
if (!importDeclaration.isOnDemand()) {
Name name = importDeclaration.getName();
if (importDeclaration.isStatic() && name.isQualifiedName()) {
name = ((QualifiedName) name).getQualifier();
}
int kind = JavaModelUtil.is50OrHigher(cu.getJavaProject()) ? SimilarElementsRequestor.REF_TYPES : SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
UnresolvedElementsSubProcessor.addNewTypeProposals(cu, name, kind, IProposalRelevance.IMPORT_NOT_FOUND_NEW_TYPE, proposals);
}
String name = ASTNodes.asString(importDeclaration.getName());
if (importDeclaration.isOnDemand()) {
//$NON-NLS-1$
name = JavaModelUtil.concatenateName(name, "*");
}
addProjectSetupFixProposal(context, problem, name, proposals);
}
use of org.eclipse.jdt.core.dom.ImportDeclaration in project che by eclipse.
the class ImportRemover method divideTypeRefs.
private void divideTypeRefs(List<SimpleName> importNames, List<SimpleName> staticNames, List<SimpleName> removedRefs, List<SimpleName> unremovedRefs) {
final List<int[]> removedStartsEnds = new ArrayList<int[]>();
fRoot.accept(new ASTVisitor(true) {
int fRemovingStart = -1;
@Override
public void preVisit(ASTNode node) {
Object property = node.getProperty(PROPERTY_KEY);
if (property == REMOVED) {
if (fRemovingStart == -1) {
fRemovingStart = node.getStartPosition();
} else {
/*
* Bug in client code: REMOVED node should not be nested inside another REMOVED node without
* an intermediate RETAINED node.
* Drop REMOVED property to prevent problems later (premature end of REMOVED section).
*/
node.setProperty(PROPERTY_KEY, null);
}
} else if (property == RETAINED) {
if (fRemovingStart != -1) {
removedStartsEnds.add(new int[] { fRemovingStart, node.getStartPosition() });
fRemovingStart = -1;
} else {
/*
* Bug in client code: RETAINED node should not be nested inside another RETAINED node without
* an intermediate REMOVED node and must have an enclosing REMOVED node.
* Drop RETAINED property to prevent problems later (premature restart of REMOVED section).
*/
node.setProperty(PROPERTY_KEY, null);
}
}
super.preVisit(node);
}
@Override
public void postVisit(ASTNode node) {
Object property = node.getProperty(PROPERTY_KEY);
if (property == RETAINED) {
int end = node.getStartPosition() + node.getLength();
fRemovingStart = end;
} else if (property == REMOVED) {
if (fRemovingStart != -1) {
int end = node.getStartPosition() + node.getLength();
removedStartsEnds.add(new int[] { fRemovingStart, end });
fRemovingStart = -1;
}
}
super.postVisit(node);
}
});
for (Iterator<SimpleName> iterator = importNames.iterator(); iterator.hasNext(); ) {
SimpleName name = iterator.next();
if (isInRemoved(name, removedStartsEnds))
removedRefs.add(name);
else
unremovedRefs.add(name);
}
for (Iterator<SimpleName> iterator = staticNames.iterator(); iterator.hasNext(); ) {
SimpleName name = iterator.next();
if (isInRemoved(name, removedStartsEnds))
removedRefs.add(name);
else
unremovedRefs.add(name);
}
for (Iterator<ImportDeclaration> iterator = fInlinedStaticImports.iterator(); iterator.hasNext(); ) {
ImportDeclaration importDecl = iterator.next();
Name name = importDecl.getName();
if (name instanceof QualifiedName)
name = ((QualifiedName) name).getName();
removedRefs.add((SimpleName) name);
}
}
Aggregations