Search in sources :

Example 6 with NameEnvironmentAnswer

use of org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer 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) {
            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) {
            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) {
            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

NameEnvironmentAnswer (org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer)6 File (java.io.File)4 ProjectItemModifiedEvent (org.eclipse.che.api.project.server.notification.ProjectItemModifiedEvent)3 ResourceChangedEvent (org.eclipse.che.jdt.core.resources.ResourceChangedEvent)3 ClassFileReader (org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader)3 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)3 Test (org.junit.Test)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)2 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1