mirror of
https://github.com/corda/corda.git
synced 2024-12-28 00:38:55 +00:00
sketch a few more classpath classes
This commit is contained in:
parent
48226f988c
commit
fd770fd884
@ -1,7 +1,11 @@
|
||||
package java.lang;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class Thread implements Runnable {
|
||||
private final Runnable task;
|
||||
private Map<ThreadLocal, Object> locals;
|
||||
private long peer;
|
||||
|
||||
public Thread(Runnable task) {
|
||||
@ -16,6 +20,15 @@ public class Thread implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<ThreadLocal, Object> locals() {
|
||||
if (locals == null) {
|
||||
locals = new WeakHashMap();
|
||||
}
|
||||
return locals;
|
||||
}
|
||||
|
||||
public static native Thread currentThread();
|
||||
|
||||
public static native void sleep(long milliseconds)
|
||||
throws InterruptedException;
|
||||
}
|
||||
|
36
classpath/java/lang/ThreadLocal.java
Normal file
36
classpath/java/lang/ThreadLocal.java
Normal file
@ -0,0 +1,36 @@
|
||||
package java.lang;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ThreadLocal<T> {
|
||||
private static final Object Null = new Object();
|
||||
|
||||
protected T initialValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
Map<ThreadLocal, Object> map = Thread.currentThread().locals();
|
||||
Object o = map.get(this);
|
||||
if (o == null) {
|
||||
o = initialValue();
|
||||
if (o == null) {
|
||||
o = Null;
|
||||
}
|
||||
map.put(this, o);
|
||||
}
|
||||
if (o == Null) {
|
||||
o = null;
|
||||
}
|
||||
return (T) o;
|
||||
}
|
||||
|
||||
public void set(T value) {
|
||||
Map<ThreadLocal, Object> map = Thread.currentThread().locals();
|
||||
Object o = value;
|
||||
if (o == null) {
|
||||
o = Null;
|
||||
}
|
||||
map.put(this, o);
|
||||
}
|
||||
}
|
@ -23,6 +23,10 @@ public class Throwable {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
public Throwable getCause() {
|
||||
return cause;
|
||||
}
|
||||
|
||||
private static native Object trace(int skipCount);
|
||||
|
||||
private static native StackTraceElement[] resolveTrace(Object trace);
|
||||
|
7
classpath/java/lang/reflect/Array.java
Normal file
7
classpath/java/lang/reflect/Array.java
Normal file
@ -0,0 +1,7 @@
|
||||
package java.lang.reflect;
|
||||
|
||||
public final class Array {
|
||||
private Array() { }
|
||||
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package java.lang.reflect;
|
||||
|
||||
public class Constructor<T> extends AccessibleObject {
|
||||
public class Constructor<T> extends AccessibleObject implements Member {
|
||||
private Method<T> method;
|
||||
|
||||
private Constructor() { }
|
||||
@ -17,4 +17,16 @@ public class Constructor<T> extends AccessibleObject {
|
||||
public void setAccessible(boolean v) {
|
||||
method.setAccessible(v);
|
||||
}
|
||||
|
||||
public Class<T> getDeclaringClass() {
|
||||
return method.getDeclaringClass();
|
||||
}
|
||||
|
||||
public int getModifiers() {
|
||||
return method.getModifiers();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return method.getName();
|
||||
}
|
||||
}
|
||||
|
@ -18,4 +18,16 @@ public class Field<T> extends AccessibleObject {
|
||||
public void setAccessible(boolean v) {
|
||||
if (v) vmFlags |= Accessible; else vmFlags &= ~Accessible;
|
||||
}
|
||||
|
||||
public Class<T> getDeclaringClass() {
|
||||
return class_;
|
||||
}
|
||||
|
||||
public int getModifiers() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return new String(name, 0, name.length - 1, false);
|
||||
}
|
||||
}
|
||||
|
19
classpath/java/lang/reflect/InvocationTargetException.java
Normal file
19
classpath/java/lang/reflect/InvocationTargetException.java
Normal file
@ -0,0 +1,19 @@
|
||||
package java.lang.reflect;
|
||||
|
||||
public class InvocationTargetException extends Exception {
|
||||
public InvocationTargetException(Throwable targetException, String message) {
|
||||
super(message, targetException);
|
||||
}
|
||||
|
||||
public InvocationTargetException(Throwable targetException) {
|
||||
this(targetException, null);
|
||||
}
|
||||
|
||||
public InvocationTargetException() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
public Throwable getTargetException() {
|
||||
return getCause();
|
||||
}
|
||||
}
|
12
classpath/java/lang/reflect/Member.java
Normal file
12
classpath/java/lang/reflect/Member.java
Normal file
@ -0,0 +1,12 @@
|
||||
package java.lang.reflect;
|
||||
|
||||
public interface Member {
|
||||
public static final int PUBLIC = 0;
|
||||
public static final int DECLARED = 1;
|
||||
|
||||
public Class getDeclaringClass();
|
||||
|
||||
public int getModifiers();
|
||||
|
||||
public String getName();
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package java.lang.reflect;
|
||||
|
||||
public class Method<T> extends AccessibleObject {
|
||||
public class Method<T> extends AccessibleObject implements Member {
|
||||
private byte vmFlags;
|
||||
private byte parameterCount;
|
||||
private short parameterFootprint;
|
||||
@ -20,4 +20,16 @@ public class Method<T> extends AccessibleObject {
|
||||
public void setAccessible(boolean v) {
|
||||
if (v) vmFlags |= Accessible; else vmFlags &= ~Accessible;
|
||||
}
|
||||
|
||||
public Class<T> getDeclaringClass() {
|
||||
return class_;
|
||||
}
|
||||
|
||||
public int getModifiers() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return new String(name, 0, name.length - 1, false);
|
||||
}
|
||||
}
|
||||
|
19
classpath/java/lang/reflect/Modifier.java
Normal file
19
classpath/java/lang/reflect/Modifier.java
Normal file
@ -0,0 +1,19 @@
|
||||
package java.lang.reflect;
|
||||
|
||||
public final class Modifier {
|
||||
public static final int PUBLIC = 1 << 0;
|
||||
public static final int PRIVATE = 1 << 1;
|
||||
public static final int PROTECTED = 1 << 2;
|
||||
public static final int STATIC = 1 << 3;
|
||||
public static final int FINAL = 1 << 4;
|
||||
public static final int SUPER = 1 << 5;
|
||||
public static final int SYNCHRONIZED = SUPER;
|
||||
public static final int VOLATILE = 1 << 6;
|
||||
public static final int TRANSIENT = 1 << 7;
|
||||
public static final int NATIVE = 1 << 8;
|
||||
public static final int INTERFACE = 1 << 9;
|
||||
public static final int ABSTRACT = 1 << 10;
|
||||
public static final int STRICT = 1 << 11;
|
||||
|
||||
private Modifier() { }
|
||||
}
|
220
classpath/java/util/HashMap.java
Normal file
220
classpath/java/util/HashMap.java
Normal file
@ -0,0 +1,220 @@
|
||||
package java.util;
|
||||
|
||||
public class HashMap<K, V> implements Map<K, V> {
|
||||
private int size;
|
||||
private Cell[] array;
|
||||
private Cell<K, V> nullCell;
|
||||
private final CellFactory factory;
|
||||
|
||||
HashMap(int capacity, CellFactory<K, V> factory) {
|
||||
if (capacity > 0) {
|
||||
array = new Cell[nextPowerOfTwo(capacity)];
|
||||
}
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public HashMap(int capacity) {
|
||||
this(capacity, new MyCellFactory());
|
||||
}
|
||||
|
||||
public HashMap() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
private static int nextPowerOfTwo(int n) {
|
||||
int r = 1;
|
||||
while (r < n) r <<= 1;
|
||||
return r;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
private void resize() {
|
||||
if (array == null || size >= array.length * 2) {
|
||||
resize(array == null ? 16 : array.length * 2);
|
||||
} else if (size <= array.length / 3) {
|
||||
resize(array.length / 2);
|
||||
}
|
||||
}
|
||||
|
||||
private void resize(int capacity) {
|
||||
Cell<K, V>[] newArray = null;
|
||||
if (capacity != 0) {
|
||||
capacity = nextPowerOfTwo(capacity);
|
||||
if (array != null && array.length == capacity) {
|
||||
return;
|
||||
}
|
||||
|
||||
newArray = new Cell[capacity];
|
||||
if (array != null) {
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
Cell<K, V> next;
|
||||
for (Cell<K, V> c = array[i]; c != null; c = next) {
|
||||
next = c.next();
|
||||
int index = c.getKey().hashCode() & (capacity - 1);
|
||||
c.setNext(array[index]);
|
||||
array[index] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
private Cell<K, V> find(K key) {
|
||||
if (key == null) {
|
||||
return nullCell;
|
||||
} else {
|
||||
if (array != null) {
|
||||
int index = key.hashCode() & (array.length - 1);
|
||||
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
|
||||
if (key.equals(c.getKey())) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void insert(Cell<K, V> cell) {
|
||||
++ size;
|
||||
|
||||
if (cell.getKey() == null) {
|
||||
nullCell = cell;
|
||||
} else {
|
||||
resize();
|
||||
|
||||
int index = cell.hashCode() & (array.length - 1);
|
||||
cell.setNext(array[index]);
|
||||
array[index] = cell;
|
||||
}
|
||||
}
|
||||
|
||||
// primarily for use by WeakHashMap:
|
||||
void remove(Cell<K, V> cell) {
|
||||
if (cell == nullCell) {
|
||||
nullCell = null;
|
||||
-- size;
|
||||
} else {
|
||||
int index = cell.hashCode() & (array.length - 1);
|
||||
Cell<K, V> p = null;
|
||||
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
|
||||
if (c == cell) {
|
||||
if (p == null) {
|
||||
array[index] = c.next();
|
||||
} else {
|
||||
p.setNext(c.next());
|
||||
}
|
||||
-- size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
resize();
|
||||
}
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
Cell<K, V> c = find(key);
|
||||
return (c == null ? null : c.getValue());
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
Cell<K, V> c = find(key);
|
||||
if (c == null) {
|
||||
insert(factory.make(key, value, null));
|
||||
return null;
|
||||
} else {
|
||||
V old = c.getValue();
|
||||
c.setValue(value);
|
||||
return old;
|
||||
}
|
||||
}
|
||||
|
||||
public V remove(K key) {
|
||||
V old = null;
|
||||
if (key == null) {
|
||||
if (nullCell != null) {
|
||||
old = nullCell.getValue();
|
||||
nullCell = null;
|
||||
-- size;
|
||||
}
|
||||
} else {
|
||||
if (array != null) {
|
||||
int index = key.hashCode() & (array.length - 1);
|
||||
Cell<K, V> p = null;
|
||||
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {
|
||||
if (key.equals(c.getKey())) {
|
||||
old = c.getValue();
|
||||
if (p == null) {
|
||||
array[index] = c.next();
|
||||
} else {
|
||||
p.setNext(c.next());
|
||||
}
|
||||
-- size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
resize();
|
||||
}
|
||||
}
|
||||
return old;
|
||||
}
|
||||
|
||||
interface Cell<K, V> extends Entry<K, V> {
|
||||
public HashMap.Cell<K, V> next();
|
||||
|
||||
public void setNext(HashMap.Cell<K, V> next);
|
||||
}
|
||||
|
||||
interface CellFactory<K, V> {
|
||||
public Cell<K, V> make(K key, V value, Cell<K, V> next);
|
||||
}
|
||||
|
||||
private static class MyCell<K, V> implements Cell<K, V> {
|
||||
public final K key;
|
||||
public V value;
|
||||
public Cell<K, V> next;
|
||||
|
||||
public MyCell(K key, V value, Cell<K, V> next) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(V value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public HashMap.Cell<K, V> next() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(HashMap.Cell<K, V> next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return key.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyCellFactory<K, V> implements CellFactory<K, V> {
|
||||
public Cell<K, V> make(K key, V value, Cell<K, V> next) {
|
||||
return new MyCell(key, value, next);
|
||||
}
|
||||
}
|
||||
}
|
19
classpath/java/util/Map.java
Normal file
19
classpath/java/util/Map.java
Normal file
@ -0,0 +1,19 @@
|
||||
package java.util;
|
||||
|
||||
public interface Map<K, V> {
|
||||
public int size();
|
||||
|
||||
public V get(K key);
|
||||
|
||||
public V put(K key, V value);
|
||||
|
||||
public V remove(K key);
|
||||
|
||||
public interface Entry<K, V> {
|
||||
public K getKey();
|
||||
|
||||
public V getValue();
|
||||
|
||||
public void setValue(V value);
|
||||
}
|
||||
}
|
95
classpath/java/util/WeakHashMap.java
Normal file
95
classpath/java/util/WeakHashMap.java
Normal file
@ -0,0 +1,95 @@
|
||||
package java.util;
|
||||
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class WeakHashMap<K, V> implements Map<K, V> {
|
||||
private final HashMap<K, V> map;
|
||||
private final ReferenceQueue queue;
|
||||
|
||||
public WeakHashMap(int capacity) {
|
||||
map = new HashMap(capacity, new MyCellFactory());
|
||||
queue = new ReferenceQueue();
|
||||
}
|
||||
|
||||
public WeakHashMap() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
private void poll() {
|
||||
for (MyCell<K, V> c = (MyCell<K, V>) queue.poll();
|
||||
c != null;
|
||||
c = (MyCell<K, V>) queue.poll())
|
||||
{
|
||||
map.remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
poll();
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
poll();
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
public V remove(K key) {
|
||||
poll();
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
private static class MyCell<K, V>
|
||||
extends WeakReference<K>
|
||||
implements HashMap.Cell<K, V>
|
||||
{
|
||||
public V value;
|
||||
public HashMap.Cell<K, V> next;
|
||||
public int hashCode;
|
||||
|
||||
public MyCell(K key, V value, HashMap.Cell<K, V> next) {
|
||||
super(key);
|
||||
this.value = value;
|
||||
this.next = next;
|
||||
this.hashCode = key.hashCode();
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return get();
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(V value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public HashMap.Cell<K, V> next() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(HashMap.Cell<K, V> next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyCellFactory<K, V>
|
||||
implements HashMap.CellFactory<K, V>
|
||||
{
|
||||
public HashMap.Cell<K, V> make(K key, V value, HashMap.Cell<K, V> next) {
|
||||
return new MyCell(key, value, next);
|
||||
}
|
||||
}
|
||||
}
|
@ -41,6 +41,12 @@ notifyAll(Thread* t, jobject this_)
|
||||
vm::notifyAll(t, *this_);
|
||||
}
|
||||
|
||||
jobject
|
||||
currentThread(Thread* t)
|
||||
{
|
||||
return pushReference(t, t->javaThread);
|
||||
}
|
||||
|
||||
void
|
||||
sleep(Thread* t, jlong milliseconds)
|
||||
{
|
||||
@ -247,6 +253,8 @@ populate(Thread* t, object map)
|
||||
|
||||
{ "Java_java_lang_Thread_start",
|
||||
reinterpret_cast<void*>(start) },
|
||||
{ "Java_java_lang_Thread_currentThread",
|
||||
reinterpret_cast<void*>(currentThread) },
|
||||
{ "Java_java_lang_Thread_sleep",
|
||||
reinterpret_cast<void*>(sleep) },
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdlib.h"
|
||||
#include "stdarg.h"
|
||||
@ -29,8 +27,6 @@
|
||||
|
||||
inline void* operator new(size_t, void* p) throw() { return p; }
|
||||
|
||||
|
||||
|
||||
namespace vm {
|
||||
|
||||
typedef void* object;
|
||||
|
@ -1382,7 +1382,7 @@ Thread::Thread(Machine* m, Allocator* allocator, object javaThread,
|
||||
|
||||
builtin::populate(t, m->builtinMap);
|
||||
|
||||
javaThread = makeThread(t, 0, reinterpret_cast<int64_t>(t));
|
||||
javaThread = makeThread(t, 0, 0, reinterpret_cast<int64_t>(t));
|
||||
} else {
|
||||
threadPeer(this, javaThread) = reinterpret_cast<jlong>(this);
|
||||
parent->child = this;
|
||||
@ -1749,6 +1749,10 @@ hashMapResize(Thread* t, object map, uint32_t (*hash)(Thread*, object),
|
||||
PROTECT(t, oldArray);
|
||||
|
||||
unsigned newLength = nextPowerOfTwo(size);
|
||||
if (oldArray and arrayLength(t, oldArray) == newLength) {
|
||||
return;
|
||||
}
|
||||
|
||||
newArray = makeArray(t, newLength, true);
|
||||
|
||||
if (oldArray) {
|
||||
@ -1832,6 +1836,7 @@ hashMapRemove(Thread* t, object map, object key,
|
||||
o = tripleSecond(t, *n);
|
||||
set(t, *n, tripleThird(t, *n));
|
||||
-- hashMapSize(t, map);
|
||||
break;
|
||||
} else {
|
||||
n = &tripleThird(t, *n);
|
||||
}
|
||||
|
@ -127,6 +127,7 @@
|
||||
(type thread java/lang/Thread
|
||||
(extends jobject)
|
||||
(object task)
|
||||
(object locals)
|
||||
(int64_t peer))
|
||||
|
||||
(type stackTraceElement java/lang/StackTraceElement
|
||||
|
Loading…
Reference in New Issue
Block a user