Example usage for org.eclipse.jdt.internal.compiler.batch FileSystem FileSystem

List of usage examples for org.eclipse.jdt.internal.compiler.batch FileSystem FileSystem

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.batch FileSystem FileSystem.

Prototype

protected FileSystem(Classpath[] paths, String[] initialFileNames, boolean annotationsFromClasspath) 

Source Link

Usage

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.  j  ava2  s  .c o  m
    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;
}

From source file:com.tsc9526.monalisa.plugin.eclipse.jdt.JDTCompiler.java

License:Open Source License

public void compile(File java) {
    INameEnvironment nameEnvironment = new FileSystem(classPaths, new String[] { java.getAbsolutePath() },
            "UTF-8");

    Compiler jdtCompiler = new Compiler(nameEnvironment, policy, options, compilerRequestor, problemFactory);

    jdtCompiler.compile(new ICompilationUnit[] { new CompilationUnit(java) });
}

From source file:fr.inria.astor.core.manipulation.compiler.bytecode.JDTByteCodeCompiler.java

License:Open Source License

FileSystem getLibraryAccess() {

    // strategy 1
    String bootpath = System.getProperty("sun.boot.class.path");
    Set<String> lst = new HashSet<String>();
    for (String s : bootpath.split(File.pathSeparator)) {
        File f = new File(s);
        if (f.exists()) {
            lst.add(f.getAbsolutePath());
        }/*from   ww w.jav  a 2s  .c  om*/
    }

    // strategy 2
    ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();//ClassLoader.getSystemClassLoader();      
    if (currentClassLoader instanceof URLClassLoader) {
        URL[] urls = ((URLClassLoader) currentClassLoader).getURLs();
        if (urls != null && urls.length > 0) {

            for (URL url : urls) {
                //   classpath+=File.pathSeparator+url.getFile();
                lst.add(url.getFile());
            }

        }
    }

    // strategy 3
    String classpath = System.getProperty("java.class.path");
    for (String s : classpath.split(File.pathSeparator)) {
        File f = new File(s);
        if (f.exists()) {
            lst.add(f.getAbsolutePath());
        }
    }

    //   
    return new FileSystem(lst.toArray(new String[0]), new String[0], System.getProperty("file.encoding"));
}

From source file:lombok.RunTestsViaEcj.java

License:Open Source License

private FileSystem createFileSystem(File file) {
    List<String> classpath = new ArrayList<String>();
    classpath.addAll(Arrays.asList(System.getProperty("sun.boot.class.path").split(File.pathSeparator)));
    for (Iterator<String> i = classpath.iterator(); i.hasNext();) {
        if (FileSystem.getClasspath(i.next(), "UTF-8", null) == null) {
            i.remove();/*from  w  ww  .  jav a 2 s  .c  om*/
        }
    }
    classpath.add("bin");
    classpath.add("dist/lombok.jar");
    classpath.add("lib/test/commons-logging-commons-logging.jar");
    classpath.add("lib/test/org.slf4j-slf4j-api.jar");
    classpath.add("lib/test/org.slf4j-slf4j-ext.jar");
    classpath.add("lib/test/log4j-log4j.jar");
    classpath.add("lib/test/org.apache.logging.log4j-log4j-api.jar");
    classpath.add("lib/test/com.google.guava-guava.jar");
    classpath.add("lib/test/com.google.code.findbugs-findbugs.jar");
    return new FileSystem(classpath.toArray(new String[0]), new String[] { file.getAbsolutePath() }, "UTF-8");
}

From source file:org.conqat.engine.java.ecj.EcjASTAccess.java

License:Apache License

/**
 * Compiles the given code./*w w  w  .j  a  va  2  s  .c  om*/
 */
