public static List<String> getCompilationUnitImports(String compilationUnitContents) { List<String> result = new ArrayList<String>(); ASTParser parser = cachedASTParser.getAndSet(null); if (parser == null) { parser = CodeGenUtil.EclipseUtil.newASTParser(true); } parser.setSource(compilationUnitContents.toCharArray()); CompilationUnit compilationUnit = (CompilationUnit)parser.createAST(new NullProgressMonitor()); for (Iterator<?> i = compilationUnit.imports().iterator(); i.hasNext();) { ImportDeclaration importDeclaration = (ImportDeclaration)i.next(); result.add(importDeclaration.getName().getFullyQualifiedName()); } cachedASTParser.set(parser); return result; } }
/** * Creates and returns <code>ASTParser</code>. * * @return new ASTParser object */ protected ASTParser createASTParser() { // caching parser does not parse 2nd file in the same way (javadoc of package for example) // hence, new parser is created every time this method is called ASTParser astParser = CodeGenUtil.EclipseUtil.newASTParser(true); Map<String, String> javaCoreOptions = new HashMap<String, String>(); for (Map.Entry<?, ?> entry : getJavaCoreOptions().entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); if (key instanceof String && value instanceof String) { javaCoreOptions.put((String)key, (String)value); } } astParser.setCompilerOptions(javaCoreOptions); return astParser; }
ASTParser astParser = CodeGenUtil.EclipseUtil.newASTParser(true); astParser.setCompilerOptions(Collections.singletonMap(JavaCore.COMPILER_PB_UNUSED_IMPORT, JavaCore.ERROR)); astParser.setResolveBindings(true);