Search in sources :

Example 1 with JavaSource

use of org.eclipse.xtext.xbase.compiler.JavaSource in project xtext-eclipse by eclipse.

the class InMemoryJavaCompiler method compile.

public InMemoryJavaCompiler.Result compile(final JavaSource... sources) {
    final InMemoryJavaCompiler.Result result = new InMemoryJavaCompiler.Result(this.parentClassLoader);
    IErrorHandlingPolicy _proceedWithAllProblems = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    final ICompilerRequestor _function = (CompilationResult it) -> {
        ClassFile[] _classFiles = it.getClassFiles();
        for (final ClassFile cf : _classFiles) {
            final Function1<char[], String> _function_1 = (char[] it_1) -> {
                return String.valueOf(it_1);
            };
            result.classMap.put(IterableExtensions.join(ListExtensions.<char[], String>map(((List<char[]>) Conversions.doWrapArray(cf.getCompoundName())), _function_1), "."), cf.getBytes());
        }
    };
    org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler(this.nameEnv, _proceedWithAllProblems, this.compilerOptions, _function, new DefaultProblemFactory() {

        @Override
        public CategorizedProblem createProblem(final char[] originatingFileName, final int problemId, final String[] problemArguments, final int elaborationId, final String[] messageArguments, final int severity, final int startPosition, final int endPosition, final int lineNumber, final int columnNumber) {
            final CategorizedProblem problem = super.createProblem(originatingFileName, problemId, problemArguments, elaborationId, messageArguments, severity, startPosition, endPosition, lineNumber, columnNumber);
            result.compilationProblems.add(problem);
            return problem;
        }

        @Override
        public CategorizedProblem createProblem(final char[] originatingFileName, final int problemId, final String[] problemArguments, final String[] messageArguments, final int severity, final int startPosition, final int endPosition, final int lineNumber, final int columnNumber) {
            final CategorizedProblem problem = super.createProblem(originatingFileName, problemId, problemArguments, messageArguments, severity, startPosition, endPosition, lineNumber, columnNumber);
            result.compilationProblems.add(problem);
            return problem;
        }
    });
    final Function1<JavaSource, CompilationUnit> _function_1 = (JavaSource it) -> {
        char[] _charArray = it.getCode().toCharArray();
        String _fileName = it.getFileName();
        return new CompilationUnit(_charArray, _fileName, null);
    };
    ICompilationUnit[] units = ((ICompilationUnit[]) Conversions.unwrapArray(ListExtensions.<JavaSource, CompilationUnit>map(((List<JavaSource>) Conversions.doWrapArray(sources)), _function_1), ICompilationUnit.class));
    compiler.compile(units);
    return result;
}
Also used : IErrorHandlingPolicy(org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult) JavaSource(org.eclipse.xtext.xbase.compiler.JavaSource) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) CompilationUnit(org.eclipse.jdt.internal.compiler.batch.CompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) CategorizedProblem(org.eclipse.jdt.core.compiler.CategorizedProblem) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult)

Example 2 with JavaSource

use of org.eclipse.xtext.xbase.compiler.JavaSource in project xtext-eclipse by eclipse.

the class OnTheFlyJavaCompiler2 method compileToClass.

