author | dxu |
Thu, 04 Apr 2013 15:39:17 -0700 | |
changeset 16734 | da1901d79073 |
parent 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
16734
da1901d79073
8000406: change files using @GenerateNativeHeader to use @Native
dxu
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
12047 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package com.apple.jobjc; |
|
26 |
||
27 |
import java.lang.reflect.Field; |
|
28 |
import java.nio.ByteOrder; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.List; |
|
31 |
||
32 |
import sun.misc.Unsafe; |
|
33 |
||
13166
8470c1078142
7181501: Add some GenerateNativeHeader annotations and misc Mac adjustments to makefiles
erikj
parents:
12047
diff
changeset
|
34 |
|
12047 | 35 |
public final class JObjCRuntime { |
36 |
static { System.loadLibrary("JObjC"); } |
|
37 |
||
38 |
public static enum Arch{ ppc, i386, x86_64 }; |
|
39 |
public static enum Width{ W32, W64 }; |
|
40 |
||
41 |
public static final Arch ARCH = getArch(); |
|
42 |
public static final Width WIDTH = getWidth(); |
|
43 |
||
44 |
private static Arch getArch(){ |
|
45 |
String arch = System.getProperty("os.arch"); |
|
46 |
if("ppc".equals(arch)) return Arch.ppc; |
|
47 |
if("i386".equals(arch)) return Arch.i386; |
|
48 |
if("x86_64".equals(arch)) return Arch.x86_64; |
|
49 |
if("amd64".equals(arch)) return Arch.x86_64; |
|
50 |
if("universal".equals(arch)) return Arch.x86_64; |
|
51 |
throw new RuntimeException("Did not recognize os.arch system property: '" + arch + "'"); |
|
52 |
} |
|
53 |
||
54 |
private static Width getWidth(){ |
|
55 |
String width = System.getProperty("sun.arch.data.model"); |
|
56 |
if("32".equals(width)) return Width.W32; |
|
57 |
if("64".equals(width)) return Width.W64; |
|
58 |
throw new RuntimeException("Did not recognize sun.arch.data.model system property: '" + width + "'"); |
|
59 |
} |
|
60 |
||
61 |
public static final boolean IS32 = System.getProperty("sun.arch.data.model").equals("32"); |
|
62 |
public static final boolean IS64 = System.getProperty("sun.arch.data.model").equals("64"); |
|
63 |
public static final int PTR_LEN = IS64 ? 8 : 4; |
|
64 |
public static final boolean IS_BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN; |
|
65 |
static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("JObjC.debug")); |
|
66 |
||
67 |
static void checkPermission(){ |
|
68 |
final SecurityManager security = System.getSecurityManager(); |
|
69 |
if (security != null) security.checkPermission(new RuntimePermission("canProcessApplicationEvents")); |
|
70 |
} |
|
71 |
||
72 |
public final void assertOK(){ |
|
73 |
if(this != instance) |
|
74 |
throw new SecurityException("runtime"); |
|
75 |
} |
|
76 |
||
77 |
private JObjCRuntime(){} |
|
78 |
||
79 |
private static JObjCRuntime instance; |
|
80 |
static JObjCRuntime inst() { |
|
81 |
if (instance == null) instance = new JObjCRuntime(); |
|
82 |
return instance; |
|
83 |
} |
|
84 |
||
85 |
public static JObjCRuntime getInstance() { |
|
86 |
checkPermission(); |
|
87 |
return inst(); |
|
88 |
} |
|
89 |
||
90 |
public final NativeArgumentBuffer getThreadLocalState() { |
|
91 |
return NativeArgumentBuffer.getThreadLocalBuffer(this); |
|
92 |
} |
|
93 |
||
94 |
final Unsafe unsafe = getUnsafe(); |
|
95 |
final Subclassing subclassing = new Subclassing(this); |
|
96 |
final List<String> registeredPackages = new ArrayList<String>(); |
|
97 |
||
98 |
@SuppressWarnings("unchecked") |
|
99 |
Class<? extends ID> getClassForNativeClassName(final String className) { |
|
100 |
for (final String pkg : registeredPackages) { |
|
101 |
try { |
|
102 |
final Class<?> clazz = Class.forName(pkg + "." + className); |
|
103 |
if (clazz != null) return (Class<? extends ID>)clazz; |
|
104 |
} catch (final ClassNotFoundException e) { } |
|
105 |
} |
|
106 |
||
107 |
return null; |
|
108 |
} |
|
109 |
||
110 |
private final static Unsafe getUnsafe() { |
|
111 |
Unsafe inst = null; |
|
112 |
try { |
|
113 |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
|
114 |
f.setAccessible(true); |
|
115 |
inst = (Unsafe) f.get(null); |
|
116 |
if(inst == null) throw new NullPointerException("Unsafe.theUnsafe == null"); |
|
117 |
} catch (Exception e) { |
|
118 |
throw new RuntimeException("Unable to get instance of Unsafe.", e); |
|
119 |
} |
|
120 |
return inst; |
|
121 |
} |
|
122 |
||
123 |
public void registerPackage(final String pkg) { |
|
124 |
registeredPackages.add(pkg); |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Register a subclass of NSObject to allow the native side to send |
|
129 |
* messages which in turn call java methods declared on the class. |
|
130 |
* If a native class by the same name already exists, registerClass |
|
131 |
* will simply return without doing anything. |
|
132 |
* |
|
133 |
* For a usage example, see the SubclassingTest. |
|
134 |
*/ |
|
135 |
public boolean registerUserClass(Class<? extends ID> clazz, Class<? extends NSClass> clazzClazz) { |
|
136 |
return subclassing.registerUserClass(clazz, clazzClazz); |
|
137 |
} |
|
138 |
||
139 |
} |