Java 类net.sf.jasperreports.engine.design.JRCompilationUnit 实例源码

项目:jasperreports    文件:JRBshCompiler.java   
@Override
protected String compileUnits(JRCompilationUnit[] units, String classpath, File tempDirFile) throws JRException
{
    verifyScripts(units);

    for (int i = 0; i < units.length; i++)
    {
        String script = units[i].getSourceCode();
        units[i].setCompileData(script);
    }       

    return null;
}
项目:jasperreports    文件:JRBshCompiler.java   
private void verifyScripts(JRCompilationUnit[] units) throws JRException
{
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    // trick for detecting the Ant class loader
    try
    {
        classLoader.loadClass(JRCalculator.class.getName());
    }
    catch(ClassNotFoundException e)
    {
        classLoader = getClass().getClassLoader();
    }

    ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
    try
    {
        Thread.currentThread().setContextClassLoader(classLoader);

        for (int i = 0; i < units.length; i++)
        {
            String script = units[i].getSourceCode();

            JRBshEvaluator bshEvaluator = new JRBshEvaluator(script);
            bshEvaluator.verify(units[i].getExpressions());
        }
    }
    finally
    {
        Thread.currentThread().setContextClassLoader(oldContextClassLoader);
    }
}
项目:jasperreports    文件:JavaScriptClassCompiler.java   
protected void compileScripts(JRCompilationUnit unit, CompilerEnvirons compilerEnv, 
        CompileSources compileSources, JavaScriptCompiledData compiledData)
{
    List<String> scripts = compileSources.getScripts();
    int scriptIndex = 0;
    for (String scriptSource : scripts)
    {
        String scriptClassName = unit.getName() + "_" + scriptIndex;

        if (log.isTraceEnabled())
        {
            log.trace("compiling script with name " + scriptClassName
                    + "\n" + scriptSource);
        }

        ClassCompiler compiler = new ClassCompiler(compilerEnv);
        // this should not fail since we've already separately compiled the default expression
        Object[] compilationResult = compiler.compileToClassFiles(scriptSource, unit.getName(), 0, scriptClassName);
        if (compilationResult.length != 2)
        {
            throw 
                new JRRuntimeException(
                    EXCEPTION_MESSAGE_KEY_UNEXPECTED_CLASSES_LENGTH,
                    new Object[]{compilationResult.length});
        }
        if (!scriptClassName.equals(compilationResult[0]))
        {
            throw 
                new JRRuntimeException(
                    EXCEPTION_MESSAGE_KEY_UNEXPECTED_CLASS_NAME,
                    new Object[]{compilationResult[0], scriptClassName});
        }

        byte[] compiledClass = (byte[]) compilationResult[1];
        compiledData.addClass(scriptClassName, compiledClass);

        ++scriptIndex;
    }
}
项目:PDFReporter-Studio    文件:JEvalCompiler.java   
@Override
protected String compileUnits(JRCompilationUnit[] units, String classpath,
        File tempDirFile) throws JRException {
    for (JRCompilationUnit unit : units) {
        // just set the compilation unit name as compile data. Later we can retrieve the Evaluator by this name
        unit.setCompileData(new DummyCompileData()); 
    }
    return null; // no error
}
项目:PDFReporter-Studio    文件:JRJdtCompiler.java   
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);
}
项目:PDFReporter-Studio    文件:JRJdtCompiler.java   
public CompilerRequestor(final JasperReportsContext jasperReportsContext, final JRJdtCompiler compiler, final JRCompilationUnit[] units) {
    this.jasperReportsContext = jasperReportsContext;
    this.compiler = compiler;
    this.units = units;
    this.unitResults = new CompilationUnitResult[units.length];

    reset();
}
项目:ireport-fork    文件:ExtendedJRJdtCompiler.java   
public void acceptResult(CompilationResult result)
{
    super.acceptResult(result);
    if (result.hasErrors())
    {
        String className = String.valueOf(result.getCompilationUnit().getMainTypeName());

        JRCompilationUnit unit = null;
        for (int classIdx = 0; classIdx < units.length; ++classIdx)
        {
            if (className.equals(units[classIdx].getName()))
            {
                unit = units[classIdx];
                break;
            }
        }

        IProblem[] errors = result.getErrors();
        for (int i = 0; i < errors.length; i++)
        {
            IProblem problem = errors[i];
            int line = problem.getSourceLineNumber();
            JRCompilationSourceCode sourceCode = unit.getCompilationSource();
            JRExpression expression = sourceCode.getExpressionAtLine(line);
            if (expression == null)
            {
                getErrorHandler().addMarker( problem, null);
            }
            else if (addExpressionError(expression, problem))
            {
                SourceLocation location = getDigester().getLocation(expression);
                getErrorHandler().addMarker(problem, expression, location);
            }
        }
    }
}
项目:jasperreports    文件:JavaScriptCompiler.java   
@Override
protected String compileUnits(JRCompilationUnit[] units, String classpath,
        File tempDirFile) throws JRException
{
    Context context = ContextFactory.getGlobal().enterContext();
    try
    {
        Errors errors = new Errors();
        for (int i = 0; i < units.length; i++)
        {
            JRCompilationUnit unit = units[i];
            JavaScriptCompileData compileData = new JavaScriptCompileData();
            for (Iterator<JRExpression> it = unit.getExpressions().iterator(); it.hasNext();)
            {
                JRExpression expr = it.next();
                int id = unit.getCompileTask().getExpressionId(expr).intValue();

                ScriptExpressionVisitor defaultVisitor = defaultExpressionCreator();
                JRExpressionUtil.visitChunks(expr, defaultVisitor);
                String defaultExpression = defaultVisitor.getScript();

                //compile the default expression to catch syntax errors
                try
                {
                    context.compileString(defaultExpression, "expression", 0, null);
                }
                catch (EvaluatorException e)
                {
                    errors.addError(e);
                }

                if (!errors.hasErrors())
                {
                    ScriptExpressionVisitor oldVisitor = oldExpressionCreator();
                    ScriptExpressionVisitor estimatedVisitor = estimatedExpressionCreator();
                    JRExpressionUtil.visitChunks(expr, new CompositeExpressionChunkVisitor(oldVisitor, estimatedVisitor));

                    compileData.addExpression(id, defaultExpression, estimatedVisitor.getScript(), oldVisitor.getScript());
                }
            }

            if (!errors.hasErrors())
            {
                unit.setCompileData(compileData);
            }
        }

        return errors.errorMessage();
    }
    finally
    {
        Context.exit();
    }
}
项目:jasperreports    文件:JavaScriptClassCompiler.java   
@Override
protected String compileUnits(JRCompilationUnit[] units, String classpath,
        File tempDirFile) throws JRException
{
    Context context = ContextFactory.getGlobal().enterContext();
    try
    {
        JRPropertiesUtil properties = JRPropertiesUtil.getInstance(jasperReportsContext);
        int expressionsPerScript = properties.getIntegerProperty(PROPERTY_EXPRESSIONS_PER_SCRIPT);
        int scriptMaxLength = properties.getIntegerProperty(PROPERTY_SCRIPT_MAX_SIZE);

        int optimizationLevel = properties.getIntegerProperty(PROPERTY_OPTIMIZATION_LEVEL);
        context.setOptimizationLevel(optimizationLevel);
        context.getWrapFactory().setJavaPrimitiveWrap(false);

        Errors errors = new Errors();

        CompilerEnvirons compilerEnv = new CompilerEnvirons();
        compilerEnv.initFromContext(context);

        // we're using the context to compile the expressions in interpreted mode to catch syntax errors 
        context.setOptimizationLevel(-1);

        for (int i = 0; i < units.length; i++)
        {
            JRCompilationUnit unit = units[i];
            CompileSources compileSources = new CompileSources(expressionsPerScript, scriptMaxLength);
            JavaScriptCompiledData compiledData = new JavaScriptCompiledData();

            for (Iterator<JRExpression> it = unit.getExpressions().iterator(); it.hasNext();)
            {
                JRExpression expr = it.next();
                int id = unit.getCompileTask().getExpressionId(expr).intValue();

                ScriptExpressionVisitor defaultVisitor = defaultExpressionCreator();
                JRExpressionUtil.visitChunks(expr, defaultVisitor);
                String defaultExpression = defaultVisitor.getScript();

                //compile the default expression to catch syntax errors
                try
                {
                    context.compileString(defaultExpression, "expression", 0, null);
                }
                catch (EvaluatorException e)
                {
                    errors.addError(e);
                }

                if (!errors.hasErrors())
                {
                    ScriptExpressionVisitor oldVisitor = oldExpressionCreator();
                    ScriptExpressionVisitor estimatedVisitor = estimatedExpressionCreator();
                    JRExpressionUtil.visitChunks(expr, new CompositeExpressionChunkVisitor(oldVisitor, estimatedVisitor));

                    int defaultExpressionIdx = compileSources.addExpression(defaultExpression);
                    int oldExpressionIdx = compileSources.addExpression(oldVisitor.getScript());
                    int estimatedExpressionIdx = compileSources.addExpression(estimatedVisitor.getScript());

                    compiledData.addExpression(id, defaultExpressionIdx, oldExpressionIdx, estimatedExpressionIdx);
                }
            }

            if (!errors.hasErrors())
            {
                compileScripts(unit, compilerEnv, compileSources, compiledData);
                unit.setCompileData(compiledData);
            }
        }

        return errors.errorMessage();
    }
    finally
    {
        Context.exit();
    }
}
项目:PDFReporter-Studio    文件:JRJdtCompiler.java   
/**
 *
 */
protected String compileUnits(final JRCompilationUnit[] units, String classpath, File tempDirFile) {
    final INameEnvironment env = getNameEnvironment(units);

    final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());

    final CompilerRequestor requestor = getCompilerRequestor(units);

    final Compiler compiler = new Compiler(env, policy, getJdtSettings(), requestor, problemFactory);

    do {
        CompilationUnit[] compilationUnits = requestor.processCompilationUnits();

        compiler.compile(compilationUnits);
    } while (requestor.hasMissingMethods());

    requestor.processProblems();

    return requestor.getFormattedProblems();
}
项目:PDFReporter-Studio    文件:JRJdtCompiler.java   
/**
 * 
 */
protected INameEnvironment getNameEnvironment(final JRCompilationUnit[] units) {
    return new NameEnvironement(this, units);
}
项目:PDFReporter-Studio    文件:JRJdtCompiler.java   
/**
 *
 */
protected CompilerRequestor getCompilerRequestor(final JRCompilationUnit[] units) {
    return new CompilerRequestor(jasperReportsContext, this, units);
}
项目:PDFReporter-Studio    文件:NameEnvironement.java   
public NameEnvironement(JRJdtCompiler jrJdtCompiler, JRCompilationUnit[] units) {
    this.jrJdtCompiler = jrJdtCompiler;
    this.units = units;

}
项目:PDFReporter-Studio    文件:JasperReportCompiler.java   
protected LocalCompilerRequestor(final JasperReportsContext jasperReportsContext, final JRJdtCompiler compiler, final JRCompilationUnit[] units) {
    super(jasperReportsContext, compiler, units);
}
项目:ireport-fork    文件:ExtendedJRJdtCompiler.java   
@Override
protected CompilerRequestor getCompilerRequestor(JRCompilationUnit[] units)
{
        return new CompilerRequestor(jasperReportsContext, this, units);
}
项目:ireport-fork    文件:ExtendedJRJdtCompiler.java   
protected CompilerRequestor(JasperReportsContext jasperReportsContext, final JRJdtCompiler compiler, final JRCompilationUnit[] units)
{
    super(jasperReportsContext, compiler, units);
    this.units = units;
}