package net.bytebuddy.description.type;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.annotation.AnnotationSource;
import net.bytebuddy.jar.asm.Opcodes;
/**
* A package description represents a Java package.
*/
public interface PackageDescription extends NamedElement.WithRuntimeName, AnnotationSource {
/**
* The name of a Java class representing a package description.
*/
StringPACKAGE_CLASS_NAME = "package-info";
/**
* The modifiers of a Java class representing a package description.
*/
int PACKAGE_MODIFIERS = Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_SYNTHETIC;
/**
* A named constant for an undefined package what applies for primitive and array types.
*/
PackageDescriptionUNDEFINED = null;
/**
* Checks if this package contains the provided type.
*
* @param typeDescription The type to examine.
* @return {@code true} if the given type contains the provided type.
*/
boolean contains(TypeDescriptiontypeDescription);
/**
* An abstract base implementation of a package description.
*/
abstract class AbstractBase implements PackageDescription {
@Override
public StringgetInternalName() {
return getName().replace('.', '/');
}
@Override
public StringgetActualName() {
return getName();
}
@Override
public boolean contains(TypeDescriptiontypeDescription) {
return this.equals(typeDescription.getPackage());
}
@Override
public int hashCode() {
return getName().hashCode();
}
@Override
public boolean equals(Objectother) {
return this == other || other instanceof PackageDescription && getName().equals(((PackageDescription) other).getName());
}
@Override
public StringtoString() {
return "package " + getName();
}
}
/**
* A simple implementation of a package without annotations.
*/
class Simple extends AbstractBase {
/**
* The name of the package.
*/
private final Stringname;
/**
* Creates a new simple package.
*
* @param name The name of the package.
*/
public Simple(Stringname) {
this.name = name;
}
@Override
public AnnotationListgetDeclaredAnnotations() {
return new AnnotationList.Empty();
}
@Override
public StringgetName() {
return name;
}
}
/**
* Represents a loaded {@link java.lang.Package} wrapped as a
* {@link PackageDescription}.
*/
class ForLoadedPackage extends AbstractBase {
/**
* The represented package.
*/
private final PackageaPackage;
/**
* Creates a new loaded package representation.
*
* @param aPackage The represented package.
*/
public ForLoadedPackage(PackageaPackage) {
this.aPackage = aPackage;
}
@Override
public AnnotationListgetDeclaredAnnotations() {
return new AnnotationList.ForLoadedAnnotations(aPackage.getDeclaredAnnotations());
}
@Override
public StringgetName() {
return aPackage.getName();
}
}
}