Java Code Examples for org.codehaus.groovy.control.CompilationUnit#addPhaseOperation()

The following examples show how to use org.codehaus.groovy.control.CompilationUnit#addPhaseOperation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DependencyTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    cache = new StringSetMap();
    cu = new CompilationUnit();
    cu.addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
        DependencyTracker dt = new DependencyTracker(source, cache);
        dt.visitClass(classNode);
    }, Phases.CLASS_GENERATION);
}
 
Example 2
Source File: AbstractScriptTransformer.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void register(CompilationUnit compilationUnit) {
    compilationUnit.addPhaseOperation(this, getPhase());
}
 
Example 3
Source File: AbstractScriptTransformer.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void register(CompilationUnit compilationUnit) {
    compilationUnit.addPhaseOperation(this, getPhase());
}
 
Example 4
Source File: JSR223SecurityTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
    CompilationUnit unit = super.createCompilationUnit(config, source);
    unit.addPhaseOperation(new CustomPrimaryClassNodeOperation(), Phases.SEMANTIC_ANALYSIS);
    return unit;
}
 
Example 5
Source File: ASTTransformationVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static void addPhaseOperationsForGlobalTransforms(CompilationUnit compilationUnit,
        Map<String, URL> transformNames, boolean isFirstScan) {
    GroovyClassLoader transformLoader = compilationUnit.getTransformLoader();
    for (Map.Entry<String, URL> entry : transformNames.entrySet()) {
        try {
            Class<?> gTransClass = transformLoader.loadClass(entry.getKey(), false, true, false);
            GroovyASTTransformation transformAnnotation = gTransClass.getAnnotation(GroovyASTTransformation.class);
            if (transformAnnotation == null) {
                compilationUnit.getErrorCollector().addWarning(new WarningMessage(
                    WarningMessage.POSSIBLE_ERRORS,
                    "Transform Class " + entry.getKey() + " is specified as a global transform in " + entry.getValue().toExternalForm()
                    + " but it is not annotated by " + GroovyASTTransformation.class.getName()
                    + " the global transform associated with it may fail and cause the compilation to fail.",
                    null,
                    null));
                continue;
            }
            if (ASTTransformation.class.isAssignableFrom(gTransClass)) {
                ASTTransformation instance = (ASTTransformation) gTransClass.getDeclaredConstructor().newInstance();
                if (instance instanceof CompilationUnitAware) {
                    ((CompilationUnitAware) instance).setCompilationUnit(compilationUnit);
                }
                CompilationUnit.ISourceUnitOperation suOp = source -> instance.visit(new ASTNode[]{source.getAST()}, source);
                if (isFirstScan) {
                    compilationUnit.addPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber());
                } else {
                    compilationUnit.addNewPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber());
                }
            } else {
                compilationUnit.getErrorCollector().addError(new SimpleMessage(
                    "Transform Class " + entry.getKey() + " specified at "
                    + entry.getValue().toExternalForm() + " is not an ASTTransformation.", null));
            }
        } catch (Exception e) {
            Throwable effectiveException = e instanceof InvocationTargetException ? e.getCause() : e;
            compilationUnit.getErrorCollector().addError(new SimpleMessage(
                "Could not instantiate global transform class " + entry.getKey() + " specified at "
                + entry.getValue().toExternalForm() + "  because of exception " + effectiveException.toString(), null));
        }
    }
}
 
Example 6
Source File: GroovyScriptEngine.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration configuration, CodeSource source) {
    CompilationUnit cu = super.createCompilationUnit(configuration, source);
    LocalData local = getLocalData().get();
    local.cu = cu;
    final StringSetMap cache = local.dependencyCache;
    final Map<String, String> precompiledEntries = local.precompiledEntries;

    // "." is used to transfer compilation dependencies, which will be
    // recollected later during compilation
    for (String depSourcePath : cache.get(".")) {
        try {
            cache.get(depSourcePath);
            cu.addSource(getResourceConnection(depSourcePath).getURL());
        } catch (ResourceException e) {
            /* ignore */
        }
    }

    // remove all old entries including the "." entry
    cache.clear();

    cu.addPhaseOperation((final SourceUnit sourceUnit, final GeneratorContext context, final ClassNode classNode) -> {
       // GROOVY-4013: If it is an inner class, tracking its dependencies doesn't really
       // serve any purpose and also interferes with the caching done to track dependencies
       if (classNode.getOuterClass() != null) return;
       DependencyTracker dt = new DependencyTracker(sourceUnit, cache, precompiledEntries);
       dt.visitClass(classNode);
    }, Phases.CLASS_GENERATION);

    cu.setClassNodeResolver(new ClassNodeResolver() {
        @Override
        public LookupResult findClassNode(String origName, CompilationUnit compilationUnit) {
            CompilerConfiguration cc = compilationUnit.getConfiguration();
            String name = origName.replace('.', '/');
            for (String ext : cc.getScriptExtensions()) {
                try {
                    String finalName = name + "." + ext;
                    URLConnection conn = rc.getResourceConnection(finalName);
                    URL url = conn.getURL();
                    String path = url.toExternalForm();
                    ScriptCacheEntry entry = scriptCache.get(path);
                    Class clazz = null;
                    if (entry != null) clazz = entry.scriptClass;
                    if (GroovyScriptEngine.this.isSourceNewer(entry)) {
                        try {
                            SourceUnit su = compilationUnit.addSource(url);
                            return new LookupResult(su, null);
                        } finally {
                            forceClose(conn);
                        }
                    } else {
                        precompiledEntries.put(origName, path);
                    }
                    if (clazz != null) {
                        ClassNode cn = ClassHelper.make(clazz);
                        return new LookupResult(null, cn);
                    }
                } catch (ResourceException re) {
                    // skip
                }
            }
            return super.findClassNode(origName, compilationUnit);
        }
    });

    return cu;
}
 
Example 7
Source File: AbstractScriptTransformer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void register(CompilationUnit compilationUnit) {
    compilationUnit.addPhaseOperation(this, getPhase());
}
 
Example 8
Source File: AbstractScriptTransformer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void register(CompilationUnit compilationUnit) {
    compilationUnit.addPhaseOperation(this, getPhase());
}