1 /* 2 Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2.0, 6 as published by the Free Software Foundation. 7 8 This program is also distributed with certain software (including 9 but not limited to OpenSSL) that is licensed under separate terms, 10 as designated in a particular file or component or in included license 11 documentation. The authors of MySQL hereby grant you an additional 12 permission to link the program and your derivative works with the 13 separately licensed software that they have included with MySQL. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License, version 2.0, for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 /* 25 * Wrapper.java 26 */ 27 28 package com.mysql.jtie; 29 30 /** 31 * A base class for Java peer classes representing a C/C++ object type. 32 * 33 * This class declares only non-public members, which enable the JTie 34 * runtime to map a Java </code>Wrapper</code> instance to a C/C++ object. 35 * 36 * A Java peer class extending </code>Wrapper</code> 37 * <ol> 38 * <li> must provide a no-arg constructor (may be of any access modifier, 39 * but preferred private), 40 * <li> will declare a native method for each C/C++ (member and non-member) 41 * function to be mapped to Java, and 42 * <li> ought not to have any non-static fields (i.e., state by its own). 43 * </ol> 44 * 45 * Please, note that JTie provides no guarantees on the association between 46 * </code>Wrapper</code> instances and their underlying C/C++ objects. 47 * In particular, 48 * <ol> 49 * <li> multiple </code>Wrapper</code> instances may exist for the same 50 * C/C++ object at any time and 51 * <li> a C/C++ object may have been deleted by the application while 52 * corresponding Java </code>Wrapper</code> instances still exist 53 * (i.e., referential integrity cannot be assumed). 54 * </ol> 55 */ 56 public class Wrapper { 57 58 /* 59 * A native function called during static initialization of this class 60 * for preloaded caching of field/method Ids. 61 * 62 * Caution, this leads easily to circular dependencies: If the underlying 63 * native library has not been loaded when this function is called, the 64 * result is in a non-descriptive UnsatisfiedLinkError. 65 * 66 * For this, and since preloading cannot be expected to provide a large 67 * performance gain, this feature is not implemented at this time. 68 * 69 * static private native void initIds(); 70 * static { 71 * initIds(); 72 * } 73 */ 74 75 /** 76 * The address of the native delegate object. 77 */ 78 // comments: this field 79 // - is allowed to be private, for access from JNI is always possible; 80 // - is private for better security and consistency assurances; 81 // - must not be final if it is to be nullified when deleting the 82 // native delegate object through this instance from JNI; 83 // - is not required to be final under the hashcode() and equals() 84 // consistency requirement; 85 // - XXX consider (and benchmark) declaring this field volatile 86 // - a write then happens-before every subsequent read of that field 87 // - writes and reads of volatile longs and doubles are always atomic 88 private volatile long cdelegate; 89 90 /** 91 * Creates an unattached wrapper instance. 92 */ 93 // comments: 94 // - field access from JNI is fast, hence no initialization of this 95 // cdelegate field in a c'tor; Wrapper()96 protected Wrapper() { 97 //System.out.println("<-> jtie.Wrapper()"); 98 }; 99 100 /** 101 * Copies a wrapper instance. 102 */ Wrapper(Wrapper o)103 protected Wrapper(Wrapper o) { 104 //System.out.println("<-> jtie.Wrapper(Wrapper)"); 105 cdelegate = o.cdelegate; 106 }; 107 108 /** 109 * Indicates whether some other object is a wrapper that refers to the 110 * same native delegate instance. 111 * @see Object#equals(Object) 112 */ 113 // all equals() requirements are met due to pure delegation semantics equals(Object obj)114 public final boolean equals(Object obj) { 115 if (this == obj) 116 return true; 117 if (!(obj instanceof Wrapper)) 118 return false; 119 final Wrapper wo = (Wrapper)obj; 120 return (cdelegate == wo.cdelegate); 121 } 122 123 /** 124 * Returns a hash code value for the object. 125 * @see Object#hashCode() 126 */ 127 // all hashCode() requirements are met due to pure delegation semantics hashCode()128 public final int hashCode() { 129 return (int)cdelegate; // precision loss ok, for only a hash 130 } 131 132 /** 133 * Returns a string representation of the object. 134 * @see Object#toString() 135 */ 136 // overrides inherited toString() for full precision of cdelegate toString()137 public String toString() { 138 return (getClass().getName() + '@' + Long.toHexString(cdelegate)); 139 } 140 } 141