/* * Copyright (C) 2015 The Dagger 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 dagger.internal.codegen.langmodel; import static androidx.room.compiler.processing.XElementKt.isField; import static androidx.room.compiler.processing.XElementKt.isTypeElement; import static androidx.room.compiler.processing.XTypeKt.isArray; import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; import static dagger.internal.codegen.xprocessing.XElements.isExecutable; import static dagger.internal.codegen.xprocessing.XElements.isPackage; import static dagger.internal.codegen.xprocessing.XElements.isPrivate; import static dagger.internal.codegen.xprocessing.XElements.isPublic; import static dagger.internal.codegen.xprocessing.XTypeElements.isNested; import static dagger.internal.codegen.xprocessing.XTypes.asArray; import static dagger.internal.codegen.xprocessing.XTypes.getEnclosingType; import static dagger.internal.codegen.xprocessing.XTypes.isDeclared; import static dagger.internal.codegen.xprocessing.XTypes.isNoType; import static dagger.internal.codegen.xprocessing.XTypes.isNullType; import static dagger.internal.codegen.xprocessing.XTypes.isPrimitive; import static dagger.internal.codegen.xprocessing.XTypes.isTypeVariable; import static dagger.internal.codegen.xprocessing.XTypes.isWildcard; import androidx.room.compiler.processing.XElement; import androidx.room.compiler.processing.XProcessingEnv; import androidx.room.compiler.processing.XType; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.TypeName; import java.util.Optional; /** * Utility methods for determining whether a {@link XType} or an {@link XElement} is accessible * given the rules outlined in section 6.6 of the * Java Language Specification. * *
This class only provides an approximation for accessibility. It does not always yield the same
* result as the compiler, but will always err on the side of declaring something inaccessible. This
* ensures that using this class will never result in generating code that will not compile.
*/
public final class Accessibility {
/** Returns true if the given type can be referenced from any package. */
public static boolean isTypePubliclyAccessible(XType type) {
return isTypeAccessibleFrom(type, Optional.empty());
}
/** Returns true if the given type can be referenced from code in the given package. */
public static boolean isTypeAccessibleFrom(XType type, String packageName) {
return isTypeAccessibleFrom(type, Optional.of(packageName));
}
private static boolean isTypeAccessibleFrom(XType type, Optional
*
*/
public static TypeName accessibleTypeName(
XType type, ClassName requestingClass, XProcessingEnv processingEnv) {
if (isTypeAccessibleFrom(type, requestingClass.packageName())) {
return type.getTypeName();
} else if (isDeclared(type) && isRawTypeAccessible(type, requestingClass.packageName())) {
return type.getRawType().getTypeName();
} else {
return TypeName.OBJECT;
}
}
private Accessibility() {}
}