/*
 * Copyright 2015 Google Inc. All rights reserved.
 *
 * 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 com.google.errorprone.scanner;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.CodeTransformer;
import com.google.errorprone.DescriptionListener;
import com.google.errorprone.ErrorProneOptions;
import com.google.errorprone.VisitorState;

import com.sun.source.util.TreePath;
import com.sun.tools.javac.util.Context;

import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * Adapter from an {@link ErrorProneScanner} to a {@link CodeTransformer}.
 */
@AutoValue
public abstract class ErrorProneScannerTransformer implements CodeTransformer {
  static final Context.Key<Map<String, SeverityLevel>> SEVERITY_MAP_KEY = new Context.Key<>();

  public static ErrorProneScannerTransformer create(ErrorProneScanner scanner) {
    return new AutoValue_ErrorProneScannerTransformer(scanner);
  }

  abstract ErrorProneScanner scanner();

  @Override
  public void apply(TreePath tree, Context context, DescriptionListener listener) {
    scanner().scan(tree, createVisitorState(context, listener).withPath(tree));
  }

  @Override
  public ImmutableClassToInstanceMap<Annotation> annotations() {
    return ImmutableClassToInstanceMap.<Annotation>builder().build();
  }

  /**
   * Create a VisitorState object from a compilation unit.
   */
  private VisitorState createVisitorState(Context context, DescriptionListener listener) {
    return new VisitorState(
        context, listener, context.get(SEVERITY_MAP_KEY), context.get(ErrorProneOptions.class));
  }
}

@@ -754,63 +754,8 @@
 ap;%0A
-import com.google.errorprone.BugPattern.SeverityLevel;%0A
 impo
@@ -1060,30 +1060,8 @@
 ion;
-%0Aimport java.util.Map;
 %0A%0A/*
@@ -1235,103 +1235,8 @@
 er %7B
-%0A  static final Context.Key%3CMap%3CString, SeverityLevel%3E%3E SEVERITY_MAP_KEY = new Context.Key%3C%3E();
 %0A%0A  
@@ -1285,26 +1285,16 @@
  create(
-ErrorProne
 Scanner 
@@ -1384,26 +1384,16 @@
 bstract 
-ErrorProne
 Scanner 
@@ -1959,36 +1959,30 @@
 er, 
-context.get(SEVERITY_MAP_KEY
+scanner().severityMap(
 ), c

Rewrite ErrorProneScannerTransformer to match the usage in ErrorProneAnalyzer better. The severityMap comes from the scanner itself, not passed in via the Context, and an arbitrary Scanner, not just an ErrorProneScanner, is accepted.