/* @test
 * @compile GetScopeCrashes.java
 * @compile -processor GetScopeCrashes GetScopeCrashes.java
 */

import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;

@SupportedAnnotationTypes("*")
public class GetScopeCrashes extends AbstractProcessor {

    private boolean installed;

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        if (!installed) {
            installed = true;
            JavacTask task = JavacTask.instance(processingEnv);
            task.addTaskListener(new TaskListener() {
                @Override
                public void finished(TaskEvent e) {
                    if (e.getKind() == TaskEvent.Kind.ANALYZE) {
                        Trees trees = Trees.instance(task);
                        TreePath path = trees.getPath(e.getTypeElement());
                        
                        new TreePathScanner<Void, Void>() {
                            @Override
                            public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
                                trees.getScope(getCurrentPath());
                                return super.visitMethodInvocation(node, p);
                            }
                        }.scan(path, null);
                    }
                }
            });
        }
        return false;
    }
    
}

class SwitchScope {
    public void t(int i) {
        switch (i) {
            case 0:
                String.valueOf(i); //when getScope is called here, the variable below remains unattributed, which then crashes Attr.addVars:
                int j;
                break;
        }
    }
}