JAVAJava中的lang.Package类

Java2添加了一个名为Package的类,该类封装了与包关联的版本数据。软件包版本信息变得越来越重要,因为软件包越来越多,而且java程序可能需要知道软件包的可用版本。 这个版本信息由加载类的ClassLoader实例检索并提供。通常,它存储在与类一起分发的清单中。它扩展了类对象并实现了注释删除。

null

方法:

  1. getAnnotation(类annotationClass): 返回指定类型的该元素的注释(如果存在此类注释),否则返回null。
    Syntax: public  A getAnnotation(Class annotationClass)
    Returns: this element's annotation for the specified 
    annotation type if present on this element, else null.
    Exception: NullPointerException - if the given annotation class is null.
    

    // Java code illustrating getAnnotation() method
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.reflect.Method;
    // declare a annotation type
    @Retention (RetentionPolicy.RUNTIME)
    @interface Demo
    {
    String str();
    int val();
    }
    public class PackageDemo
    {
    // setting values for the annotation
    @Demo (str = " Gfg Demo Annotation" , val = 100 )
    // a method to call in the main
    public static void gfg() throws NoSuchMethodException
    {
    PackageDemo ob = new PackageDemo();
    Class c = ob.getClass();
    // get the method example
    Method m = c.getMethod( "gfg" );
    // get the annotation for class Demo
    Demo annotation = m.getAnnotation(Demo. class );
    // checking the annotation
    System.out.println(annotation.str() + " " + annotation.val());
    }
    public static void main(String args[]) throws Exception
    {
    gfg();
    }
    }

    
    

    输出:

    Gfg Demo Annotation 100
    
  2. 注释[]getAnnotations(): 返回此元素上存在的所有批注。(如果此元素没有注释,则返回长度为零的数组。)此方法的调用方可以自由修改返回的数组;它对返回给其他调用者的数组没有影响。
    Syntax: public Annotation[] getDeclaredAnnotations().
    Returns: All annotations directly present on this element.
    Exception: NA.
    

    // Java code illustrating getAnnotation() method
    import java.lang.annotation.Annotation;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.reflect.Method;
    // declare a annotation type
    @Retention (RetentionPolicy.RUNTIME)
    @interface Demo
    {
    String str();
    int val();
    }
    public class PackageDemo
    {
    // setting values for the annotation
    @Demo (str = " Gfg Demo Annotation" , val = 100 )
    // a method to call in the main
    public static void gfg() throws NoSuchMethodException
    {
    PackageDemo ob = new PackageDemo();
    Class c = ob.getClass();
    // get the method example
    Method m = c.getMethod( "gfg" );
    // get the annotation for class Demo
    Demo annotation = m.getAnnotation(Demo. class );
    // checking the annotation
    System.out.println(annotation.str() + " " + annotation.val());
    Annotation[] gfg_ann = m.getAnnotations();
    for ( int i = 0 ; i < gfg_ann.length; i++)
    {
    System.out.println(gfg_ann[i]);
    }
    }
    public static void main(String args[]) throws Exception
    {
    gfg();
    }
    }

    
    

    输出:

    Gfg Demo Annotation 100
    @Demo(str= Gfg Demo Annotation, val=100)
    
  3. 注释[]getDeclaredAnnotations(): 返回此元素上直接存在的所有注释。与此接口中的其他方法不同,此方法忽略继承的注释。(如果此元素上没有直接的注释,则返回长度为零的数组。)此方法的调用方可以自由修改返回的数组;它对返回给其他调用者的数组没有影响。
    Syntax: public Annotation[] getDeclaredAnnotations().
    Returns: All annotations directly present on this element.
    Exception: NA.
    

    // java code illustrating getDeclaredAnnotation() method
    import java.lang.annotation.Annotation;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.reflect.Method;
    // declare a annotation type
    @Retention (RetentionPolicy.RUNTIME)
    @interface Demo
    {
    String str();
    int val();
    }
    public class PackageDemo
    {
    // setting values for the annotation
    @Demo (str = " Gfg Demo Annotation" , val = 100 )
    // a method to call in the main
    public static void gfg() throws NoSuchMethodException
    {
    PackageDemo ob = new PackageDemo();
    Class c = ob.getClass();
    // get the method example
    Method m = c.getMethod( "gfg" );
    // get the annotation for class Demo
    Demo annotation = m.getAnnotation(Demo. class );
    // checking the annotation
    System.out.println(annotation.str() + " " + annotation.val());
    Annotation[] gfg_ann = m.getDeclaredAnnotations();
    for ( int i = 0 ; i < gfg_ann.length; i++)
    {
    System.out.println(gfg_ann[i]);
    }
    }
    public static void main(String args[]) throws Exception
    {
    gfg();
    }
    }

    
    

    输出:

     Gfg Demo Annotation 100
    @Demo(str= Gfg Demo Annotation, val=100)
    
  4. 字符串getImplementationTitle(): 返回此包的标题。
    Syntax: public String getImplementationTitle()
    Returns: the title of the implementation, null is returned if it is not known.
    Exception: NA
    
  5. 字符串getImplementationVersion(): 返回此实现的版本。它由该实现的供应商分配的任何字符串组成,没有Java运行时指定或预期的任何特定语法。可以将其与此供应商用于此软件包实现的其他软件包版本字符串进行比较,以确定是否相等。
    Syntax: public String getImplementationVersion()
    Returns: the version of the implementation, null is returned if it is not known.
    Exception: NA
    
  6. 字符串getImplementationVendor(): 返回提供此实现的组织、供应商或公司的名称。
    Syntax: public String getImplementationVendor().
    Returns: the vendor that implemented this package.
    Exception: NA.
    
  7. 字符串getName(): 返回此包的名称。
    Syntax: public String getName()
    Returns: The fully-qualified name of this package as defined
     in section 6.5.3 of The Java™ Language Specification, for example, java.lang.
    Exception: NA
    

    // Java code illustrating getName(), getImplementationTitle()
    // and getImplementationVendor() and getImplementationVersion()
    // methods
    class PackageDemo
    {
    public static void main(String arg[])
    {
    Package pkgs[];
    pkgs = Package.getPackages();
    for ( int i= 0 ; i< 1 ; i++)
    {
    //  name of the package
    System.out.println(pkgs[i].getName());
    // checking title of this implementation
    System.out.println(pkgs[i].getImplementationTitle());
    // checking the vendor
    System.out.println(pkgs[i].getImplementationVendor());
    // version of this implementation
    System.out.println(pkgs[i].getImplementationVersion());
    }
    }
    }

    
    

    输出:

    sun.reflect
    Java Runtime Environment
    Oracle Corporation
    1.8.0_121
    
  8. 静态包getPackage(字符串名称): 在callers ClassLoader实例中按名称查找包。callers ClassLoader实例用于查找与命名类对应的包实例。如果调用者类加载器实例为空,则搜索系统类加载器实例加载的包集以查找命名包。
    Syntax: public static Package getPackage(String name)
    Returns: the package of the requested name. It may 
    be null if no package information is available from the archive or 
    codebase.
    Exception: NA
    
  9. 静态包[]getPackages(): 获取调用方的ClassLoader实例当前已知的所有包。这些包对应于通过该类加载器实例加载或通过名称访问的类。如果调用方的类加载器实例是引导类加载器实例(在某些实现中可能用null表示),则只返回与引导类加载器实例加载的类对应的包。
    Syntax: public static Package[] getPackages()
    Returns: a new array of packages known to the callers 
    ClassLoader instance. An zero length array is returned if none are known.
    Exception: NA
    

    // Java code illustrating getPackages() method
    class PackageDemo
    {
    public static void main(String arg[])
    {
    Package pkgs[];
    pkgs = Package.getPackages();
    Package pkg = Package.getPackage( "java.lang" );
    for ( int i= 0 ; i< 1 ; i++)
    {
    System.out.println(pkg.getName());
    System.out.println(pkgs[i].getName());
    }
    }
    }

    
    

    输出:

    java.lang
    sun.reflect
    
  10. 字符串getSpecificationTitle(): 返回此包实现的规范的标题。
    Syntax: public String getSpecificationTitle()
    Returns: the specification title, null is returned 
    if it is not known.
    exception: NA.
    
  11. 字符串getSpecificationVersion(): 返回此包实现的规范的版本号。此版本字符串必须是由“.”分隔的非负十进制整数序列和可能有前导零。比较版本字符串时,会比较最重要的数字。
    Syntax: public String getSpecificationVersion().
    Returns: the specification version, null is returned 
    if it is not known.
    Exception: NA.
    
  12. 字符串getSpecificationVendor(): 返回拥有并维护实现此包的类的规范的组织、供应商或公司的名称。
    Syntax: public String getSpecificationVendor()
    Returns: the specification vendor, null is returned
     if it is not known.
    Exception: NA.
    
  13. int hashCode(): 返回从包名计算出的哈希代码。
    Syntax: Return the hash code computed from the package name.
    Exception: NA
    Returns: the hash code.
    

    // Java code illustrating hashCode(), getSpecificationTitle()
    // getSpecificationVendor() and getSpecificationVersion()
    class PackageDemo
    {
    public static void main(String arg[])
    {
    Package pkgs[];
    pkgs = Package.getPackages();
    for ( int i= 0 ; i< 1 ; i++)
    {
    //  name of the package
    System.out.println(pkgs[i].hashCode());
    // checking title
    System.out.println(pkgs[i].getSpecificationTitle());
    // checking the vendor
    System.out.println(pkgs[i].getSpecificationVendor());
    // checking version
    System.out.println(pkgs[i].getSpecificationVersion());
    }
    }
    }

    
    

    输出:

    685414683
    Java Platform API Specification
    Oracle Corporation
    1.8
    
  14. 布尔值isCompatibleWith(需要字符串): 将此软件包的规范版本与所需版本进行比较。如果此软件包规格版本号大于或等于所需的版本号,则返回true。
    Syntax: public boolean isCompatibleWith(String desired).
    Returns: true if this package's version number is 
    greater than or equal to the desired version number
    Exception: 
    NumberFormatException - if the desired or current version is not 
    of the correct dotted form.
    
  15. 布尔值isSealed(): 如果此包已密封,则返回true。
    Syntax: public boolean isSealed()
    Returns: true if the package is sealed, false otherwise.
    Exception: NA
    
  16. 布尔isSealed(URL): 如果此包是相对于指定的代码源url密封的,则返回true。
    Syntax: public boolean isSealed(URL url)
    Returns: true if this package is sealed with respect to url
    Exception: NA
    
  17. 字符串toString(): 返回此包的字符串表示形式。它的值是字符串“package”和包名。如果定义了包标题,则会附加该标题。如果定义了软件包版本,则会追加该版本。
    Syntax: public String toString()
    Returns: the string representation of the package.
    Exception: NA
    

    // java code illustrating isCompatibleWith(), toString(),
    // isSealed methods
    import java.net.MalformedURLException;
    import java.net.URL;
    class PackageDemo
    {
    public static void main(String arg[]) throws MalformedURLException
    {
    Package pkg = Package.getPackage( "java.lang" );
    // checking if pkg is compatible with 1.0
    System.out.println(pkg.isCompatibleWith( "1.0" ));
    // checking if packet is sealed
    System.out.println(pkg.isSealed());
    URL url = new URL( " https://www.youtube.com/ " );
    System.out.println(pkg.isSealed(url));
    // string equivalent of package
    System.out.println(pkg.toString());
    }
    }

    
    

    输出:

    true
    false
    false
    package java.lang, Java Platform API Specification, version 1.8
    

本文由 阿布舍克·维马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享