Skip to content

Commit e44e90e

Browse files
Make fields/methods always use fast calls
1 parent 0b2391e commit e44e90e

File tree

14 files changed

+132
-384
lines changed

14 files changed

+132
-384
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
library_version=1.0.0b1
1+
library_version=1.0.0b2

src/main/kotlin/dev/thecodewarrior/mirror/member/ConstructorMirror.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,17 @@ public class ConstructorMirror internal constructor(
4646
}
4747

4848
/**
49-
* Create a new instance using this constructor. If performance is of the essence use [callFast], which should be
50-
* near-native speed, but will provide somewhat less helpful exceptions.
49+
* Create a new instance using this constructor. After the one-time cost of creating the
50+
* [MethodHandle][java.lang.invoke.MethodHandle], the access should be near-native speed.
5151
*/
5252
@Suppress("UNCHECKED_CAST")
5353
public fun <T : Any?> call(vararg args: Any?): T {
54-
if(args.size != parameters.size)
55-
throw IllegalArgumentException("Incorrect argument count (${args.size}) for constructor `$this`")
5654
return raw.wrapper(args as Array<Any?>) as T
5755
}
5856

5957
@JvmSynthetic
6058
public operator fun <T> invoke(vararg args: Any?): T = call(*args)
6159

62-
/**
63-
* Create a new instance using this constructor. After the one-time cost of creating the
64-
* [MethodHandle][java.lang.invoke.MethodHandle], the access should be near-native speed. This method, while faster
65-
* than [call], will provide somewhat less helpful exceptions.
66-
*/
67-
@Suppress("UNCHECKED_CAST")
68-
public fun <T : Any?> callFast(vararg args: Any?): T {
69-
return raw.wrapper(args as Array<Any?>) as T
70-
}
71-
7260
override fun toString(): String {
7361
var str = ""
7462
if(access != Modifier.Access.DEFAULT) {

src/main/kotlin/dev/thecodewarrior/mirror/member/FieldMirror.kt

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -125,33 +125,13 @@ public class FieldMirror internal constructor(
125125
MethodHandleHelper.wrapperForStaticGetter(java)
126126
}
127127

128-
/**
129-
* Get the value of this field in the passed instance. If this is a static field, `null` should be used for the
130-
* instance. If performance is of the essence however, use [getFast], which should be near-native speed, but will
131-
* have somewhat less helpful exceptions.
132-
*/
133-
@Suppress("UNCHECKED_CAST")
134-
public fun <T : Any?> get(receiver: Any?): T {
135-
if(isStatic) {
136-
if(receiver != null)
137-
throw IllegalArgumentException("Invalid receiver for static field `${declaringClass.java.simpleName}.$name`. Expected null.")
138-
return raw.staticGetWrapper() as T
139-
} else {
140-
if (receiver == null)
141-
throw NullPointerException("Null receiver for instance field `${declaringClass.java.simpleName}.$name`")
142-
if(!declaringClass.java.isAssignableFrom(receiver.javaClass))
143-
throw IllegalArgumentException("Invalid receiver type `${receiver.javaClass.simpleName}` for instance field `${declaringClass.java.simpleName}.$name`")
144-
return raw.instanceGetWrapper(receiver) as T
145-
}
146-
}
147-
148128
/**
149129
* Get the value of this field in the passed instance. If this is a static field, `null` should be used for the
150130
* instance. After the one-time cost of creating the [MethodHandle][java.lang.invoke.MethodHandle], the access should
151-
* be near-native speed. This method, while faster than [get], will provide somewhat less helpful exceptions.
131+
* be near-native speed.
152132
*/
153133
@Suppress("UNCHECKED_CAST")
154-
public fun <T : Any?> getFast(receiver: Any?): T {
134+
public fun <T : Any?> get(receiver: Any?): T {
155135
if(isStatic) {
156136
return raw.staticGetWrapper() as T
157137
} else {
@@ -168,33 +148,13 @@ public class FieldMirror internal constructor(
168148
MethodHandleHelper.wrapperForStaticSetter(java)
169149
}
170150

171-
/**
172-
* Set the value of this field in the passed instance. If this is a static field, `null` should be used for the
173-
* instance. If performance is of the essence however, use [setFast], which should be near-native speed, but will
174-
* have somewhat less helpful exceptions.
175-
*/
176-
@Suppress("UNCHECKED_CAST")
177-
public fun set(receiver: Any?, value: Any?) {
178-
if(isStatic) {
179-
if(receiver != null)
180-
throw IllegalArgumentException("Invalid receiver for static field `${declaringClass.java.simpleName}.$name`. Expected null.")
181-
raw.staticSetWrapper(value)
182-
} else {
183-
if (receiver == null)
184-
throw NullPointerException("Null receiver for instance field `${declaringClass.java.simpleName}.$name`")
185-
if(!declaringClass.java.isAssignableFrom(receiver.javaClass))
186-
throw IllegalArgumentException("Invalid receiver type `${receiver.javaClass.simpleName}` for instance field `${declaringClass.java.simpleName}.$name`")
187-
raw.instanceSetWrapper(receiver, value)
188-
}
189-
}
190-
191151
/**
192152
* Set the value of this field in the passed instance. If this is a static field, `null` should be used for the
193153
* instance. After the one-time cost of creating the [MethodHandle][java.lang.invoke.MethodHandle], the access
194-
* should be near-native speed. This method, while faster than [set], will provide somewhat less helpful exceptions.
154+
* should be near-native speed.
195155
*/
196156
@Suppress("UNCHECKED_CAST")
197-
public fun setFast(receiver: Any?, value: Any?) {
157+
public fun set(receiver: Any?, value: Any?) {
198158
if(isStatic) {
199159
raw.staticSetWrapper(value)
200160
} else {

src/main/kotlin/dev/thecodewarrior/mirror/member/MemberMirror.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class MemberMirror internal constructor(
4242
*/
4343
public abstract fun withDeclaringClass(enclosing: ClassMirror?): MemberMirror
4444

45-
override fun <T: Annotation?> getAnnotation(annotationClass: Class<T>): T {
45+
override fun <T: Annotation> getAnnotation(annotationClass: Class<T>): T? {
4646
return annotatedElement.getAnnotation(annotationClass)
4747
}
4848

src/main/kotlin/dev/thecodewarrior/mirror/member/MethodMirror.kt

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -175,49 +175,23 @@ public class MethodMirror internal constructor(
175175
}
176176

177177
/**
178-
* Call this method on the passed instance. If this is a static method, `null` should be used for the instance. If
179-
* performance is of the essence use [callFast], which should be near-native speed, but will provide somewhat less
180-
* helpful exceptions.
178+
* Call this method on the passed instance. If this is a static method, `null` should be used for the instance.
179+
* After the one-time cost of creating the [MethodHandle][java.lang.invoke.MethodHandle], the access should be
180+
* near-native speed.
181181
*/
182182
@Suppress("UNCHECKED_CAST")
183183
@Throws(Throwable::class)
184184
public fun <T> call(receiver: Any?, vararg args: Any?): T {
185185
if(isStatic) {
186-
if(receiver != null)
187-
throw IllegalArgumentException("Invalid receiver for static method `${declaringClass.java.simpleName}.$name`. Expected null.")
188-
if(args.size != parameters.size)
189-
throw IllegalArgumentException("Incorrect argument count (${args.size}) for static method `$this`")
190-
191186
return raw.staticWrapper(args as Array<Any?>) as T
192187
} else {
193-
if(receiver == null)
194-
throw NullPointerException("Null receiver for instance method `${declaringClass.java.simpleName}.$name`")
195-
if(!declaringClass.java.isAssignableFrom(receiver.javaClass))
196-
throw IllegalArgumentException("Invalid receiver type `${receiver.javaClass.simpleName}` for instance method `${declaringClass.java.simpleName}.$name`")
197-
if(args.size != parameters.size)
198-
throw IllegalArgumentException("Incorrect argument count (${args.size}) for instance method `$this`")
199-
200-
return raw.instanceWrapper(receiver, args as Array<Any?>) as T
188+
return raw.instanceWrapper(receiver!!, args as Array<Any?>) as T
201189
}
202190
}
203191

204192
@JvmSynthetic
205193
public operator fun <T> invoke(receiver: Any?, vararg args: Any?): T = call(receiver, *args)
206194

207-
/**
208-
* Call this method on the passed instance. If this is a static method, `null` should be used for the instance.
209-
* After the one-time cost of creating the [MethodHandle][java.lang.invoke.MethodHandle], the access should be
210-
* near-native speed. This method, while faster than [call], will provide somewhat less helpful exceptions.
211-
*/
212-
@Suppress("UNCHECKED_CAST")
213-
public fun <T : Any?> callFast(receiver: Any?, vararg args: Any?): T {
214-
if(isStatic) {
215-
return raw.staticWrapper(args as Array<Any?>) as T
216-
} else {
217-
return raw.instanceWrapper(receiver!!, args as Array<Any?>) as T
218-
}
219-
}
220-
221195
override fun toString(): String {
222196
var str = ""
223197
str += modifiers.joinToString("") { "$it " }

src/test/kotlin/dev/thecodewarrior/mirror/methodhandles/Constructors.kt

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -64,59 +64,4 @@ internal class Constructors: MTest() {
6464
Mirror.reflect(X._constructor()).call<Any>(0)
6565
}
6666
}
67-
68-
@Test
69-
fun `fast calling a constructor should correctly return an instance`() {
70-
val X by sources.add("X", "class X { int field; X() { field = 20; } }")
71-
sources.compile()
72-
val instance = Mirror.reflect(X._constructor()).callFast<Any>()
73-
assertEquals(X, instance.javaClass)
74-
assertEquals(20, instance._get<Any>("field"))
75-
}
76-
77-
@Test
78-
fun `fast calling a private constructor should not throw`() {
79-
val X by sources.add("X", "class X { int field; private X() { field = 20; } }")
80-
sources.compile()
81-
assertDoesNotThrow {
82-
Mirror.reflect(X._constructor()).callFast<Any>()
83-
}
84-
}
85-
86-
@Test
87-
fun `fast calling a constructor with arguments should pass them`() {
88-
val X by sources.add("X", "class X { int field; X(int param) { field = param; } }")
89-
sources.compile()
90-
val instance = Mirror.reflect(X._constructor()).callFast<Any>(20)
91-
assertEquals(X, instance.javaClass)
92-
assertEquals(20, instance._get<Any>("field"))
93-
}
94-
95-
@Test
96-
fun `fast calling a constructor with arguments and no parameters should not throw`() {
97-
val X by sources.add("X", "class X { X() { } }")
98-
sources.compile()
99-
// for some reason passing arguments to a zero-arg method is fine, but not constructors?
100-
assertThrows<IllegalArgumentException> {
101-
Mirror.reflect(X._constructor()).callFast<Any>(0)
102-
}
103-
}
104-
105-
@Test
106-
fun `fast calling a constructor with too many arguments should throw`() {
107-
val X by sources.add("X", "class X { X(int a) { } }")
108-
sources.compile()
109-
assertThrows<IllegalArgumentException> {
110-
Mirror.reflect(X._constructor()).callFast<Any>(0, 1)
111-
}
112-
}
113-
114-
@Test
115-
fun `fast calling a constructor with too few arguments should throw`() {
116-
val X by sources.add("X", "class X { X(int a, int b) { } }")
117-
sources.compile()
118-
assertThrows<IllegalArgumentException> {
119-
Mirror.reflect(X._constructor()).callFast<Any>(0)
120-
}
121-
}
12267
}

src/test/kotlin/dev/thecodewarrior/mirror/methodhandles/FastCall.kt

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)