Search in sources :

Example 1 with JRCompilationUnit

use of net.sf.jasperreports.engine.design.JRCompilationUnit in project opennms by OpenNMS.

the class CustomJRJdtCompiler method recreateCompileUnit.

protected JRCompilationUnit recreateCompileUnit(JRCompilationUnit compilationUnit, Set<Method> missingMethods) {
    String unitName = compilationUnit.getName();
    JRSourceCompileTask sourceTask = compilationUnit.getCompileTask();
    JRCompilationSourceCode sourceCode = JRClassGenerator.modifySource(sourceTask, missingMethods, compilationUnit.getSourceCode());
    File sourceFile = compilationUnit.getSourceFile();
    File saveSourceDir = sourceFile == null ? null : sourceFile.getParentFile();
    sourceFile = getSourceFile(saveSourceDir, unitName, sourceCode);
    return new JRCompilationUnit(unitName, sourceCode, sourceFile, compilationUnit.getExpressions(), sourceTask);
}
Also used : JRSourceCompileTask(net.sf.jasperreports.engine.design.JRSourceCompileTask) JRCompilationUnit(net.sf.jasperreports.engine.design.JRCompilationUnit) JRCompilationSourceCode(net.sf.jasperreports.engine.design.JRCompilationSourceCode) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) File(java.io.File)

Example 2 with JRCompilationUnit

use of net.sf.jasperreports.engine.design.JRCompilationUnit in project opennms by OpenNMS.

the class CustomJRJdtCompiler method compileUnits.

@Override
protected String compileUnits(final JRCompilationUnit[] units, String classpath, File tempDirFile) {
    final INameEnvironment env = getNameEnvironment(units);
    final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    final CompilerOptions options = getJdtSettings();
    final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
    final CompilerRequestor requestor = getCompilerRequestor(units);
    final Compiler compiler = new Compiler(env, policy, options, requestor, problemFactory);
    do {
        CompilationUnit[] compilationUnits = requestor.processCompilationUnits();
        compiler.compile(compilationUnits);
    } while (requestor.hasMissingMethods());
    requestor.processProblems();
    return requestor.getFormattedProblems();
}
Also used : IErrorHandlingPolicy(org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy) JRCompilationUnit(net.sf.jasperreports.engine.design.JRCompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) JRAbstractJavaCompiler(net.sf.jasperreports.engine.design.JRAbstractJavaCompiler) Compiler(org.eclipse.jdt.internal.compiler.Compiler) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) IProblemFactory(org.eclipse.jdt.internal.compiler.IProblemFactory)

Example 3 with JRCompilationUnit

use of net.sf.jasperreports.engine.design.JRCompilationUnit in project opennms by OpenNMS.

the class CustomJRJdtCompiler method getNameEnvironment.

protected INameEnvironment getNameEnvironment(final JRCompilationUnit[] units) {
    final INameEnvironment env = new INameEnvironment() {

        @Override
        public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
            final StringBuilder result = new StringBuilder();
            String sep = "";
            for (int i = 0; i < compoundTypeName.length; i++) {
                result.append(sep);
                result.append(compoundTypeName[i]);
                sep = ".";
            }
            return findType(result.toString());
        }

        @Override
        public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
            final StringBuilder result = new StringBuilder();
            String sep = "";
            for (int i = 0; i < packageName.length; i++) {
                result.append(sep);
                result.append(packageName[i]);
                sep = ".";
            }
            result.append(sep);
            result.append(typeName);
            return findType(result.toString());
        }

        private int getClassIndex(String className) {
            int classIdx;
            for (classIdx = 0; classIdx < units.length; ++classIdx) {
                if (className.equals(units[classIdx].getName())) {
                    break;
                }
            }
            if (classIdx >= units.length) {
                classIdx = -1;
            }
            return classIdx;
        }

        private NameEnvironmentAnswer findType(String className) {
            try {
                int classIdx = getClassIndex(className);
                if (classIdx >= 0) {
                    ICompilationUnit compilationUnit = new CompilationUnit(units[classIdx].getSourceCode(), className);
                    return new NameEnvironmentAnswer(compilationUnit, null);
                }
                String resourceName = className.replace('.', '/') + ".class";
                InputStream is = getResource(resourceName);
                if (is != null) {
                    try {
                        byte[] classBytes = JRLoader.loadBytes(is);
                        char[] fileName = className.toCharArray();
                        ClassFileReader classFileReader = new ClassFileReader(classBytes, fileName, true);
                        return new NameEnvironmentAnswer(classFileReader, null);
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                        // ignore
                        }
                    }
                }
            } catch (JRException e) {
                LOG.error("Compilation error", e);
            } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) {
                LOG.error("Compilation error", exc);
            } catch (IllegalArgumentException e) {
                throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_NAME_ENVIRONMENT_ANSWER_INSTANCE_ERROR, (Object[]) null, e);
            }
            return null;
        }

        private boolean isPackage(String result) {
            int classIdx = getClassIndex(result);
            if (classIdx >= 0) {
                return false;
            }
            String resourceName = result.replace('.', '/') + ".class";
            boolean isPackage = true;
            InputStream is = getResource(resourceName);
            if (is != null) {
                // 1.5 plugin
                try {
                    isPackage = (is.read() > 0);
                } catch (IOException e) {
                // ignore
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                    // ignore
                    }
                }
            }
            return isPackage;
        }

        @Override
        public boolean isPackage(char[][] parentPackageName, char[] packageName) {
            final StringBuilder result = new StringBuilder();
            String sep = "";
            if (parentPackageName != null) {
                for (int i = 0; i < parentPackageName.length; i++) {
                    result.append(sep);
                    result.append(parentPackageName[i]);
                    sep = ".";
                }
            }
            if (Character.isUpperCase(packageName[0])) {
                if (!isPackage(result.toString())) {
                    return false;
                }
            }
            result.append(sep);
            result.append(packageName);
            return isPackage(result.toString());
        }

        @Override
        public void cleanup() {
        }
    };
    return env;
}
Also used : JRCompilationUnit(net.sf.jasperreports.engine.design.JRCompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) NameEnvironmentAnswer(org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer) JRException(net.sf.jasperreports.engine.JRException) InputStream(java.io.InputStream) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) JRRuntimeException(net.sf.jasperreports.engine.JRRuntimeException) IOException(java.io.IOException) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment)

Aggregations

JRCompilationUnit (net.sf.jasperreports.engine.design.JRCompilationUnit)3 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)2 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JRException (net.sf.jasperreports.engine.JRException)1 JRRuntimeException (net.sf.jasperreports.engine.JRRuntimeException)1 JRAbstractJavaCompiler (net.sf.jasperreports.engine.design.JRAbstractJavaCompiler)1 JRCompilationSourceCode (net.sf.jasperreports.engine.design.JRCompilationSourceCode)1 JRSourceCompileTask (net.sf.jasperreports.engine.design.JRSourceCompileTask)1 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)1 Compiler (org.eclipse.jdt.internal.compiler.Compiler)1 ICompilerRequestor (org.eclipse.jdt.internal.compiler.ICompilerRequestor)1 IErrorHandlingPolicy (org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy)1 IProblemFactory (org.eclipse.jdt.internal.compiler.IProblemFactory)1 ClassFileReader (org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader)1 NameEnvironmentAnswer (org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer)1 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)1 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)1