Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/SecurityManager.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/SecurityManager.java?rev=433041&r1=433040&r2=433041&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/SecurityManager.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/SecurityManager.java Sun Aug 20 13:54:27 2006
@@ -1,765 +1,765 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.lang;
-
-import java.io.File;
-import java.io.FileDescriptor;
-import java.io.FilePermission;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Member;
-import java.net.InetAddress;
-import java.net.SocketPermission;
-import java.security.AccessControlContext;
-import java.security.AccessController;
-import java.security.AllPermission;
-import java.security.Permission;
-import java.security.PrivilegedAction;
-import java.security.Security;
-import java.security.SecurityPermission;
-import java.util.PropertyPermission;
-import java.util.StringTokenizer;
-
-import org.apache.harmony.luni.util.PriviAction;
-
-/**
- * SecurityManager is the abstract superclass of the classes which can provide
- * security verification for a running program.
- */
-public class SecurityManager {
-
- static String[] securePackageList;
-
- private static final PropertyPermission READ_WRITE_ALL_PROPERTIES_PERMISSION = new PropertyPermission(
- "*", "read,write");
-
-
- /**
- * Flag to indicate whether a security check is in progress.
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- protected boolean inCheck;
-
- /**
- * Constructs a new instance of this class.
- */
- public SecurityManager() {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkPermission(RuntimePermission.permissionToCreateSecurityManager);
- }
- Class<?> type = Security.class; // initialize Security properties
- if (type == null) {
- throw new AssertionError();
- }
- }
-
- /**
- * Checks whether the running program is allowed to accept socket
- * connections.
- *
- * @param host
- * the address of the host which is attempting to connect
- * @param port
- * the port number to check
- */
- public void checkAccept(String host, int port) {
- if (host == null) {
- throw new NullPointerException();
- }
- checkPermission(new SocketPermission(host + ':' + port, "accept"));
- }
-
- /**
- * Checks whether the running program is allowed to modify the thread.
- *
- * @param thread
- * the thread we are attempting to modify
- */
- public void checkAccess(Thread thread) {
- // Only worry about system threads. Dead threads have a null group.
- ThreadGroup group = thread.getThreadGroup();
- if ((group != null) && (group.parent == null)) {
- checkPermission(RuntimePermission.permissionToModifyThread);
- }
- }
-
- /**
- * Checks whether the running program is allowed to modify the thread group.
- *
- *
- * @param group
- * the thread group we are attempting to modify
- */
- public void checkAccess(ThreadGroup group) {
- // Only worry about system threads.
- if (group == null) {
- throw new NullPointerException();
- }
- if (group.parent == null) {
- checkPermission(RuntimePermission.permissionToModifyThreadGroup);
- }
- }
-
- /**
- * Checks whether the running program is allowed to establish socket
- * connections. A -1 port indicates the caller is trying to resolve the
- * hostname.
- *
- * @param host
- * String the address of the host to connect to.
- * @param port
- * int the port number to check, or -1 for resolve.
- */
- public void checkConnect(String host, int port) {
- if (host == null) {
- throw new NullPointerException();
- }
- if (port > 0) {
- checkPermission(new SocketPermission(host + ':' + port, "connect"));
- } else {
- checkPermission(new SocketPermission(host, "resolve"));
- }
- }
-
- /**
- * Checks whether the given security context is allowed to establish socket
- * connections. A -1 port indicates the caller is trying to resolve the
- * hostname.
- *
- * @param host
- * String the address of the host to connect to.
- * @param port
- * int the port number to check, or -1 for resolve.
- * @param context
- * Object the security context to use for the check.
- */
- public void checkConnect(String host, int port, Object context) {
- if (port > 0) {
- checkPermission(new SocketPermission(host + ':' + port, "connect"),
- context);
- } else {
- checkPermission(new SocketPermission(host, "resolve"), context);
- }
- }
-
- /**
- * Checks whether the running program is allowed to create a class loader.
- */
- public void checkCreateClassLoader() {
- checkPermission(RuntimePermission.permissionToCreateClassLoader);
- }
-
- /**
- * Checks whether the running program is allowed to delete the file named by
- * the argument, which should be passed in canonical form.
- *
- * @param file
- * the name of the file to check
- */
- public void checkDelete(String file) {
- checkPermission(new FilePermission(file, "delete"));
- }
-
- /**
- * Checks whether the running program is allowed to execute the specified
- * platform specific command.
- *
- * @param cmd
- * the command line
- */
- public void checkExec(String cmd) {
- checkPermission(new FilePermission(new File(cmd).isAbsolute() ? cmd
- : "<<ALL FILES>>", "execute"));
- }
-
- /**
- * Checks whether the running program is allowed to terminate itself.
- *
- * @param status
- * the status to return from the exit.
- */
- public void checkExit(int status) {
- checkPermission(RuntimePermission.permissionToExitVM);
- }
-
- /**
- * Checks whether the running program is allowed to load the specified native
- * library.
- *
- * @param libName
- * the name of the library to load
- */
- public void checkLink(String libName) {
- if (libName == null) {
- throw new NullPointerException();
- }
- checkPermission(new RuntimePermission("loadLibrary." + libName));
- }
-
- /**
- * Checks whether the running program is allowed to listen on the specified
- * port.
- *
- * @param port
- * int the port number to check
- */
- public void checkListen(int port) {
- if (port == 0) {
- checkPermission(new SocketPermission("localhost:1024-",
- "listen"));
- } else {
- checkPermission(new SocketPermission("localhost:" + port,
- "listen"));
- }
- }
-
- /**
- * Checks whether the running program is allowed to access members. The
- * default is to allow access to public members (i.e.
- * java.lang.reflect.PUBLIC) and to classes loaded by the same loader as the
- * original caller (i.e. the method that called the reflect API).
- *
- * Due to the nature of the check, overriding implementations cannot call
- * super.checkMemberAccess() since the stack would no longer be of the
- * expected shape.
- *
- * @param cls ?
- * @param type
- * Either java.lang.reflect.Member.PUBLIC or DECLARED
- */
- public void checkMemberAccess(Class<?> cls, int type) {
- if (cls == null) {
- throw new NullPointerException();
- }
- if (type == Member.PUBLIC) {
- return;
- }
- //
- // Need to compare the classloaders.
- // Stack shape is
- // <user code> <- want this class
- // Class.getDeclared*();
- // Class.checkMemberAccess();
- // SecurityManager.checkMemberAccess(); <- current frame
- //
- // Use getClassLoaderImpl() since getClassLoader()
- // returns null for the bootstrap class loader.
- if (ClassLoader.getStackClassLoader(3) == cls.getClassLoaderImpl()) {
- return;
- }
-
- // Forward off to the permission mechanism.
- checkPermission(new RuntimePermission("accessDeclaredMembers"));
- }
-
- /**
- * Checks whether the running program is allowed to join, leave or send to a
- * multicast address.
- */
- public void checkMulticast(InetAddress maddr) {
- checkPermission(new SocketPermission(maddr.getHostAddress(),
- "accept,connect"));
- }
-
- /**
- * Checks whether the running program is allowed to join, leave or send to a
- * multicast address.
- *
- * @deprecated use SecurityManager#checkMulticast(java.net.InetAddress)
- */
- @Deprecated
- public void checkMulticast(InetAddress maddr, byte ttl) {
- checkPermission(new SocketPermission(maddr.getHostAddress(),
- "accept,connect"));
- }
-
- /**
- * Checks whether the running program is allowed to access the specified
- * package.
- *
- * @param packageName
- * the name of the package to be accessed.
- */
- public void checkPackageAccess(String packageName) {
- if (packageName == null) {
- throw new NullPointerException();
- }
- if (securePackageList == null) {
- String securePackages = getSecurityProperty("package.access");
- if (securePackages != null) {
- StringTokenizer tokenizer = new StringTokenizer(securePackages,
- ", ");
- int i = 0;
- securePackageList = new String[tokenizer.countTokens()];
- while (tokenizer.hasMoreTokens()) {
- securePackageList[i++] = tokenizer.nextToken();
- }
- } else {
- securePackageList = new String[0];
- }
- }
- for (int i = 0; i < securePackageList.length; i++) {
- if (packageName.startsWith(securePackageList[i])) {
- checkPermission(new RuntimePermission("accessClassInPackage."
- + packageName));
- return;
- }
- }
- }
-
- /**
- * Checks whether the running program is allowed to define new classes in
- * the specified package.
- *
- * @param packageName
- * the name of the package to add a class to.
- */
- public void checkPackageDefinition(String packageName) {
- if (packageName == null) {
- throw new NullPointerException();
- }
- String securePackages = getSecurityProperty("package.definition");
- if (securePackages != null) {
- StringTokenizer tokenizer = new StringTokenizer(securePackages,
- ", ");
- while (tokenizer.hasMoreTokens()) {
- if (packageName.startsWith(tokenizer.nextToken())) {
- checkPermission(new RuntimePermission(
- "defineClassInPackage." + packageName));
- return;
- }
- }
- }
- }
-
- private String getSecurityProperty(final String property) {
- PrivilegedAction<String> pa = PriviAction.getSecurityProperty(property);
- return AccessController.doPrivileged(pa);
- }
-
- /**
- * Checks whether the running program is allowed to access the system
- * properties.
- */
- public void checkPropertiesAccess() {
- checkPermission(READ_WRITE_ALL_PROPERTIES_PERMISSION);
- }
-
- /**
- * Checks whether the running program is allowed to access a particular
- * system property.
- *
- * @param key
- * the name of the property to be accessed.
- */
- public void checkPropertyAccess(String key) {
- checkPermission(new PropertyPermission(key, "read"));
- }
-
- /**
- * Checks whether the running program is allowed to read from the file whose
- * descriptor is the argument.
- *
- * @param fd
- * the file descriptor of the file to check
- */
- public void checkRead(FileDescriptor fd) {
- if (fd == null) {
- throw new NullPointerException();
- }
- checkPermission(RuntimePermission.permissionToReadFileDescriptor);
- }
-
- /**
- * Checks whether the running program is allowed to read from the file named
- * by the argument, which should be passed in canonical form.
- *
- * @param file
- * String the name of the file or directory to check.
- */
- public void checkRead(String file) {
- checkPermission(new FilePermission(file, "read"));
- }
-
- /**
- * Checks whether the given security context is allowed to read from the
- * file named by the argument, which should be passed in canonical form.
- *
- * @param file
- * String the name of the file or directory to check.
- * @param context
- * Object the security context to use for the check.
- */
- public void checkRead(String file, Object context) {
- checkPermission(new FilePermission(file, "read"), context);
- }
-
- /**
- * Checks whether the running program is allowed to perform the security
- * operation named by the target.
- *
- * @param target
- * String the name of the operation to perform.
- */
- public void checkSecurityAccess(String target) {
- checkPermission(new SecurityPermission(target));
- }
-
- /**
- * Checks whether the running program is allowed to set the net object
- * factories.
- */
- public void checkSetFactory() {
- checkPermission(RuntimePermission.permissionToSetFactory);
- }
-
- /**
- * Checks whether the running program is allowed to create a top level
- * window.
- *
- * @param window
- * The non-null window for which to check access
- */
- public boolean checkTopLevelWindow(Object window) {
- if (window == null) {
- throw new NullPointerException();
- }
- try {
- Class<?> awtPermission = Class.forName("java.awt.AWTPermission");
- Class[] args = new Class[] { java.lang.String.class };
- Constructor<?> constructor = awtPermission.getConstructor(args);
- Object[] constructorArgs = new Object[] { "showWindowWithoutWarningBanner" };
- Permission perm = (Permission) constructor.newInstance(constructorArgs);
- checkPermission(perm);
- } catch (ClassNotFoundException e) {
- } catch (NoSuchMethodException e) {
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- } catch (InvocationTargetException e) {
- } catch (SecurityException e) {
- return false;
- }
- return true;
- }
-
- /**
- * Checks whether the running program is allowed to access the system
- * clipboard.
- */
- public void checkSystemClipboardAccess() {
- try {
- Class<?> awtPermission = Class.forName("java.awt.AWTPermission");
- Class[] args = new Class[] { String.class };
- Constructor<?> constructor = awtPermission.getConstructor(args);
- Object[] constructorArgs = new Object[] { "accessClipboard" };
- Permission perm = (Permission) constructor.newInstance(constructorArgs);
- checkPermission(perm);
- return;
- } catch (ClassNotFoundException e) {
- } catch (NoSuchMethodException e) {
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- } catch (InvocationTargetException e) {
- }
- throw new SecurityException();
- }
-
- /**
- * Checks whether the running program is allowed to access the AWT Event
- * queue. Since we don't support AWT, the answer is no.
- */
- public void checkAwtEventQueueAccess() {
- try {
- Class<?> awtPermission = Class.forName("java.awt.AWTPermission");
- Class[] ar = new Class[] { String.class };
- Constructor<?> constructor = awtPermission.getConstructor(ar);
- Object[] constructorArgs = new Object[] { "accessEventQueue" };
- Permission perm = (Permission) constructor.newInstance(constructorArgs);
- checkPermission(perm);
- return;
- } catch (ClassNotFoundException e) {
- } catch (NoSuchMethodException e) {
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- } catch (InvocationTargetException e) {
- }
- throw new SecurityException();
- }
-
- /**
- * Checks whether the running program is allowed to start a new print job.
- */
- public void checkPrintJobAccess() {
- checkPermission(RuntimePermission.permissionToQueuePrintJob);
- }
-
- /**
- * Checks whether the running program is allowed to read from the file whose
- * descriptor is the argument.
- *
- * @param fd
- * the file descriptor of the file to check
- */
- public void checkWrite(FileDescriptor fd) {
- if (fd == null) {
- throw new NullPointerException();
- }
- checkPermission(RuntimePermission.permissionToWriteFileDescriptor);
- }
-
- /**
- * Checks whether the running program is allowed to write to the file named
- * by the argument, which should be passed in canonical form.
- *
- * @param file
- * the name of the file to check
- */
- public void checkWrite(String file) {
- checkPermission(new FilePermission(file, "write"));
- }
-
- /**
- * Answers true if the security manager is currently checking something.
- *
- * @return boolean true if we are are in a security check method.
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- public boolean getInCheck() {
- return inCheck;
- }
-
- /**
- * Answers an array containing one entry for each method in the stack. Each
- * entry is the java.lang.Class which represents the class in which the
- * method is defined.
- *
- * @return Class[] all of the classes in the stack.
- */
- protected Class[] getClassContext() {
- return Class.getStackClasses(-1, false);
- }
-
- /**
- * Answers the class loader of the first class in the stack whose class
- * loader is not a system class loader.
- *
- * @return ClassLoader the most recent non-system class loader.
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- protected ClassLoader currentClassLoader() {
-
- // First, check if AllPermission is allowed. If so, then we
- // are effectively running in an unsafe environment, so just
- // answer null (==> everything is a system class).
- try {
- checkPermission(new AllPermission());
- return null;
- } catch (SecurityException ex) {
- }
-
- // Now, check if there are any non-system class loaders in
- // the stack up to the first privileged method (or the end
- // of the stack.
- Class[] classes = Class.getStackClasses(-1, true);
- for (int i = 0; i < classes.length; i++) {
- ClassLoader cl = classes[i].getClassLoaderImpl();
- if (!cl.isSystemClassLoader()) {
- return cl;
- }
- }
- return null;
- }
-
- /**
- * Answers the index in the stack of three first class whose class loader is
- * not a system class loader.
- *
- * @return int the frame index of the first method whose class was loaded by
- * a non-system class loader.
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- protected int classLoaderDepth() {
- // First, check if AllPermission is allowed. If so, then we
- // are effectively running in an unsafe environment, so just
- // answer -1 (==> everything is a system class).
- try {
- checkPermission(new AllPermission());
- return -1;
- } catch (SecurityException ex) {
- }
-
- // Now, check if there are any non-system class loaders in
- // the stack up to the first privileged method (or the end
- // of the stack.
- Class[] classes = Class.getStackClasses(-1, true);
- for (int i = 0; i < classes.length; i++) {
- ClassLoader cl = classes[i].getClassLoaderImpl();
- if (!cl.isSystemClassLoader()) {
- return i;
- }
- }
- return -1;
- }
-
- /**
- * Answers the first class in the stack which was loaded by a class loader
- * which is not a system class loader.
- *
- * @return Class the most recent class loaded by a non-system class loader.
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- protected Class<?> currentLoadedClass() {
- // First, check if AllPermission is allowed. If so, then we
- // are effectively running in an unsafe environment, so just
- // answer null (==> everything is a system class).
- try {
- checkPermission(new AllPermission());
- return null;
- } catch (SecurityException ex) {
- }
-
- // Now, check if there are any non-system class loaders in
- // the stack up to the first privileged method (or the end
- // of the stack.
- Class[] classes = Class.getStackClasses(-1, true);
- for (int i = 0; i < classes.length; i++) {
- ClassLoader cl = classes[i].getClassLoaderImpl();
- if (!cl.isSystemClassLoader()) {
- return classes[i];
- }
- }
- return null;
- }
-
- /**
- * Answers the index in the stack of the first method which is contained in
- * a class called <code>name</code>. If no methods from this class are in
- * the stack, return -1.
- *
- * @param name
- * String the name of the class to look for.
- * @return int the depth in the stack of a the first method found.
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- protected int classDepth(String name) {
- Class[] classes = Class.getStackClasses(-1, false);
- for (int i = 0; i < classes.length; i++) {
- if (classes[i].getName().equals(name)) {
- return i;
- }
- }
- return -1;
- }
-
- /**
- * Answers true if there is a method on the stack from the specified class,
- * and false otherwise.
- *
- * @param name
- * String the name of the class to look for.
- * @return boolean true if we are running a method from the specified class.
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- protected boolean inClass(String name) {
- return classDepth(name) != -1;
- }
-
- /**
- * Answers true if there is a method on the stack from a class which was
- * defined by a non-system classloader.
- *
- * @return boolean
- *
- * @deprecated Use checkPermission
- */
- @Deprecated
- protected boolean inClassLoader() {
- return currentClassLoader() != null;
- }
-
- /**
- * Answers the thread group which should be used to instantiate new threads.
- * By default, this is the same as the thread group of the thread running
- * this method.
- *
- * @return ThreadGroup The thread group to create new threads in.
- */
- public ThreadGroup getThreadGroup() {
- return Thread.currentThread().getThreadGroup();
- }
-
- /**
- * Answers an object which encapsulates the security state of the current
- * point in the execution. In our case, this is an AccessControlContext.
- */
- public Object getSecurityContext() {
- return AccessController.getContext();
- }
-
- /**
- * Checks whether the running program is allowed to access the resource
- * being guarded by the given Permission argument.
- *
- * @param permission
- * the permission to check
- */
- public void checkPermission(Permission permission) {
- try {
- inCheck = true;
- AccessController.checkPermission(permission);
- } finally {
- inCheck = false;
- }
- }
-
- /**
- * Checks whether the running program is allowed to access the resource
- * being guarded by the given Permission argument.
- *
- * @param permission
- * the permission to check
- */
- public void checkPermission(Permission permission, Object context) {
- try {
- inCheck = true;
- // Must be an AccessControlContext. If we don't check
- // this, then applications could pass in an arbitrary
- // object which circumvents the security check.
- if (context instanceof AccessControlContext) {
- ((AccessControlContext) context).checkPermission(permission);
- } else {
- throw new SecurityException();
- }
- } finally {
- inCheck = false;
- }
- }
-}
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.lang;
+
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FilePermission;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Member;
+import java.net.InetAddress;
+import java.net.SocketPermission;
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.AllPermission;
+import java.security.Permission;
+import java.security.PrivilegedAction;
+import java.security.Security;
+import java.security.SecurityPermission;
+import java.util.PropertyPermission;
+import java.util.StringTokenizer;
+
+import org.apache.harmony.luni.util.PriviAction;
+
+/**
+ * SecurityManager is the abstract superclass of the classes which can provide
+ * security verification for a running program.
+ */
+public class SecurityManager {
+
+ static String[] securePackageList;
+
+ private static final PropertyPermission READ_WRITE_ALL_PROPERTIES_PERMISSION = new PropertyPermission(
+ "*", "read,write");
+
+
+ /**
+ * Flag to indicate whether a security check is in progress.
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ protected boolean inCheck;
+
+ /**
+ * Constructs a new instance of this class.
+ */
+ public SecurityManager() {
+ SecurityManager security = System.getSecurityManager();
+ if (security != null) {
+ security.checkPermission(RuntimePermission.permissionToCreateSecurityManager);
+ }
+ Class<?> type = Security.class; // initialize Security properties
+ if (type == null) {
+ throw new AssertionError();
+ }
+ }
+
+ /**
+ * Checks whether the running program is allowed to accept socket
+ * connections.
+ *
+ * @param host
+ * the address of the host which is attempting to connect
+ * @param port
+ * the port number to check
+ */
+ public void checkAccept(String host, int port) {
+ if (host == null) {
+ throw new NullPointerException();
+ }
+ checkPermission(new SocketPermission(host + ':' + port, "accept"));
+ }
+
+ /**
+ * Checks whether the running program is allowed to modify the thread.
+ *
+ * @param thread
+ * the thread we are attempting to modify
+ */
+ public void checkAccess(Thread thread) {
+ // Only worry about system threads. Dead threads have a null group.
+ ThreadGroup group = thread.getThreadGroup();
+ if ((group != null) && (group.parent == null)) {
+ checkPermission(RuntimePermission.permissionToModifyThread);
+ }
+ }
+
+ /**
+ * Checks whether the running program is allowed to modify the thread group.
+ *
+ *
+ * @param group
+ * the thread group we are attempting to modify
+ */
+ public void checkAccess(ThreadGroup group) {
+ // Only worry about system threads.
+ if (group == null) {
+ throw new NullPointerException();
+ }
+ if (group.parent == null) {
+ checkPermission(RuntimePermission.permissionToModifyThreadGroup);
+ }
+ }
+
+ /**
+ * Checks whether the running program is allowed to establish socket
+ * connections. A -1 port indicates the caller is trying to resolve the
+ * hostname.
+ *
+ * @param host
+ * String the address of the host to connect to.
+ * @param port
+ * int the port number to check, or -1 for resolve.
+ */
+ public void checkConnect(String host, int port) {
+ if (host == null) {
+ throw new NullPointerException();
+ }
+ if (port > 0) {
+ checkPermission(new SocketPermission(host + ':' + port, "connect"));
+ } else {
+ checkPermission(new SocketPermission(host, "resolve"));
+ }
+ }
+
+ /**
+ * Checks whether the given security context is allowed to establish socket
+ * connections. A -1 port indicates the caller is trying to resolve the
+ * hostname.
+ *
+ * @param host
+ * String the address of the host to connect to.
+ * @param port
+ * int the port number to check, or -1 for resolve.
+ * @param context
+ * Object the security context to use for the check.
+ */
+ public void checkConnect(String host, int port, Object context) {
+ if (port > 0) {
+ checkPermission(new SocketPermission(host + ':' + port, "connect"),
+ context);
+ } else {
+ checkPermission(new SocketPermission(host, "resolve"), context);
+ }
+ }
+
+ /**
+ * Checks whether the running program is allowed to create a class loader.
+ */
+ public void checkCreateClassLoader() {
+ checkPermission(RuntimePermission.permissionToCreateClassLoader);
+ }
+
+ /**
+ * Checks whether the running program is allowed to delete the file named by
+ * the argument, which should be passed in canonical form.
+ *
+ * @param file
+ * the name of the file to check
+ */
+ public void checkDelete(String file) {
+ checkPermission(new FilePermission(file, "delete"));
+ }
+
+ /**
+ * Checks whether the running program is allowed to execute the specified
+ * platform specific command.
+ *
+ * @param cmd
+ * the command line
+ */
+ public void checkExec(String cmd) {
+ checkPermission(new FilePermission(new File(cmd).isAbsolute() ? cmd
+ : "<<ALL FILES>>", "execute"));
+ }
+
+ /**
+ * Checks whether the running program is allowed to terminate itself.
+ *
+ * @param status
+ * the status to return from the exit.
+ */
+ public void checkExit(int status) {
+ checkPermission(RuntimePermission.permissionToExitVM);
+ }
+
+ /**
+ * Checks whether the running program is allowed to load the specified native
+ * library.
+ *
+ * @param libName
+ * the name of the library to load
+ */
+ public void checkLink(String libName) {
+ if (libName == null) {
+ throw new NullPointerException();
+ }
+ checkPermission(new RuntimePermission("loadLibrary." + libName));
+ }
+
+ /**
+ * Checks whether the running program is allowed to listen on the specified
+ * port.
+ *
+ * @param port
+ * int the port number to check
+ */
+ public void checkListen(int port) {
+ if (port == 0) {
+ checkPermission(new SocketPermission("localhost:1024-",
+ "listen"));
+ } else {
+ checkPermission(new SocketPermission("localhost:" + port,
+ "listen"));
+ }
+ }
+
+ /**
+ * Checks whether the running program is allowed to access members. The
+ * default is to allow access to public members (i.e.
+ * java.lang.reflect.PUBLIC) and to classes loaded by the same loader as the
+ * original caller (i.e. the method that called the reflect API).
+ *
+ * Due to the nature of the check, overriding implementations cannot call
+ * super.checkMemberAccess() since the stack would no longer be of the
+ * expected shape.
+ *
+ * @param cls ?
+ * @param type
+ * Either java.lang.reflect.Member.PUBLIC or DECLARED
+ */
+ public void checkMemberAccess(Class<?> cls, int type) {
+ if (cls == null) {
+ throw new NullPointerException();
+ }
+ if (type == Member.PUBLIC) {
+ return;
+ }
+ //
+ // Need to compare the classloaders.
+ // Stack shape is
+ // <user code> <- want this class
+ // Class.getDeclared*();
+ // Class.checkMemberAccess();
+ // SecurityManager.checkMemberAccess(); <- current frame
+ //
+ // Use getClassLoaderImpl() since getClassLoader()
+ // returns null for the bootstrap class loader.
+ if (ClassLoader.getStackClassLoader(3) == cls.getClassLoaderImpl()) {
+ return;
+ }
+
+ // Forward off to the permission mechanism.
+ checkPermission(new RuntimePermission("accessDeclaredMembers"));
+ }
+
+ /**
+ * Checks whether the running program is allowed to join, leave or send to a
+ * multicast address.
+ */
+ public void checkMulticast(InetAddress maddr) {
+ checkPermission(new SocketPermission(maddr.getHostAddress(),
+ "accept,connect"));
+ }
+
+ /**
+ * Checks whether the running program is allowed to join, leave or send to a
+ * multicast address.
+ *
+ * @deprecated use SecurityManager#checkMulticast(java.net.InetAddress)
+ */
+ @Deprecated
+ public void checkMulticast(InetAddress maddr, byte ttl) {
+ checkPermission(new SocketPermission(maddr.getHostAddress(),
+ "accept,connect"));
+ }
+
+ /**
+ * Checks whether the running program is allowed to access the specified
+ * package.
+ *
+ * @param packageName
+ * the name of the package to be accessed.
+ */
+ public void checkPackageAccess(String packageName) {
+ if (packageName == null) {
+ throw new NullPointerException();
+ }
+ if (securePackageList == null) {
+ String securePackages = getSecurityProperty("package.access");
+ if (securePackages != null) {
+ StringTokenizer tokenizer = new StringTokenizer(securePackages,
+ ", ");
+ int i = 0;
+ securePackageList = new String[tokenizer.countTokens()];
+ while (tokenizer.hasMoreTokens()) {
+ securePackageList[i++] = tokenizer.nextToken();
+ }
+ } else {
+ securePackageList = new String[0];
+ }
+ }
+ for (int i = 0; i < securePackageList.length; i++) {
+ if (packageName.startsWith(securePackageList[i])) {
+ checkPermission(new RuntimePermission("accessClassInPackage."
+ + packageName));
+ return;
+ }
+ }
+ }
+
+ /**
+ * Checks whether the running program is allowed to define new classes in
+ * the specified package.
+ *
+ * @param packageName
+ * the name of the package to add a class to.
+ */
+ public void checkPackageDefinition(String packageName) {
+ if (packageName == null) {
+ throw new NullPointerException();
+ }
+ String securePackages = getSecurityProperty("package.definition");
+ if (securePackages != null) {
+ StringTokenizer tokenizer = new StringTokenizer(securePackages,
+ ", ");
+ while (tokenizer.hasMoreTokens()) {
+ if (packageName.startsWith(tokenizer.nextToken())) {
+ checkPermission(new RuntimePermission(
+ "defineClassInPackage." + packageName));
+ return;
+ }
+ }
+ }
+ }
+
+ private String getSecurityProperty(final String property) {
+ PrivilegedAction<String> pa = PriviAction.getSecurityProperty(property);
+ return AccessController.doPrivileged(pa);
+ }
+
+ /**
+ * Checks whether the running program is allowed to access the system
+ * properties.
+ */
+ public void checkPropertiesAccess() {
+ checkPermission(READ_WRITE_ALL_PROPERTIES_PERMISSION);
+ }
+
+ /**
+ * Checks whether the running program is allowed to access a particular
+ * system property.
+ *
+ * @param key
+ * the name of the property to be accessed.
+ */
+ public void checkPropertyAccess(String key) {
+ checkPermission(new PropertyPermission(key, "read"));
+ }
+
+ /**
+ * Checks whether the running program is allowed to read from the file whose
+ * descriptor is the argument.
+ *
+ * @param fd
+ * the file descriptor of the file to check
+ */
+ public void checkRead(FileDescriptor fd) {
+ if (fd == null) {
+ throw new NullPointerException();
+ }
+ checkPermission(RuntimePermission.permissionToReadFileDescriptor);
+ }
+
+ /**
+ * Checks whether the running program is allowed to read from the file named
+ * by the argument, which should be passed in canonical form.
+ *
+ * @param file
+ * String the name of the file or directory to check.
+ */
+ public void checkRead(String file) {
+ checkPermission(new FilePermission(file, "read"));
+ }
+
+ /**
+ * Checks whether the given security context is allowed to read from the
+ * file named by the argument, which should be passed in canonical form.
+ *
+ * @param file
+ * String the name of the file or directory to check.
+ * @param context
+ * Object the security context to use for the check.
+ */
+ public void checkRead(String file, Object context) {
+ checkPermission(new FilePermission(file, "read"), context);
+ }
+
+ /**
+ * Checks whether the running program is allowed to perform the security
+ * operation named by the target.
+ *
+ * @param target
+ * String the name of the operation to perform.
+ */
+ public void checkSecurityAccess(String target) {
+ checkPermission(new SecurityPermission(target));
+ }
+
+ /**
+ * Checks whether the running program is allowed to set the net object
+ * factories.
+ */
+ public void checkSetFactory() {
+ checkPermission(RuntimePermission.permissionToSetFactory);
+ }
+
+ /**
+ * Checks whether the running program is allowed to create a top level
+ * window.
+ *
+ * @param window
+ * The non-null window for which to check access
+ */
+ public boolean checkTopLevelWindow(Object window) {
+ if (window == null) {
+ throw new NullPointerException();
+ }
+ try {
+ Class<?> awtPermission = Class.forName("java.awt.AWTPermission");
+ Class[] args = new Class[] { java.lang.String.class };
+ Constructor<?> constructor = awtPermission.getConstructor(args);
+ Object[] constructorArgs = new Object[] { "showWindowWithoutWarningBanner" };
+ Permission perm = (Permission) constructor.newInstance(constructorArgs);
+ checkPermission(perm);
+ } catch (ClassNotFoundException e) {
+ } catch (NoSuchMethodException e) {
+ } catch (InstantiationException e) {
+ } catch (IllegalAccessException e) {
+ } catch (InvocationTargetException e) {
+ } catch (SecurityException e) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Checks whether the running program is allowed to access the system
+ * clipboard.
+ */
+ public void checkSystemClipboardAccess() {
+ try {
+ Class<?> awtPermission = Class.forName("java.awt.AWTPermission");
+ Class[] args = new Class[] { String.class };
+ Constructor<?> constructor = awtPermission.getConstructor(args);
+ Object[] constructorArgs = new Object[] { "accessClipboard" };
+ Permission perm = (Permission) constructor.newInstance(constructorArgs);
+ checkPermission(perm);
+ return;
+ } catch (ClassNotFoundException e) {
+ } catch (NoSuchMethodException e) {
+ } catch (InstantiationException e) {
+ } catch (IllegalAccessException e) {
+ } catch (InvocationTargetException e) {
+ }
+ throw new SecurityException();
+ }
+
+ /**
+ * Checks whether the running program is allowed to access the AWT Event
+ * queue. Since we don't support AWT, the answer is no.
+ */
+ public void checkAwtEventQueueAccess() {
+ try {
+ Class<?> awtPermission = Class.forName("java.awt.AWTPermission");
+ Class[] ar = new Class[] { String.class };
+ Constructor<?> constructor = awtPermission.getConstructor(ar);
+ Object[] constructorArgs = new Object[] { "accessEventQueue" };
+ Permission perm = (Permission) constructor.newInstance(constructorArgs);
+ checkPermission(perm);
+ return;
+ } catch (ClassNotFoundException e) {
+ } catch (NoSuchMethodException e) {
+ } catch (InstantiationException e) {
+ } catch (IllegalAccessException e) {
+ } catch (InvocationTargetException e) {
+ }
+ throw new SecurityException();
+ }
+
+ /**
+ * Checks whether the running program is allowed to start a new print job.
+ */
+ public void checkPrintJobAccess() {
+ checkPermission(RuntimePermission.permissionToQueuePrintJob);
+ }
+
+ /**
+ * Checks whether the running program is allowed to read from the file whose
+ * descriptor is the argument.
+ *
+ * @param fd
+ * the file descriptor of the file to check
+ */
+ public void checkWrite(FileDescriptor fd) {
+ if (fd == null) {
+ throw new NullPointerException();
+ }
+ checkPermission(RuntimePermission.permissionToWriteFileDescriptor);
+ }
+
+ /**
+ * Checks whether the running program is allowed to write to the file named
+ * by the argument, which should be passed in canonical form.
+ *
+ * @param file
+ * the name of the file to check
+ */
+ public void checkWrite(String file) {
+ checkPermission(new FilePermission(file, "write"));
+ }
+
+ /**
+ * Answers true if the security manager is currently checking something.
+ *
+ * @return boolean true if we are are in a security check method.
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ public boolean getInCheck() {
+ return inCheck;
+ }
+
+ /**
+ * Answers an array containing one entry for each method in the stack. Each
+ * entry is the java.lang.Class which represents the class in which the
+ * method is defined.
+ *
+ * @return Class[] all of the classes in the stack.
+ */
+ protected Class[] getClassContext() {
+ return Class.getStackClasses(-1, false);
+ }
+
+ /**
+ * Answers the class loader of the first class in the stack whose class
+ * loader is not a system class loader.
+ *
+ * @return ClassLoader the most recent non-system class loader.
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ protected ClassLoader currentClassLoader() {
+
+ // First, check if AllPermission is allowed. If so, then we
+ // are effectively running in an unsafe environment, so just
+ // answer null (==> everything is a system class).
+ try {
+ checkPermission(new AllPermission());
+ return null;
+ } catch (SecurityException ex) {
+ }
+
+ // Now, check if there are any non-system class loaders in
+ // the stack up to the first privileged method (or the end
+ // of the stack.
+ Class[] classes = Class.getStackClasses(-1, true);
+ for (int i = 0; i < classes.length; i++) {
+ ClassLoader cl = classes[i].getClassLoaderImpl();
+ if (!cl.isSystemClassLoader()) {
+ return cl;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Answers the index in the stack of three first class whose class loader is
+ * not a system class loader.
+ *
+ * @return int the frame index of the first method whose class was loaded by
+ * a non-system class loader.
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ protected int classLoaderDepth() {
+ // First, check if AllPermission is allowed. If so, then we
+ // are effectively running in an unsafe environment, so just
+ // answer -1 (==> everything is a system class).
+ try {
+ checkPermission(new AllPermission());
+ return -1;
+ } catch (SecurityException ex) {
+ }
+
+ // Now, check if there are any non-system class loaders in
+ // the stack up to the first privileged method (or the end
+ // of the stack.
+ Class[] classes = Class.getStackClasses(-1, true);
+ for (int i = 0; i < classes.length; i++) {
+ ClassLoader cl = classes[i].getClassLoaderImpl();
+ if (!cl.isSystemClassLoader()) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Answers the first class in the stack which was loaded by a class loader
+ * which is not a system class loader.
+ *
+ * @return Class the most recent class loaded by a non-system class loader.
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ protected Class<?> currentLoadedClass() {
+ // First, check if AllPermission is allowed. If so, then we
+ // are effectively running in an unsafe environment, so just
+ // answer null (==> everything is a system class).
+ try {
+ checkPermission(new AllPermission());
+ return null;
+ } catch (SecurityException ex) {
+ }
+
+ // Now, check if there are any non-system class loaders in
+ // the stack up to the first privileged method (or the end
+ // of the stack.
+ Class[] classes = Class.getStackClasses(-1, true);
+ for (int i = 0; i < classes.length; i++) {
+ ClassLoader cl = classes[i].getClassLoaderImpl();
+ if (!cl.isSystemClassLoader()) {
+ return classes[i];
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Answers the index in the stack of the first method which is contained in
+ * a class called <code>name</code>. If no methods from this class are in
+ * the stack, return -1.
+ *
+ * @param name
+ * String the name of the class to look for.
+ * @return int the depth in the stack of a the first method found.
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ protected int classDepth(String name) {
+ Class[] classes = Class.getStackClasses(-1, false);
+ for (int i = 0; i < classes.length; i++) {
+ if (classes[i].getName().equals(name)) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Answers true if there is a method on the stack from the specified class,
+ * and false otherwise.
+ *
+ * @param name
+ * String the name of the class to look for.
+ * @return boolean true if we are running a method from the specified class.
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ protected boolean inClass(String name) {
+ return classDepth(name) != -1;
+ }
+
+ /**
+ * Answers true if there is a method on the stack from a class which was
+ * defined by a non-system classloader.
+ *
+ * @return boolean
+ *
+ * @deprecated Use checkPermission
+ */
+ @Deprecated
+ protected boolean inClassLoader() {
+ return currentClassLoader() != null;
+ }
+
+ /**
+ * Answers the thread group which should be used to instantiate new threads.
+ * By default, this is the same as the thread group of the thread running
+ * this method.
+ *
+ * @return ThreadGroup The thread group to create new threads in.
+ */
+ public ThreadGroup getThreadGroup() {
+ return Thread.currentThread().getThreadGroup();
+ }
+
+ /**
+ * Answers an object which encapsulates the security state of the current
+ * point in the execution. In our case, this is an AccessControlContext.
+ */
+ public Object getSecurityContext() {
+ return AccessController.getContext();
+ }
+
+ /**
+ * Checks whether the running program is allowed to access the resource
+ * being guarded by the given Permission argument.
+ *
+ * @param permission
+ * the permission to check
+ */
+ public void checkPermission(Permission permission) {
+ try {
+ inCheck = true;
+ AccessController.checkPermission(permission);
+ } finally {
+ inCheck = false;
+ }
+ }
+
+ /**
+ * Checks whether the running program is allowed to access the resource
+ * being guarded by the given Permission argument.
+ *
+ * @param permission
+ * the permission to check
+ */
+ public void checkPermission(Permission permission, Object context) {
+ try {
+ inCheck = true;
+ // Must be an AccessControlContext. If we don't check
+ // this, then applications could pass in an arbitrary
+ // object which circumvents the security check.
+ if (context instanceof AccessControlContext) {
+ ((AccessControlContext) context).checkPermission(permission);
+ } else {
+ throw new SecurityException();
+ }
+ } finally {
+ inCheck = false;
+ }
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/SecurityManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java?rev=433041&r1=433040&r2=433041&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java Sun Aug 20 13:54:27 2006
@@ -1,364 +1,364 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.lang;
-
-/**
- * <p>
- * Short is the wrapper for the primitive type <code>short</code>.
- * </p>
- *
- * @see java.lang.Number
- * @since 1.1
- */
-public final class Short extends Number implements Comparable<Short> {
-
- private static final long serialVersionUID = 7515723908773894738L;
-
- /**
- * The value which the receiver represents.
- */
- private final short value;
-
- /**
- * <p>
- * Constant for the maximum <code>short</code> value, 2<sup>15</sup>-1.
- * </p>
- */
- public static final short MAX_VALUE = (short) 0x7FFF;
-
- /**
- * <p>
- * Constant for the minimum <code>short</code> value, -2<sup>15</sup>.
- * </p>
- */
- public static final short MIN_VALUE = (short) 0x8000;
-
- /**
- * <p>
- * Constant for the number of bits to represent a <code>short</code> in
- * two's compliment form.
- * </p>
- *
- * @since 1.5
- */
- public static final int SIZE = 16;
-
- /**
- * The java.lang.Class that represents this class.
- */
- @SuppressWarnings("unchecked")
- public static final Class<Short> TYPE = (Class<Short>) new short[0]
- .getClass().getComponentType();
-
- // Note: This can't be set to "short.class", since *that* is
- // defined to be "java.lang.Short.TYPE";
-
- /**
- * <p>
- * A cache of instances used by {@link #valueOf(short)} and auto-boxing.
- * </p>
- */
- private static final Short[] CACHE = new Short[256];
-
- /**
- * Constructs a new instance of this class given a string.
- *
- * @param string
- * a string representation of a short quantity.
- * @exception NumberFormatException
- * if the argument could not be parsed as a short quantity.
- */
- public Short(String string) throws NumberFormatException {
- this(parseShort(string));
- }
-
- /**
- * Constructs a new instance of the receiver which represents the short
- * valued argument.
- *
- * @param value
- * the short to store in the new instance.
- */
- public Short(short value) {
- this.value = value;
- }
-
- /**
- * Answers the byte value which the receiver represents
- *
- * @return byte the value of the receiver.
- */
- @Override
- public byte byteValue() {
- return (byte) value;
- }
-
- /**
- * <p>
- * Compares this <code>Short</code> to the <code>Short</code>
- * passed. If this instance's value is equal to the value of the instance
- * passed, then 0 is returned. If this instance's value is less than the
- * value of the instance passed, then a negative value is returned. If this
- * instance's value is greater than the value of the instance passed, then a
- * positive value is returned.
- * </p>
- *
- * @param object The instance to compare to.
- * @throws NullPointerException if <code>object</code> is
- * <code>null</code>.
- * @since 1.2
- */
- public int compareTo(Short object) {
- return value > object.value ? 1 : (value < object.value ? -1 : 0);
- }
-
- /**
- * Parses the string argument as if it was a short value and returns the
- * result. Throws NumberFormatException if the string does not represent an
- * int quantity. The string may be a hexadecimal ("0x..."), octal ("0..."),
- * or decimal ("...") representation of a byte.
- *
- * @param string
- * a string representation of a short quantity.
- * @return Short the value represented by the argument
- * @exception NumberFormatException
- * if the argument could not be parsed as a short quantity.
- */
- public static Short decode(String string) throws NumberFormatException {
- int intValue = Integer.decode(string).intValue();
- short result = (short) intValue;
- if (result == intValue) {
- return valueOf(result);
- }
- throw new NumberFormatException();
- }
-
- /**
- * Answers the double value which the receiver represents
- *
- * @return double the value of the receiver.
- */
- @Override
- public double doubleValue() {
- return value;
- }
-
- /**
- * Compares the argument to the receiver, and answers true if they represent
- * the <em>same</em> object using a class specific comparison.
- * <p>
- * In this case, the argument must also be a Short, and the receiver and
- * argument must represent the same short value.
- *
- * @param object
- * the object to compare with this object
- * @return <code>true</code> if the object is the same as this object
- * <code>false</code> if it is different from this object
- * @see #hashCode
- */
- @Override
- public boolean equals(Object object) {
- return (object == this) || (object instanceof Short)
- && (value == ((Short) object).value);
- }
-
- /**
- * Answers the float value which the receiver represents
- *
- * @return float the value of the receiver.
- */
- @Override
- public float floatValue() {
- return value;
- }
-
- /**
- * Answers an integer hash code for the receiver. Any two objects which
- * answer <code>true</code> when passed to <code>equals</code> must
- * answer the same value for this method.
- *
- * @return the receiver's hash
- *
- * @see #equals
- */
- @Override
- public int hashCode() {
- return value;
- }
-
- /**
- * Answers the int value which the receiver represents
- *
- * @return int the value of the receiver.
- */
- @Override
- public int intValue() {
- return value;
- }
-
- /**
- * Answers the long value which the receiver represents
- *
- * @return long the value of the receiver.
- */
- @Override
- public long longValue() {
- return value;
- }
-
- /**
- * Parses the string argument as if it was a short value and returns the
- * result. Throws NumberFormatException if the string does not represent an
- * short quantity.
- *
- * @param string
- * a string representation of a short quantity.
- * @return short the value represented by the argument
- * @exception NumberFormatException
- * if the argument could not be parsed as a short quantity.
- */
- public static short parseShort(String string) throws NumberFormatException {
- return parseShort(string, 10);
- }
-
- /**
- * Parses the string argument as if it was a short value and returns the
- * result. Throws NumberFormatException if the string does not represent a
- * single short quantity. The second argument specifies the radix to use
- * when parsing the value.
- *
- * @param string
- * a string representation of a short quantity.
- * @param radix
- * the radix to use when parsing.
- * @return short the value represented by the argument
- * @exception NumberFormatException
- * if the argument could not be parsed as a short quantity.
- */
- public static short parseShort(String string, int radix)
- throws NumberFormatException {
- int intValue = Integer.parseInt(string, radix);
- short result = (short) intValue;
- if (result == intValue) {
- return result;
- }
- throw new NumberFormatException();
- }
-
- /**
- * Answers the short value which the receiver represents
- *
- * @return short the value of the receiver.
- */
- @Override
- public short shortValue() {
- return value;
- }
-
- /**
- * Answers a string containing a concise, human-readable description of the
- * receiver.
- *
- * @return a printable representation for the receiver.
- */
- @Override
- public String toString() {
- return Integer.toString(value);
- }
-
- /**
- * Answers a string containing a concise, human-readable description of the
- * argument.
- *
- * @param value
- * short the short to convert.
- * @return String a printable representation for the short.
- */
- public static String toString(short value) {
- return Integer.toString(value);
- }
-
- /**
- * Parses the string argument as if it was a short value and returns a Short
- * representing the result. Throws NumberFormatException if the string does
- * not represent a single short quantity.
- *
- * @param string
- * a string representation of a short quantity.
- * @return Short the value represented by the argument
- * @exception NumberFormatException
- * if the argument could not be parsed as a short quantity.
- */
- public static Short valueOf(String string) throws NumberFormatException {
- return valueOf(parseShort(string));
- }
-
- /**
- * Parses the string argument as if it was a short value and returns a Short
- * representing the result. Throws NumberFormatException if the string does
- * not represent a short quantity. The second argument specifies the radix
- * to use when parsing the value.
- *
- * @param string
- * a string representation of a short quantity.
- * @param radix
- * the radix to use when parsing.
- * @return Short the value represented by the argument
- * @exception NumberFormatException
- * if the argument could not be parsed as a short quantity.
- */
- public static Short valueOf(String string, int radix)
- throws NumberFormatException {
- return valueOf(parseShort(string, radix));
- }
-
- /**
- * <p>
- * Reverses the bytes of a <code>short</code>.
- * </p>
- *
- * @param s The <code>short</code> to reverse.
- * @return The reversed value.
- * @since 1.5
- */
- public static short reverseBytes(short s) {
- int high = (s >> 8) & 0xFF;
- int low = (s & 0xFF) << 8;
- return (short) (low | high);
- }
-
- /**
- * <p>
- * Returns a <code>Short</code> instance for the <code>short</code>
- * value passed. This method is preferred over the constructor, as this
- * method may maintain a cache of instances.
- * </p>
- *
- * @param s The short value.
- * @return A <code>Short</code> instance.
- * @since 1.5
- */
- public static Short valueOf(short s) {
- if (s < -128 || s > 127) {
- return new Short(s);
- }
- synchronized (CACHE) {
- int idx = 128 + s; // 128 matches a cache size of 256
- Short result = CACHE[idx];
- return (result == null ? CACHE[idx] = new Short(s) : result);
- }
- }
-}
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.lang;
+
+/**
+ * <p>
+ * Short is the wrapper for the primitive type <code>short</code>.
+ * </p>
+ *
+ * @see java.lang.Number
+ * @since 1.1
+ */
+public final class Short extends Number implements Comparable<Short> {
+
+ private static final long serialVersionUID = 7515723908773894738L;
+
+ /**
+ * The value which the receiver represents.
+ */
+ private final short value;
+
+ /**
+ * <p>
+ * Constant for the maximum <code>short</code> value, 2<sup>15</sup>-1.
+ * </p>
+ */
+ public static final short MAX_VALUE = (short) 0x7FFF;
+
+ /**
+ * <p>
+ * Constant for the minimum <code>short</code> value, -2<sup>15</sup>.
+ * </p>
+ */
+ public static final short MIN_VALUE = (short) 0x8000;
+
+ /**
+ * <p>
+ * Constant for the number of bits to represent a <code>short</code> in
+ * two's compliment form.
+ * </p>
+ *
+ * @since 1.5
+ */
+ public static final int SIZE = 16;
+
+ /**
+ * The java.lang.Class that represents this class.
+ */
+ @SuppressWarnings("unchecked")
+ public static final Class<Short> TYPE = (Class<Short>) new short[0]
+ .getClass().getComponentType();
+
+ // Note: This can't be set to "short.class", since *that* is
+ // defined to be "java.lang.Short.TYPE";
+
+ /**
+ * <p>
+ * A cache of instances used by {@link #valueOf(short)} and auto-boxing.
+ * </p>
+ */
+ private static final Short[] CACHE = new Short[256];
+
+ /**
+ * Constructs a new instance of this class given a string.
+ *
+ * @param string
+ * a string representation of a short quantity.
+ * @exception NumberFormatException
+ * if the argument could not be parsed as a short quantity.
+ */
+ public Short(String string) throws NumberFormatException {
+ this(parseShort(string));
+ }
+
+ /**
+ * Constructs a new instance of the receiver which represents the short
+ * valued argument.
+ *
+ * @param value
+ * the short to store in the new instance.
+ */
+ public Short(short value) {
+ this.value = value;
+ }
+
+ /**
+ * Answers the byte value which the receiver represents
+ *
+ * @return byte the value of the receiver.
+ */
+ @Override
+ public byte byteValue() {
+ return (byte) value;
+ }
+
+ /**
+ * <p>
+ * Compares this <code>Short</code> to the <code>Short</code>
+ * passed. If this instance's value is equal to the value of the instance
+ * passed, then 0 is returned. If this instance's value is less than the
+ * value of the instance passed, then a negative value is returned. If this
+ * instance's value is greater than the value of the instance passed, then a
+ * positive value is returned.
+ * </p>
+ *
+ * @param object The instance to compare to.
+ * @throws NullPointerException if <code>object</code> is
+ * <code>null</code>.
+ * @since 1.2
+ */
+ public int compareTo(Short object) {
+ return value > object.value ? 1 : (value < object.value ? -1 : 0);
+ }
+
+ /**
+ * Parses the string argument as if it was a short value and returns the
+ * result. Throws NumberFormatException if the string does not represent an
+ * int quantity. The string may be a hexadecimal ("0x..."), octal ("0..."),
+ * or decimal ("...") representation of a byte.
+ *
+ * @param string
+ * a string representation of a short quantity.
+ * @return Short the value represented by the argument
+ * @exception NumberFormatException
+ * if the argument could not be parsed as a short quantity.
+ */
+ public static Short decode(String string) throws NumberFormatException {
+ int intValue = Integer.decode(string).intValue();
+ short result = (short) intValue;
+ if (result == intValue) {
+ return valueOf(result);
+ }
+ throw new NumberFormatException();
+ }
+
+ /**
+ * Answers the double value which the receiver represents
+ *
+ * @return double the value of the receiver.
+ */
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ /**
+ * Compares the argument to the receiver, and answers true if they represent
+ * the <em>same</em> object using a class specific comparison.
+ * <p>
+ * In this case, the argument must also be a Short, and the receiver and
+ * argument must represent the same short value.
+ *
+ * @param object
+ * the object to compare with this object
+ * @return <code>true</code> if the object is the same as this object
+ * <code>false</code> if it is different from this object
+ * @see #hashCode
+ */
+ @Override
+ public boolean equals(Object object) {
+ return (object == this) || (object instanceof Short)
+ && (value == ((Short) object).value);
+ }
+
+ /**
+ * Answers the float value which the receiver represents
+ *
+ * @return float the value of the receiver.
+ */
+ @Override
+ public float floatValue() {
+ return value;
+ }
+
+ /**
+ * Answers an integer hash code for the receiver. Any two objects which
+ * answer <code>true</code> when passed to <code>equals</code> must
+ * answer the same value for this method.
+ *
+ * @return the receiver's hash
+ *
+ * @see #equals
+ */
+ @Override
+ public int hashCode() {
+ return value;
+ }
+
+ /**
+ * Answers the int value which the receiver represents
+ *
+ * @return int the value of the receiver.
+ */
+ @Override
+ public int intValue() {
+ return value;
+ }
+
+ /**
+ * Answers the long value which the receiver represents
+ *
+ * @return long the value of the receiver.
+ */
+ @Override
+ public long longValue() {
+ return value;
+ }
+
+ /**
+ * Parses the string argument as if it was a short value and returns the
+ * result. Throws NumberFormatException if the string does not represent an
+ * short quantity.
+ *
+ * @param string
+ * a string representation of a short quantity.
+ * @return short the value represented by the argument
+ * @exception NumberFormatException
+ * if the argument could not be parsed as a short quantity.
+ */
+ public static short parseShort(String string) throws NumberFormatException {
+ return parseShort(string, 10);
+ }
+
+ /**
+ * Parses the string argument as if it was a short value and returns the
+ * result. Throws NumberFormatException if the string does not represent a
+ * single short quantity. The second argument specifies the radix to use
+ * when parsing the value.
+ *
+ * @param string
+ * a string representation of a short quantity.
+ * @param radix
+ * the radix to use when parsing.
+ * @return short the value represented by the argument
+ * @exception NumberFormatException
+ * if the argument could not be parsed as a short quantity.
+ */
+ public static short parseShort(String string, int radix)
+ throws NumberFormatException {
+ int intValue = Integer.parseInt(string, radix);
+ short result = (short) intValue;
+ if (result == intValue) {
+ return result;
+ }
+ throw new NumberFormatException();
+ }
+
+ /**
+ * Answers the short value which the receiver represents
+ *
+ * @return short the value of the receiver.
+ */
+ @Override
+ public short shortValue() {
+ return value;
+ }
+
+ /**
+ * Answers a string containing a concise, human-readable description of the
+ * receiver.
+ *
+ * @return a printable representation for the receiver.
+ */
+ @Override
+ public String toString() {
+ return Integer.toString(value);
+ }
+
+ /**
+ * Answers a string containing a concise, human-readable description of the
+ * argument.
+ *
+ * @param value
+ * short the short to convert.
+ * @return String a printable representation for the short.
+ */
+ public static String toString(short value) {
+ return Integer.toString(value);
+ }
+
+ /**
+ * Parses the string argument as if it was a short value and returns a Short
+ * representing the result. Throws NumberFormatException if the string does
+ * not represent a single short quantity.
+ *
+ * @param string
+ * a string representation of a short quantity.
+ * @return Short the value represented by the argument
+ * @exception NumberFormatException
+ * if the argument could not be parsed as a short quantity.
+ */
+ public static Short valueOf(String string) throws NumberFormatException {
+ return valueOf(parseShort(string));
+ }
+
+ /**
+ * Parses the string argument as if it was a short value and returns a Short
+ * representing the result. Throws NumberFormatException if the string does
+ * not represent a short quantity. The second argument specifies the radix
+ * to use when parsing the value.
+ *
+ * @param string
+ * a string representation of a short quantity.
+ * @param radix
+ * the radix to use when parsing.
+ * @return Short the value represented by the argument
+ * @exception NumberFormatException
+ * if the argument could not be parsed as a short quantity.
+ */
+ public static Short valueOf(String string, int radix)
+ throws NumberFormatException {
+ return valueOf(parseShort(string, radix));
+ }
+
+ /**
+ * <p>
+ * Reverses the bytes of a <code>short</code>.
+ * </p>
+ *
+ * @param s The <code>short</code> to reverse.
+ * @return The reversed value.
+ * @since 1.5
+ */
+ public static short reverseBytes(short s) {
+ int high = (s >> 8) & 0xFF;
+ int low = (s & 0xFF) << 8;
+ return (short) (low | high);
+ }
+
+ /**
+ * <p>
+ * Returns a <code>Short</code> instance for the <code>short</code>
+ * value passed. This method is preferred over the constructor, as this
+ * method may maintain a cache of instances.
+ * </p>
+ *
+ * @param s The short value.
+ * @return A <code>Short</code> instance.
+ * @since 1.5
+ */
+ public static Short valueOf(short s) {
+ if (s < -128 || s > 127) {
+ return new Short(s);
+ }
+ synchronized (CACHE) {
+ int idx = 128 + s; // 128 matches a cache size of 256
+ Short result = CACHE[idx];
+ return (result == null ? CACHE[idx] = new Short(s) : result);
+ }
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Short.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StackOverflowError.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StackOverflowError.java?rev=433041&r1=433040&r2=433041&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StackOverflowError.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StackOverflowError.java Sun Aug 20 13:54:27 2006
@@ -1,46 +1,46 @@
-/* Copyright 1998, 2002 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.lang;
-
-
-/**
- * This error is thrown when the depth of the callstack of the running program
- * excedes some platform or virtual machine specific limit. Typically, this will
- * occur only when a program becomes infinitely recursive, but can occur in
- * correctly written (but deeply recursive) programs.
- */
-public class StackOverflowError extends java.lang.VirtualMachineError {
-
- private static final long serialVersionUID = 8609175038441759607L;
-
- /**
- * Constructs a new instance of this class with its walkback filled in.
- */
- public StackOverflowError() {
- super();
- }
-
- /**
- * Constructs a new instance of this class with its walkback and message
- * filled in.
- *
- * @param detailMessage
- * String The detail message for the exception.
- */
- public StackOverflowError(String detailMessage) {
- super(detailMessage);
- }
-}
+/* Copyright 1998, 2002 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.lang;
+
+
+/**
+ * This error is thrown when the depth of the callstack of the running program
+ * excedes some platform or virtual machine specific limit. Typically, this will
+ * occur only when a program becomes infinitely recursive, but can occur in
+ * correctly written (but deeply recursive) programs.
+ */
+public class StackOverflowError extends java.lang.VirtualMachineError {
+
+ private static final long serialVersionUID = 8609175038441759607L;
+
+ /**
+ * Constructs a new instance of this class with its walkback filled in.
+ */
+ public StackOverflowError() {
+ super();
+ }
+
+ /**
+ * Constructs a new instance of this class with its walkback and message
+ * filled in.
+ *
+ * @param detailMessage
+ * String The detail message for the exception.
+ */
+ public StackOverflowError(String detailMessage) {
+ super(detailMessage);
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StackOverflowError.java
------------------------------------------------------------------------------
svn:eol-style = native
|