001/*
002 * Copyright 2016 The Error Prone Authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.errorprone.annotations;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024import java.util.Collection;
025
026/**
027 * Declares that a parameter to a method must be "compatible with" one of the type parameters in the
028 * method's enclosing class, or on the method itself. "Compatible with" means that there can exist a
029 * "reference casting conversion" from one type to the other (JLS 5.5.1).
030 *
031 * <p>For example, {@link Collection#contains} would be annotated as follows:
032 *
033 * <pre>{@code
034 * interface Collection<E> {
035 *   boolean contains(@CompatibleWith("E") Object o);
036 * }
037 * }</pre>
038 *
039 * <p>To indicate that invocations of {@link Collection#contains} must be passed an argument whose
040 * type is compatible with the generic type argument of the Collection instance:
041 *
042 * <pre>{@code
043 * Collection<String> stringCollection = ...;
044 * boolean shouldBeFalse = stringCollection.contains(42); // BUG! int isn't compatible with String
045 * }</pre>
046 *
047 * <p>Note: currently, this annotation can't be used if the method overrides another method that has
048 * {@code @CompatibleWith} already present.
049 */
050@Documented
051@Retention(RetentionPolicy.SOURCE)
052@Target(ElementType.PARAMETER)
053public @interface CompatibleWith {
054  String value();
055}