public Class<?> compileToClass(final String classname, final String code) {
    String _javaFile = this.toJavaFile(classname);
    JavaSource _javaSource = new JavaSource(_javaFile, code);
    final InMemoryJavaCompiler.Result result = this.inMemoryCompiler.compile(_javaSource);
    try {
        final Function1<CategorizedProblem, Boolean> _function = (CategorizedProblem it) -> {
            return Boolean.valueOf(it.isError());
        };
        boolean _exists = IterableExtensions.<CategorizedProblem>exists(result.getCompilationProblems(), _function);
        if (_exists) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("Java code compiled with errors:");
            _builder.newLine();
            final Function1<CategorizedProblem, Boolean> _function_1 = (CategorizedProblem it) -> {
                return Boolean.valueOf(it.isError());
            };
            String _join = IterableExtensions.join(IterableExtensions.<CategorizedProblem>filter(result.getCompilationProblems(), _function_1), "\n");
            _builder.append(_join);
            _builder.newLineIfNotEmpty();
            _builder.newLine();
            _builder.append("Code was:");
            _builder.newLine();
            _builder.append(code);
            _builder.newLineIfNotEmpty();
            throw new IllegalArgumentException(_builder.toString());
        }
        return result.getClassLoader().loadClass(classname);
    } catch (final Throwable _t) {
        if (_t instanceof ClassNotFoundException) {
            final ClassNotFoundException e = (ClassNotFoundException) _t;
            StringConcatenation _builder_1 = new StringConcatenation();
            _builder_1.append("Couldn\'t load \'");
            _builder_1.append(classname);
            _builder_1.append("\' ");
            _builder_1.newLineIfNotEmpty();
            _builder_1.append("source :");
            _builder_1.newLine();
            _builder_1.append("\t");
            _builder_1.append(code, "\t");
            _builder_1.newLineIfNotEmpty();
            _builder_1.newLine();
            _builder_1.append("PROBLEMS : ");
            _builder_1.newLine();
            _builder_1.append("\t");
            String _join_1 = IterableExtensions.join(result.getCompilationProblems(), "\n");
            _builder_1.append(_join_1, "\t");
            _builder_1.newLineIfNotEmpty();
            throw new IllegalStateException(_builder_1.toString(), e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : CategorizedProblem(org.eclipse.jdt.core.compiler.CategorizedProblem) JavaSource(org.eclipse.xtext.xbase.compiler.JavaSource) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) InMemoryJavaCompiler(org.eclipse.xtext.xbase.compiler.InMemoryJavaCompiler)

Example 3 with JavaSource

use of org.eclipse.xtext.xbase.compiler.JavaSource in project xtext-eclipse by eclipse.

the class OnTheFlyJavaCompiler2 method compileToClasses.

public Map<String, Class<?>> compileToClasses(final Map<String, String> sources) {
    final Function1<Map.Entry<String, String>, JavaSource> _function = (Map.Entry<String, String> it) -> {
        String _javaFile = this.toJavaFile(it.getKey());
        String _value = it.getValue();
        return new JavaSource(_javaFile, _value);
    };
    final InMemoryJavaCompiler.Result result = this.inMemoryCompiler.compile(((JavaSource[]) Conversions.unwrapArray(IterableExtensions.<Map.Entry<String, String>, JavaSource>map(sources.entrySet(), _function), JavaSource.class)));
    try {
        final Function1<CategorizedProblem, Boolean> _function_1 = (CategorizedProblem it) -> {
            return Boolean.valueOf(it.isError());
        };
        boolean _exists = IterableExtensions.<CategorizedProblem>exists(result.getCompilationProblems(), _function_1);
        if (_exists) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("Java code compiled with errors:");
            _builder.newLine();
            final Function1<CategorizedProblem, Boolean> _function_2 = (CategorizedProblem it) -> {
                return Boolean.valueOf(it.isError());
            };
            String _join = IterableExtensions.join(IterableExtensions.<CategorizedProblem>filter(result.getCompilationProblems(), _function_2), "\n");
            _builder.append(_join);
            _builder.newLineIfNotEmpty();
            _builder.newLine();
            _builder.append("Code was:");
            _builder.newLine();
            _builder.append("=========");
            _builder.newLine();
            String _join_1 = IterableExtensions.join(sources.values(), "\n=========\n");
            _builder.append(_join_1);
            _builder.newLineIfNotEmpty();
            _builder.append("=========");
            _builder.newLine();
            throw new IllegalArgumentException(_builder.toString());
        }
        final ClassLoader classLoader = result.getClassLoader();
        final Function1<String, Class<?>> _function_3 = (String it) -> {
            try {
                return classLoader.loadClass(it);
            } catch (Throwable _e) {
                throw Exceptions.sneakyThrow(_e);
            }
        };
        final Function1<Class<?>, String> _function_4 = (Class<?> it) -> {
            return it.getName();
        };
        return IterableExtensions.<String, Class<?>>toMap(IterableExtensions.<String, Class<?>>map(sources.keySet(), _function_3), _function_4);
    } catch (final Throwable _t) {
        if (_t instanceof ClassNotFoundException) {
            final ClassNotFoundException e = (ClassNotFoundException) _t;
            StringConcatenation _builder_1 = new StringConcatenation();
            String _message = e.getMessage();
            _builder_1.append(_message);
            _builder_1.append(" ");
            _builder_1.newLineIfNotEmpty();
            _builder_1.append("source :");
            _builder_1.newLine();
            _builder_1.append("\t");
            _builder_1.append(sources, "\t");
            _builder_1.newLineIfNotEmpty();
            _builder_1.newLine();
            _builder_1.append("PROBLEMS : ");
            _builder_1.newLine();
            _builder_1.append("\t");
            String _join_2 = IterableExtensions.join(result.getCompilationProblems(), "\n");
            _builder_1.append(_join_2, "\t");
            _builder_1.newLineIfNotEmpty();
            throw new IllegalStateException(_builder_1.toString(), e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : CategorizedProblem(org.eclipse.jdt.core.compiler.CategorizedProblem) JavaSource(org.eclipse.xtext.xbase.compiler.JavaSource) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Map(java.util.Map) InMemoryJavaCompiler(org.eclipse.xtext.xbase.compiler.InMemoryJavaCompiler)

Aggregations

CategorizedProblem (org.eclipse.jdt.core.compiler.CategorizedProblem)3 JavaSource (org.eclipse.xtext.xbase.compiler.JavaSource)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 InMemoryJavaCompiler (org.eclipse.xtext.xbase.compiler.InMemoryJavaCompiler)2 Map (java.util.Map)1 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 ICompilerRequestor (org.eclipse.jdt.internal.compiler.ICompilerRequestor)1 IErrorHandlingPolicy (org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy)1 CompilationUnit (org.eclipse.jdt.internal.compiler.batch.CompilationUnit)1 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)1 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)1 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)1