<commit_before>/*
 * Copyright 2013 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.bugpatterns;

import static com.google.errorprone.BugPattern.Category.GUICE;
import static com.google.errorprone.BugPattern.MaturityLevel.MATURE;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.constructor;
import static com.google.errorprone.matchers.Matchers.hasAnnotation;
import static com.google.errorprone.matchers.Matchers.methodHasParameters;
import static com.google.errorprone.matchers.MultiMatcher.MatchType.ANY;

import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.DescribingMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.matchers.MultiMatcher;

import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.VariableTree;

/**
 * This checker matches iff *both* of the following conditions are true:
 * 1) The class is assisted:
 *   a) If there is a constructor that is annotated with @Inject and that constructor has at least
 *      one parameter that is annotated with @Assisted.
 *   b) If there is no @Inject constructor and at least one constructor is annotated with
 *      @AssistedInject.
 * 2) There is an annotation on the class, and the annotation is itself annotated with
 *    @ScopeAnnotation.
 *
 * @author <EMAIL> (<NAME>)
 */
@BugPattern(name = "GuiceAssistedInjectScoping",
    summary = "Scope annotation on implementation class of AssistedInject factory is not allowed",
    explanation =
        "Classes that implement AssistedInject factories may not be annotated with scope " +
        "annotations, such as @Singleton.  This will cause a Guice error at runtime.",
    category = GUICE, severity = ERROR, maturity = MATURE)
public class GuiceAssistedInjectScoping extends DescribingMatcher<ClassTree> {

  private static final String SCOPE_ANNOTATION_STRING = "com.google.inject.ScopeAnnotation";
  private static final String ASSISTED_ANNOTATION_STRING =
      "com.google.inject.assistedinject.Assisted";
  private static final String INJECT_ANNOTATION_STRING = "com.google.inject.Inject";
  private static final String ASSISTED_INJECT_ANNOTATION_STRING =
      "com.google.inject.assistedinject.AssistedInject";

  /**
   * Matches classes that have an annotation that itself is annotated with @ScopeAnnotation.
   */
  private MultiMatcher<ClassTree, AnnotationTree> classAnnotationMatcher =
     annotations(ANY, hasAnnotation(SCOPE_ANNOTATION_STRING, AnnotationTree.class));

  /**
   * Matches if:
   * 1) If there is a constructor that is annotated with @Inject and that constructor has at least
   *    one parameter that is annotated with @Assisted.
   * 2) If there is no @Inject constructor and at least one constructor is annotated with<commit_msg>better summary message for GuiceAssistedInjectScoping<commit_after>/*
 * Copyright 2013 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.bugpatterns;

import static com.google.errorprone.BugPattern.Category.GUICE;
import static com.google.errorprone.BugPattern.MaturityLevel.MATURE;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.constructor;
import static com.google.errorprone.matchers.Matchers.hasAnnotation;
import static com.google.errorprone.matchers.Matchers.methodHasParameters;
import static com.google.errorprone.matchers.MultiMatcher.MatchType.ANY;

import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.DescribingMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.matchers.MultiMatcher;

import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.VariableTree;

/**
 * This checker matches iff *both* of the following conditions are true:
 * 1) The class is assisted:
 *   a) If there is a constructor that is annotated with @Inject and that constructor has at least
 *      one parameter that is annotated with @Assisted.
 *   b) If there is no @Inject constructor and at least one constructor is annotated with
 *      @AssistedInject.
 * 2) There is an annotation on the class, and the annotation is itself annotated with
 *    @ScopeAnnotation.
 *
 * @author <EMAIL> (<NAME>)
 */
@BugPattern(name = "GuiceAssistedInjectScoping",
    summary = "Scope annotation on implementation class of AssistedInject factory is not allowed",
    explanation =
        "Classes that AssistedInject factories create may not be annotated with scope " +
        "annotations, such as @Singleton.  This will cause a Guice error at runtime.",
    category = GUICE, severity = ERROR, maturity = MATURE)
public class GuiceAssistedInjectScoping extends DescribingMatcher<ClassTree> {

  private static final String SCOPE_ANNOTATION_STRING = "com.google.inject.ScopeAnnotation";
  private static final String ASSISTED_ANNOTATION_STRING =
      "com.google.inject.assistedinject.Assisted";
  private static final String INJECT_ANNOTATION_STRING = "com.google.inject.Inject";
  private static final String ASSISTED_INJECT_ANNOTATION_STRING =
      "com.google.inject.assistedinject.AssistedInject";

  /**
   * Matches classes that have an annotation that itself is annotated with @ScopeAnnotation.
   */
  private MultiMatcher<ClassTree, AnnotationTree> classAnnotationMatcher =
     annotations(ANY, hasAnnotation(SCOPE_ANNOTATION_STRING, AnnotationTree.class));

  /**
   * Matches if:
   * 1) If there is a constructor that is annotated with @Inject and that constructor has at least
   *    one parameter that is annotated with @Assisted.
   * 2) If there is no @Inject constructor and at least one constructor is annotated with
{'fraction_non_alphanumeric': 0.07709690283131729, 'fraction_numerical': 0.0031902166688820947, 'mean_word_length': 4.687074829931973, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544942', 'n_tokens_mistral': 2124, 'n_tokens_neox': 2039, 'n_words': 691}