001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgueni Brevnov, Roman S. Bushmanov
019: * @version $Revision: 1.1.2.1.4.4 $
020: */package java.lang;
021:
022: import java.lang.reflect.Constructor;
023: import java.lang.reflect.Field;
024: import java.lang.reflect.Method;
025: import java.lang.reflect.Member;
026: import org.apache.harmony.drlvm.VMHelper;
027:
028: /**
029: * Provides the class information methods required for the
030: * {@link java.lang.Class Class} implementation, and class loading/resolution
031: * methods for the {@link java.lang.ClassLoader ClassLoader} implementation.
032: * <p>
033: * An implementation of the <code>java.lang.Class</code> class should not relay
034: * on default object initialization by the VM. In other words the VM is free to
035: * skip execution of the <code>java.lang.Class</code> private constructor.
036: * <p>
037: * This class must be implemented according to the common policy for porting
038: * interfaces - see the porting interface overview for more detailes.
039: *
040: * @api2vm
041: */
042: final class VMClassRegistry {
043: /**
044: * This method satisfies the requirements of the specification for the
045: * {@link Class#getSimpleName() Class.getSimpleName()}
046: * method.
047: *
048: * @param clazz a class to perform an operation on.
049: * @return the simple name of the specified class
050: *
051: * @api2vm
052: */
053: static native String getSimpleName(Class<?> clazz);
054:
055: /**
056: * Returns the nearest enclosing class of the specified Class instance,
057: * or <code>null</code> if the specified class is a top-level class.
058: * <br>This information is gathered from corresponding class-file structures
059: * (either EnclosingMethod or InnerClasses attribute, if any present).
060: *
061: * @param clazz a class to perform an operation on.
062: * @return the immediately enclosing class of the specified class or null
063: *
064: * @api2vm
065: */
066: static native Class getEnclosingClass(Class<?> clazz);
067:
068: /**
069: * If the specified class is a local or anonymous class defined
070: * within a method or constructor, returns that closest
071: * enclosing reflection member. Otherwise returns <code>null</code>.
072: * Note, instance initializers and static initializers
073: * are not reflectable and will never be considered.
074: * <br>This information is gathered from corresponding class-file structure
075: * (EnclosingMethod attribute, if present).
076: *
077: * @param clazz a class to perform an operation on.
078: * @return the immediately enclosing member for the specified class or null
079: *
080: * @api2vm
081: */
082: static native Member getEnclosingMember(Class<?> clazz);
083:
084: /**
085: * This class is not supposed to be instantiated.
086: */
087: private VMClassRegistry() {
088: }
089:
090: /**
091: * Loads the specified class with the bootstrap classloader.
092: * @throws LinkageError (or any subtype) if loading failed
093: * @api2vm
094: */
095: static native Class<?> loadBootstrapClass(String name);
096:
097: /**
098: * This method satisfies the requirements of the specification for the
099: * {@link Object#getClass() Object.getClass()} method.
100: * @api2vm
101: */
102: static native Class<? extends Object> getClassNative(Object obj);
103:
104: static Class<? extends Object> getClass(Object obj) {
105: if (VMHelper.isVMMagicPackageSupported()) {
106: return (Class<? extends Object>) VMHelper.getManagedClass(
107: obj).toObjectReference().toObject();
108: }
109: return getClassNative(obj);
110: }
111:
112: /**
113: * This method satisfies the requirements of the specification for the
114: * {@link Class#getClassLoader() Class.getClassLoader()} method except
115: * the clazz parameter may be null. In this case context class loader
116: * of the current thread is returned.
117: *
118: * @api2vm
119: */
120: static ClassLoader getClassLoader(Class<?> clazz) {
121: if (clazz != null) {
122: assert (getClassLoader0(clazz) == clazz.definingLoader);
123: return clazz.definingLoader;
124: }
125: return Thread.currentThread().getContextClassLoader();
126: }
127:
128: /**
129: * This method satisfies the requirements of the specification for the
130: * {@link Class#getClassLoader() Class.getClassLoader()} method.
131: *
132: * @api2vm
133: */
134: static native ClassLoader getClassLoader0(Class<?> clazz);
135:
136: /**
137: * This method satisfies the requirements of the specification for the
138: * {@link Class#getComponentType() Class.getComponentType()} method.
139: * @api2vm
140: */
141: static native Class<?> getComponentType(Class clazz);
142:
143: /**
144: * This method satisfies the requirements of the specification for the
145: * {@link Class#getDeclaredClasses() Class.getDeclaredClasses()}
146: * method.
147: * @api2vm
148: */
149: static native Class[] getDeclaredClasses(Class clazz);
150:
151: /**
152: * This method satisfies the requirements of the specification for the
153: * {@link Class#getDeclaredConstructors() Class.getDeclaredConstructors()}
154: * method.
155: * @api2vm
156: */
157: static native <U> Constructor<U>[] getDeclaredConstructors(
158: Class<U> clazz);
159:
160: /**
161: * This method satisfies the requirements of the specification for the
162: * {@link Class#getDeclaredFields() Class.getDeclaredFields()} method.
163: * @api2vm
164: */
165: static native Field[] getDeclaredFields(Class clazz);
166:
167: /**
168: * This method satisfies the requirements of the specification for the
169: * {@link Class#getDeclaredMethods() Class.getDeclaredMethods()} method.
170: * @api2vm
171: */
172: static native Method[] getDeclaredMethods(Class clazz);
173:
174: /**
175: * This method satisfies the requirements of the specification for the
176: * {@link Class#getDeclaringClass() Class.getDeclaringClass()} method.
177: * @api2vm
178: */
179: static native Class<?> getDeclaringClass(Class clazz);
180:
181: /**
182: * This method satisfies the requirements of the specification for the
183: * {@link Class#getInterfaces() Class.getInterfaces()} method.
184: * @api2vm
185: */
186: static native Class[] getInterfaces(Class clazz);
187:
188: /**
189: * This method satisfies the requirements of the specification for the
190: * {@link Class#getModifiers() Class.getModifiers()} method.
191: * @api2vm
192: */
193: static native int getModifiers(Class clazz);
194:
195: /**
196: * This method satisfies the requirements of the specification for the
197: * {@link Class#getName() Class.getName()} method.
198: * @api2vm
199: */
200: static native String getName(Class clazz);
201:
202: /**
203: * This method satisfies the requirements of the specification for the
204: * {@link Class#getSuperclass() Class.getSuperclass()} method.
205: * @api2vm
206: */
207: static native <U> Class<? super U> getSuperclass(Class<U> clazz);
208:
209: /**
210: * This method returns a list describing the system packages,
211: * in format of {{name, url}}. That is, the list consists of
212: * pairs "{name, url}", organized as the 2-dimensional array[N][2].
213: * The "name" is a Java package name.
214: * The "url" points to the jar file from which the corresponding package
215: * is loaded. If package comes not from a jar, then url is null.
216: *
217: * @param len number of packages caller already knows. If this number is
218: * equal to the actual number of system packages defined by VM,
219: * this method will skip array creation and return null.
220: * @return a set of packages defined by bootstrap class loader or null
221: * @api2vm
222: */
223: static native String[][] getSystemPackages(int len);
224:
225: /**
226: * This method is used for the
227: * {@link Class#forName(java.lang.String, boolean, java.lang.ClassLoader)
228: * Class.forName(String name, boolean initialize, ClassLoader loader)}
229: * method implementation. If the initialize parameter is true then this
230: * method should be invoked in order to initialize a class. The specified
231: * clazz parameter must not be null.
232: *
233: * @param clazz a class to perform an operation on.
234: * @throws ExceptionInInitializerError if initialization fails.
235: * @api2vm
236: */
237: static native void initializeClass(Class clazz);
238:
239: /**
240: * This method satisfies the requirements of the specification for the
241: * {@link Class#isArray() Class.isArray()} method.
242: * @api2vm
243: */
244: static native boolean isArray(Class clazz);
245:
246: /**
247: * This method satisfies the requirements of the specification for the
248: * {@link Class#isAssignableFrom(java.lang.Class)
249: * Class.isAssignableFrom(Class cls)} method.
250: * @api2vm
251: */
252: static native boolean isAssignableFrom(Class clazz,
253: Class<?> fromClazz); //XXX: does it have any sense?
254:
255: /**
256: * This method satisfies the requirements of the specification for the
257: * {@link Class#isInstance(java.lang.Object) Class.isInstance(Object obj)}
258: * method.
259: * @api2vm
260: */
261: static native boolean isInstance(Class clazz, Object obj);
262:
263: /**
264: * This method satisfies the requirements of the specification for the
265: * {@link Class#isPrimitive() Class.isPrimitive()} method.
266: * @api2vm
267: */
268: static native boolean isPrimitive(Class clazz);
269:
270: /**
271: * This method satisfies the requirements of the specification for the
272: * {@link ClassLoader#resolveClass(java.lang.Class)
273: * ClassLoader.resolveClass(Class c)} method. Except that it doesn't throw
274: * <code>NullPointerException</code> but throws <code>LinkagError</code>
275: * exception. The specified clazz parameter must not be null.
276: *
277: * @throws LinkageError if linking fails.
278: * @api2vm
279: */
280: static native void linkClass(Class<?> clazz);
281:
282: /**
283: * This method is used for the
284: * {@link Class#forName(java.lang.String, boolean, java.lang.ClassLoader)
285: * Class.forName(String name, boolean initialize, ClassLoader loader)}
286: * method implementation. If the name parameter represents an array then this
287: * method should be invoked in order to load an array class. For example, an
288: * expression (loadArray(Integer.TYPE, 1) == new int[0].getClass()) must be
289: * true.
290: * <p>
291: * <b>Note:</b> Under design yet. Subjected to change.
292: *
293: * @param componentType the type of array components. It must not be null.
294: * @param dimensions array dimension. It must be greater or equal to 0.
295: * @return a class which represents array
296: * @api2vm
297: */
298: static native Class loadArray(Class componentType, int dimensions);
299:
300: /**
301: * This method is used for implementation of the
302: * {@link Runtime#load(java.lang.String) Runtime.load(String filename)}
303: * method.
304: * @param filename full library name.
305: * @param loader the library will be loaded into the specified class loader
306: * namespace
307: * @throws UnsatisfiedLinkError if library can not be loaded for any reason
308: * @api2vm
309: */
310: static native void loadLibrary(String filename, ClassLoader loader);
311: }
|