Tuesday 21 March 2017

Package, StackTraceElement & The java.lang.ref and java.lang.reflect Packages - Java Tutorials

Package

Java 2 added a class called Package that encapsulates version data associated with a package. Package version information is becoming more important because of the proliferation of packages and because a Java program may need to know what version of a package is available. The following program demonstrates Package, displaying the packages about which the program currently is aware.

  // Demonstrate Package
  class PkgTest {
    public static void main(String args[]) {
      Package pkgs[];

      pkgs = Package.getPackages();

      for(int i=0; i < pkgs.length; i++)
        System.out.println(
              pkgs[i].getName() + " " +
              pkgs[i].getImplementationTitle() + " " +
              pkgs[i].getImplementationVendor() + " " +
              pkgs[i].getImplementationVersion()
        );
    }
  }


The Methods Defined by Package

String getImplementationTitle( ):  Returns the title of the invoking package.

String getImplementationVendor( ):  Returns the name of the implementor of the invoking package.

String getImplementationVersion( ):  Returns the version number of the invoking package.

String getName( ):  Returns the name of the invoking package.

static Package getPackage(String pkgName):  Returns a Package object with the name specified by pkgName.

static Package[ ] getPackages( ):  Returns all packages about which the invoking program is currently aware.

String getSpecificationTitle( ):  Returns the title of the invoking package’s specification.

String getSpecificationVendor( ):  Returns the name of the owner of the specification for the invoking package.

String getSpecificationVersion( ):  Returns the invoking package’s specification version number.

int hashCode( ):  Returns the hash code for the invoking package.

boolean isCompatibleWith(String verNum) throws NumberFormatException:  Returns true if verNum is less than or equal to the invoking package’s version number.

boolean isSealed( ):  Returns true if the invoking package is sealed. Returns false otherwise.

boolean isSealed(URL url):  Returns true if the invoking package is sealed relative to url. Returns false otherwise.

String toString( ):  Returns the string equivalent of the invoking package.


RuntimePermission

RuntimePermission was added to java.lang by Java 2. It relates to Java’s security mechanism and is not examined further here.


Throwable

The Throwable class supports Java’s exception-handling system, and is the class from which all exception classes are derived. It is discussed in Chapter 10.


SecurityManager

SecurityManager is an abstract class that your subclasses can implement to create a security manager. Generally, you don’t need to implement your own security manager. If you do, you need to consult the documentation that comes with your Java development system.





StackTraceElement

Java 2, version 1.4 adds the StackTraceElement class. This class describes a single stack frame, which is an individual element of a stack trace when an exception occurs. Each stack frame represents an execution point, which includes such things as the name of the method, the name of the file, and the source-code line number. An array of StackTraceElements is returned by the getStackTrace( ) method of the Throwable class. These methods give you programmatical access to a stack trace.


The Methods Defined by StackTraceElement

boolean equals(Object ob):  Returns true if the invoking StackTraceElement is the same as the one passed in ob. Otherwise, it returns false.

String getClassName( ):  Returns the class name of the execution point described by the invoking StackTraceElement.

String getFileName( ):  Returns the file name of the execution point described by the invoking StackTraceElement.

int getLineNumber( ):  Returns the source-code line number of the execution point described by the invoking StackTraceElement. In some situations the line number will not be available, in which case a negative value is returned.

String getMethodName( ):  Returns the method name of the execution point described by the invoking StackTraceElement.

int hashCode( ):  Returns the hash code for the invoking StackTraceElement.

boolean isNativeMethod( ):  Returns true if the invoking StackTraceElement describes a native method. Otherwise, returns false.

String toString( ):  Returns the String equivalent of the invoking sequence.


The Methods Defined by CharSequence

char charAt(int idx):  Returns the character at the index specified by idx.

int length( ):  Returns the number of characters in the invoking sequence.

CharSequence subSequence(int startIdx, int stopIdx):   Returns a subset of the invoking sequence beginning at startIdx and ending at stopIdx–1.

String toString( ):  Returns the String equivalent of the invoking sequence.


The CharSequence Interface

Java 2, version 1.4 adds the CharSequence interface. CharSequence defines methods that grant read-only access to a sequence of characters. This interface is implemented by String and StringBuffer. It is also implemented by CharBuffer, which is in the new java.nio package (described later in this book).


The Comparable Interface

Objects of classes that implement Comparable can be ordered. In other words, classes that implement Comparable contain objects that can be compared in some meaningful manner. The Comparable interface declares one method that is used to determine what Java 2 calls the natural ordering of instances of a class. The signature of the method is shown here:

      int compareTo(Object obj)

This method compares the invoking object with obj. It returns 0 if the values are equal. A negative value is returned if the invoking object has a lower value. Otherwise, a positive value is returned.

This interface is implemented by several of the classes already reviewed in this book. Specifically, the Byte, Character, Double, Float, Long, Short, String, and Integer classes define a compareTo( ) method. In addition, as the next chapter explains, objects that implement this interface can be used in various collections. Comparable was added by Java 2.




The java.lang.ref and java.lang.reflect Packages

Java defines two subpackages of java.lang: java.lang.ref and java.lang.reflect. Each is briefly described here.


java.lang.ref

You learned earlier that the garbage collection facilities in Java automatically determine when no references exist to an object. The object is then assumed to be no longer needed and its memory is reclaimed. The classes in the java.lang.ref package, which was added by Java 2, provide more flexible control over the garbage collection process. For example, assume that your program has created numerous objects that you want to reuse at some later time. You can continue to hold references to these objects, but that may require too much memory.

Instead, you can define “soft” references to these objects. An object that is “softly reachable” can be reclaimed by the garbage collector, if available memory runs low. In that case, the garbage collector sets the “soft” references to that object to null. Otherwise, the garbage collector saves the object for possible future use.

A programmer has the ability to determine whether a “softly reachable” object has been reclaimed. If it has been reclaimed, it can be re-created. Otherwise, the object is still available for reuse. You may also create “weak” and “phantom” references to objects. Discussion of these and other features of the java.lang.ref package are beyond the scope of this book.


java.lang.reflect

Reflection is the ability of a program to analyze itself. The java.lang.reflect package provides the ability to obtain information about the fields, constructors, methods, and modifiers of a class. You need this information to build software tools that enable you to work with Java Beans components. The tools use reflection to determine dynamically the characteristics of a component. This topic is considered in Chapter 25.

In addition, the java.lang.reflect package includes a class that enables you to create and access arrays dynamically.

No comments:

Post a Comment