List of usage examples for org.eclipse.jdt.internal.compiler.env INameEnvironment cleanup
void cleanup();
From source file:com.android.build.gradle.tasks.annotations.ExtractAnnotationsDriver.java
License:Apache License
@SuppressWarnings("MethodMayBeStatic") public void run(@NonNull String[] args) { List<String> classpath = Lists.newArrayList(); List<File> sources = Lists.newArrayList(); List<File> mergePaths = Lists.newArrayList(); List<File> apiFilters = null; File rmTypeDefs = null;//from w w w.j a va 2 s . c o m boolean verbose = true; boolean allowMissingTypes = false; boolean allowErrors = false; boolean listFiltered = true; boolean skipClassRetention = false; String encoding = Charsets.UTF_8.name(); File output = null; File proguard = null; long languageLevel = EcjParser.getLanguageLevel(1, 7); if (args.length == 1 && "--help".equals(args[0])) { usage(System.out); } if (args.length < 2) { usage(System.err); } for (int i = 0, n = args.length; i < n; i++) { String flag = args[i]; if (flag.equals("--quiet")) { verbose = false; continue; } else if (flag.equals("--allow-missing-types")) { allowMissingTypes = true; continue; } else if (flag.equals("--allow-errors")) { allowErrors = true; continue; } else if (flag.equals("--hide-filtered")) { listFiltered = false; continue; } else if (flag.equals("--skip-class-retention")) { skipClassRetention = true; continue; } if (i == n - 1) { usage(System.err); } String value = args[i + 1]; i++; if (flag.equals("--sources")) { sources = getFiles(value); } else if (flag.equals("--classpath")) { classpath = getPaths(value); } else if (flag.equals("--merge-zips")) { mergePaths = getFiles(value); } else if (flag.equals("--output")) { output = new File(value); if (output.exists()) { if (output.isDirectory()) { abort(output + " is a directory"); } boolean deleted = output.delete(); if (!deleted) { abort("Could not delete previous version of " + output); } } else if (output.getParentFile() != null && !output.getParentFile().exists()) { abort(output.getParentFile() + " does not exist"); } } else if (flag.equals("--proguard")) { proguard = new File(value); if (proguard.exists()) { if (proguard.isDirectory()) { abort(proguard + " is a directory"); } boolean deleted = proguard.delete(); if (!deleted) { abort("Could not delete previous version of " + proguard); } } else if (proguard.getParentFile() != null && !proguard.getParentFile().exists()) { abort(proguard.getParentFile() + " does not exist"); } } else if (flag.equals("--encoding")) { encoding = value; } else if (flag.equals("--api-filter")) { if (apiFilters == null) { apiFilters = Lists.newArrayList(); } for (String path : Splitter.on(",").omitEmptyStrings().split(value)) { File apiFilter = new File(path); if (!apiFilter.isFile()) { String message = apiFilter + " does not exist or is not a file"; abort(message); } apiFilters.add(apiFilter); } } else if (flag.equals("--language-level")) { if ("1.6".equals(value)) { languageLevel = EcjParser.getLanguageLevel(1, 6); } else if ("1.7".equals(value)) { languageLevel = EcjParser.getLanguageLevel(1, 7); } else { abort("Unsupported language level " + value); } } else if (flag.equals("--rmtypedefs")) { rmTypeDefs = new File(value); if (!rmTypeDefs.isDirectory()) { abort(rmTypeDefs + " is not a directory"); } } else { System.err.println("Unknown flag " + flag + ": Use --help for usage information"); } } if (sources.isEmpty()) { abort("Must specify at least one source path"); } if (classpath.isEmpty()) { abort("Must specify classpath pointing to at least android.jar or the framework"); } if (output == null && proguard == null) { abort("Must specify output path with --output or a proguard path with --proguard"); } // API definition files ApiDatabase database = null; if (apiFilters != null && !apiFilters.isEmpty()) { try { List<String> lines = Lists.newArrayList(); for (File file : apiFilters) { lines.addAll(Files.readLines(file, Charsets.UTF_8)); } database = new ApiDatabase(lines); } catch (IOException e) { abort("Could not open API database " + apiFilters + ": " + e.getLocalizedMessage()); } } Extractor extractor = new Extractor(database, rmTypeDefs, verbose, !skipClassRetention, true); extractor.setListIgnored(listFiltered); try { Pair<Collection<CompilationUnitDeclaration>, INameEnvironment> pair = parseSources(sources, classpath, encoding, languageLevel); Collection<CompilationUnitDeclaration> units = pair.getFirst(); boolean abort = false; int errorCount = 0; for (CompilationUnitDeclaration unit : units) { // so maybe I don't need my map!! IProblem[] problems = unit.compilationResult().getAllProblems(); if (problems != null) { for (IProblem problem : problems) { if (problem.isError()) { errorCount++; String message = problem.getMessage(); if (allowMissingTypes) { if (message.contains("cannot be resolved")) { continue; } } System.out.println("Error: " + new String(problem.getOriginatingFileName()) + ":" + problem.getSourceLineNumber() + ": " + message); abort = !allowErrors; } } } } if (errorCount > 0) { System.err.println("Found " + errorCount + " errors"); } if (abort) { abort("Not extracting annotations (compilation problems encountered)"); } INameEnvironment environment = pair.getSecond(); extractor.extractFromProjectSource(units); if (mergePaths != null) { for (File jar : mergePaths) { extractor.mergeExisting(jar); } } extractor.export(output, proguard); // Remove typedefs? //noinspection VariableNotUsedInsideIf if (rmTypeDefs != null) { extractor.removeTypedefClasses(); } environment.cleanup(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.android.tools.lint.EcjParser.java
License:Apache License
/** Parse the given source units and class path and store it into the given output map */ public static INameEnvironment parse(CompilerOptions options, @NonNull List<ICompilationUnit> sourceUnits, @NonNull List<String> classPath, @NonNull Map<ICompilationUnit, CompilationUnitDeclaration> outputMap, @Nullable LintClient client) {//from w w w.java2 s . c om INameEnvironment environment = new FileSystem(classPath.toArray(new String[classPath.size()]), new String[0], options.defaultEncoding); IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems(); IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault()); ICompilerRequestor requestor = new ICompilerRequestor() { @Override public void acceptResult(CompilationResult result) { // Not used; we need the corresponding CompilationUnitDeclaration for the source // units (the AST parsed from source) which we don't get access to here, so we // instead subclass AST to get our hands on them. } }; NonGeneratingCompiler compiler = new NonGeneratingCompiler(environment, policy, options, requestor, problemFactory, outputMap); try { compiler.compile(sourceUnits.toArray(new ICompilationUnit[sourceUnits.size()])); } catch (OutOfMemoryError e) { environment.cleanup(); // Since we're running out of memory, if it's all still held we could potentially // fail attempting to log the failure. Actively get rid of the large ECJ data // structure references first so minimize the chance of that //noinspection UnusedAssignment compiler = null; //noinspection UnusedAssignment environment = null; //noinspection UnusedAssignment requestor = null; //noinspection UnusedAssignment problemFactory = null; //noinspection UnusedAssignment policy = null; String msg = "Ran out of memory analyzing .java sources with ECJ: Some lint checks " + "may not be accurate (missing type information from the compiler)"; if (client != null) { // Don't log exception too; this isn't a compiler error per se where we // need to pin point the exact unlucky code that asked for memory when it // had already run out client.log(null, msg); } else { System.out.println(msg); } } catch (Throwable t) { if (client != null) { CompilationUnitDeclaration currentUnit = compiler.getCurrentUnit(); if (currentUnit == null || currentUnit.getFileName() == null) { client.log(t, "ECJ compiler crashed"); } else { client.log(t, "ECJ compiler crashed processing %1$s", new String(currentUnit.getFileName())); } } else { t.printStackTrace(); } environment.cleanup(); environment = null; } return environment; }