diff --git a/src/main/java/java/lang/Class.java b/src/main/java/java/lang/Class.java index 832ad57..763a129 100644 --- a/src/main/java/java/lang/Class.java +++ b/src/main/java/java/lang/Class.java @@ -39,6 +39,58 @@ import org.cprover.CProver; +/** + * Instances of the class {@code Class} represent classes and + * interfaces in a running Java application. An enum is a kind of + * class and an annotation is a kind of interface. Every array also + * belongs to a class that is reflected as a {@code Class} object + * that is shared by all arrays with the same element type and number + * of dimensions. The primitive Java types ({@code boolean}, + * {@code byte}, {@code char}, {@code short}, + * {@code int}, {@code long}, {@code float}, and + * {@code double}), and the keyword {@code void} are also + * represented as {@code Class} objects. + * + *
{@code Class} has no public constructor. Instead {@code Class} + * objects are constructed automatically by the Java Virtual Machine as classes + * are loaded and by calls to the {@code defineClass} method in the class + * loader. + * + *
The following example uses a {@code Class} object to print the + * class name of an object: + * + *
+ * void printClassName(Object obj) {
+ * System.out.println("The class of " + obj +
+ * " is " + obj.getClass().getName());
+ * }
+ *
+ *
+ * It is also possible to get the {@code Class} object for a named + * type (or for void) using a class literal. See Section 15.8.2 of + * The Java™ Language Specification. + * For example: + * + *
+ * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} + *+ * + * @param
This method is useful for implementing {@link + * Object#hashCode()} on objects containing multiple fields. For + * example, if an object that has three fields, {@code x}, {@code + * y}, and {@code z}, one could write: + * + *
+ * @Override public int hashCode() {
+ * return Objects.hash(x, y, z);
+ * }
+ *
+ *
+ * Warning: When a single object reference is supplied, the returned
+ * value does not equal the hash code of that object reference. This
+ * value can be computed by calling {@link #hashCode(Object)}.
+ *
+ * @param values the values to be hashed
+ * @return a hash value of the sequence of input values
+ * @see Arrays#hashCode(Object[])
+ * @see List#hashCode
+ */
+ public static int hash(Object... values) {
+ return Arrays.hashCode(values);
+ }
+
+ /**
+ * Returns the result of calling {@code toString} for a non-{@code
+ * null} argument and {@code "null"} for a {@code null} argument.
+ *
+ * @param o an object
+ * @return the result of calling {@code toString} for a non-{@code
+ * null} argument and {@code "null"} for a {@code null} argument
+ * @see Object#toString
+ * @see String#valueOf(Object)
+ */
+ public static String toString(Object o) {
+ return String.valueOf(o);
+ }
+
+ /**
+ * Returns the result of calling {@code toString} on the first
+ * argument if the first argument is not {@code null} and returns
+ * the second argument otherwise.
+ *
+ * @param o an object
+ * @param nullDefault string to return if the first argument is
+ * {@code null}
+ * @return the result of calling {@code toString} on the first
+ * argument if it is not {@code null} and the second argument
+ * otherwise.
+ * @see Objects#toString(Object)
+ */
+ public static String toString(Object o, String nullDefault) {
+ return (o != null) ? o.toString() : nullDefault;
+ }
+
+ /**
+ * Returns 0 if the arguments are identical and {@code
+ * c.compare(a, b)} otherwise.
+ * Consequently, if both arguments are {@code null} 0
+ * is returned.
+ *
+ * Note that if one of the arguments is {@code null}, a {@code
+ * NullPointerException} may or may not be thrown depending on
+ * what ordering policy, if any, the {@link Comparator Comparator}
+ * chooses to have for {@code null} values.
+ *
+ * @param Unlike the method {@link #requireNonNull(Object, String)},
+ * this method allows creation of the message to be deferred until
+ * after the null check is made. While this may confer a
+ * performance advantage in the non-null case, when deciding to
+ * call this method care should be taken that the costs of
+ * creating the message supplier are less than the cost of just
+ * creating the string message directly.
+ *
+ * @param obj the object reference to check for nullity
+ * @param messageSupplier supplier of the detail message to be
+ * used in the event that a {@code NullPointerException} is thrown
+ * @param
+ *
+ * @param obj the object reference to check for nullity
+ * @param
+ * public Foo(Bar bar) {
+ * this.bar = Objects.requireNonNull(bar);
+ * }
+ *
+ *
+ * @param obj the object reference to check for nullity
+ * @param message detail message to be used in the event that a {@code
+ * NullPointerException} is thrown
+ * @param
+ * public Foo(Bar bar, Baz baz) {
+ * this.bar = Objects.requireNonNull(bar, "bar must not be null");
+ * this.baz = Objects.requireNonNull(baz, "baz must not be null");
+ * }
+ *