public static CompilationUnitDeclaration compileAST(String filePath, String code, String[] classpath,
        String encoding, EcjCompilerOptions options, ICompilerRequestor requestor) {
    INameEnvironment environment = new FileSystem(classpath, new String[0], encoding);

    Compiler compiler = new Compiler(environment, new ErrorHandlingPolicy(), options.obtainOptions(), requestor,
            new DefaultProblemFactory());

    ICompilationUnit[] units = { new CompilationUnit(code.toCharArray(), filePath, encoding) };

    compiler.parser.reportSyntaxErrorIsRequired = true;

    compiler.compile(units);
    CompilationUnitDeclaration declaration = compiler.parser.compilationUnit;
    // In case of classes the above expression will yield the compilation
    // unit declaration. In case of interfaces it is however null. Instead,
    // the expression below holds the compilation unit declaration.
    if (declaration == null && compiler.parser.referenceContext instanceof CompilationUnitDeclaration) {
        declaration = (CompilationUnitDeclaration) compiler.parser.referenceContext;
    }

    return declaration;
}

From source file:org.eclipse.objectteams.otdt.tests.compiler.smap.AbstractSourceMapGeneratorTest.java

License:Open Source License

protected INameEnvironment[] getClassLibs() {
    String encoding = (String) getCompilerOptions().getMap().get(CompilerOptions.OPTION_Encoding);
    if ("".equals(encoding))
        encoding = null;/*from   www  .  j a  v a2s  . c  o m*/

    INameEnvironment[] classLibs = new INameEnvironment[1];
    classLibs[0] = new FileSystem(this.classpaths, new String[] {}, // ignore initial file names
            encoding // default encoding
    );
    return classLibs;
}

From source file:org.eclipse.zest.dot.GraphCreatorViaInternalJdtCompiler.java

License:Open Source License

private URL compileWithInternalJdtCompiler(final File zestFile, final String graphName) {
    /*//from   w  w w  .ja  v a 2 s  . c  o m
     * TODO we need to set up the environment here. Here, we basically hit
     * the same issue as when running with the Java compiler API: we need
     * the classpath
     */
    INameEnvironment nameEnvironment = new FileSystem(new String[0], new String[0], "UTF-8");
    CompilerOptions compilerOptions = new CompilerOptions();
    compilerOptions.generateClassFiles = true;
    compilerOptions.verbose = true;
    org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler(
            nameEnvironment, DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions,
            new ICompilerRequestor() {
                public void acceptResult(final CompilationResult result) {
                    CategorizedProblem[] errors = result.getErrors();
                    for (CategorizedProblem categorizedProblem : errors) {
                        System.out.println(String.format("%s: '%s' (%s, line %s)",
                                categorizedProblem.getMarkerType(), categorizedProblem.getMessage(),
                                new String(categorizedProblem.getOriginatingFileName()),
                                categorizedProblem.getSourceLineNumber()));
                    }

                }
            }, ProblemFactory.getProblemFactory(Locale.getDefault()));

    compiler.compile(new ICompilationUnit[] { new ICompilationUnit() {
        public char[] getFileName() {
            return zestFile.getAbsolutePath().toCharArray();
        }

        public char[][] getPackageName() {
            return null;
        }

        public char[] getMainTypeName() {
            return graphName.toCharArray();
        }

        public char[] getContents() {
            return read(zestFile).toCharArray();
        }
    } });
    try {
        URL url = zestFile.getParentFile().toURI().toURL();
        return url;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.thiesen.ecj4ant.EcjTask.java

License:Open Source License

public FileSystem getLibraryAccess(final List<String> sourceFilenames, final List<String> classPathEntries) {

    final String[] classPathEntriesArray = convertAndCombine(_baseClasspath, classPathEntries);

    return new FileSystem(classPathEntriesArray, sourceFilenames.toArray(new String[0]), DEFAULT_ENCODING);
}

From source file:spoon.support.util.JDTCompiler.java

License:Open Source License

FileSystem getLibraryAccess() {
    String bootpath = System.getProperty("sun.boot.class.path");
    String classpath = System.getProperty("java.class.path");
    List<String> lst = new ArrayList<String>();
    for (String s : bootpath.split(File.pathSeparator)) {
        File f = new File(s);
        if (f.exists()) {
            lst.add(f.getAbsolutePath());
        }// www. j  a va  2  s  . co m
    }
    for (String s : classpath.split(File.pathSeparator)) {
        File f = new File(s);
        if (f.exists()) {
            lst.add(f.getAbsolutePath());
        }
    }
    return new FileSystem(lst.toArray(new String[0]), new String[0], System.getProperty("file.encoding"));
}