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);
}
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();
}
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;
}
Aggregations