All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.groovy.transform.stc.SharedVariableCollector Maven / Gradle / Ivy

/*
 * Copyright 2003-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.groovy.transform.stc;

import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.control.SourceUnit;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * A visitor which collects the list of variable expressions which are closure shared.
 */
public class SharedVariableCollector extends ClassCodeVisitorSupport {
    private final SourceUnit unit;
    private final Set closureSharedExpressions = new LinkedHashSet();
    private boolean visited = false;

    public SharedVariableCollector(final SourceUnit unit) {
        this.unit = unit;
    }

    @Override
    protected SourceUnit getSourceUnit() {
        return unit;
    }

    public Set getClosureSharedExpressions() {
        return Collections.unmodifiableSet(closureSharedExpressions);
    }

    @Override
    public void visitVariableExpression(final VariableExpression expression) {
        if (visited) {
            // we should not visit embedded closures recursively
            return;
        }
        visited = true;
        if (expression.isClosureSharedVariable()) closureSharedExpressions.add(expression);
        super.visitVariableExpression(expression);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy