Skip to content

Commit 6225daf

Browse files
authored
Merge pull request #818 from ngmr/refactor/use-method-handles-for-fields
refactor/use method handles for fields
2 parents e8a1b5c + db46c00 commit 6225daf

20 files changed

Lines changed: 1305 additions & 1385 deletions
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2026 IBM Corporation and others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
package org.apache.yoko.rmi.impl;
19+
20+
21+
import org.omg.CORBA.MARSHAL;
22+
23+
import javax.rmi.PortableRemoteObject;
24+
import java.io.IOException;
25+
import java.lang.reflect.Field;
26+
import java.rmi.Remote;
27+
import java.util.Map;
28+
import java.util.logging.Logger;
29+
30+
import static org.apache.yoko.util.Exceptions.as;
31+
32+
class AnyFieldDescriptor extends FieldDescriptor {
33+
static final Logger logger = Logger.getLogger(AnyFieldDescriptor.class
34+
.getName());
35+
36+
boolean narrowValue;
37+
38+
AnyFieldDescriptor(Class<?> owner, Class<?> type, String name, Field f, TypeRepository repository) {
39+
super(owner, type, name, f, repository);
40+
narrowValue = Remote.class.isAssignableFrom(type);
41+
}
42+
43+
public void read(ObjectReader reader, Object obj)
44+
throws IOException {
45+
try {
46+
Object val = reader.readAny();
47+
if (narrowValue && val != null && !type.isInstance(val)) {
48+
try {
49+
val = PortableRemoteObject.narrow(val, this.type);
50+
} catch (SecurityException ex) {
51+
logger.finer(() -> "Narrow failed" + "\n" + ex);
52+
throw ex;
53+
}
54+
} else if (val != null && !type.isInstance(val)) {
55+
throw new MARSHAL("value is instance of "
56+
+ val.getClass().getName() + " -- should be: "
57+
+ type.getName());
58+
}
59+
60+
setFieldContents(obj, val);
61+
} catch (IllegalStateException ex) {
62+
throw as(MARSHAL::new, ex, ex.getMessage());
63+
}
64+
}
65+
66+
public void write(ObjectWriter writer, Object obj)
67+
throws IOException {
68+
writer.writeAny(getFieldContents(obj));
69+
}
70+
71+
void copyState(final Object orig, final Object copy, CopyState state) {
72+
try {
73+
setFieldContents(copy, state.copy(getFieldContents(orig)));
74+
} catch (CopyRecursionException e) {
75+
state.registerRecursion(new CopyRecursionResolver(orig) {
76+
public void resolve(Object value) {
77+
try {
78+
setFieldContents(copy, value);
79+
} catch (IOException ex) {
80+
throw as(InternalError::new, ex, ex.getMessage());
81+
}
82+
}
83+
});
84+
} catch (IOException ex) {
85+
throw as(InternalError::new, ex, ex.getMessage());
86+
}
87+
}
88+
89+
/**
90+
* @see FieldDescriptor#readFieldIntoMap(ObjectReader, Map)
91+
*/
92+
void readFieldIntoMap(ObjectReader reader, Map<String, Object> map) {
93+
Object value = reader.readAny();
94+
map.put(java_name, value);
95+
}
96+
97+
/**
98+
* @see FieldDescriptor#writeFieldFromMap(ObjectWriter, Map)
99+
*/
100+
void writeFieldFromMap(ObjectWriter writer, Map<String, Object> map) throws IOException {
101+
Object value = map.get(java_name);
102+
writer.writeAny(value);
103+
}
104+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2026 IBM Corporation and others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
package org.apache.yoko.rmi.impl;
19+
20+
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
import java.lang.reflect.Field;
24+
import java.util.Map;
25+
26+
import static org.apache.yoko.util.Exceptions.as;
27+
28+
class BooleanFieldDescriptor extends FieldDescriptor {
29+
BooleanFieldDescriptor(Class<?> owner, Class<?> type, String name, Field f, TypeRepository repository) {
30+
super(owner, type, name, f, repository);
31+
}
32+
33+
public void read(ObjectReader reader, Object obj) throws IOException {
34+
boolean value = reader.readBoolean();
35+
setFieldContents(obj, value);
36+
}
37+
38+
public void write(ObjectWriter writer, Object obj) throws IOException {
39+
writer.writeBoolean((Boolean) getFieldContents(obj));
40+
}
41+
42+
void copyState(Object orig, Object copy, CopyState state) {
43+
try {
44+
setFieldContents(copy, getFieldContents(orig));
45+
} catch (IOException ex) {
46+
throw as(InternalError::new, ex, ex.getMessage());
47+
}
48+
}
49+
50+
void print(PrintWriter pw, Map<Object, Integer> recurse, Object val) {
51+
try {
52+
pw.print(java_name);
53+
pw.print("=");
54+
pw.print(getFieldContents(val));
55+
} catch (IllegalStateException | IOException ex) {
56+
pw.print("<non-local>");
57+
}
58+
}
59+
60+
/**
61+
* @see FieldDescriptor#readFieldIntoMap(ObjectReader, Map)
62+
*/
63+
void readFieldIntoMap(ObjectReader reader, Map<String, Object> map) throws IOException {
64+
map.put(java_name, reader.readBoolean());
65+
}
66+
67+
/**
68+
* @see FieldDescriptor#writeFieldFromMap(ObjectWriter, Map)
69+
*/
70+
void writeFieldFromMap(ObjectWriter writer, Map<String, Object> map) throws IOException {
71+
Boolean value = (Boolean) map.get(java_name);
72+
if (value == null) {
73+
writer.writeBoolean(false);
74+
} else {
75+
writer.writeBoolean(value);
76+
}
77+
}
78+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2026 IBM Corporation and others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
package org.apache.yoko.rmi.impl;
19+
20+
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
import java.lang.reflect.Field;
24+
import java.util.Map;
25+
26+
import static org.apache.yoko.util.Exceptions.as;
27+
28+
class ByteFieldDescriptor extends FieldDescriptor {
29+
ByteFieldDescriptor(Class<?> owner, Class<?> type, String name, Field f, TypeRepository repository) {
30+
super(owner, type, name, f, repository);
31+
}
32+
33+
public void read(ObjectReader reader, Object obj) throws IOException {
34+
byte value = reader.readByte();
35+
setFieldContents(obj, value);
36+
}
37+
38+
public void write(ObjectWriter writer, Object obj) throws IOException {
39+
writer.writeByte((Byte) getFieldContents(obj));
40+
}
41+
42+
void copyState(Object orig, Object copy, CopyState state) {
43+
try {
44+
setFieldContents(copy, getFieldContents(orig));
45+
} catch (IOException ex) {
46+
throw as(InternalError::new, ex, ex.getMessage());
47+
}
48+
}
49+
50+
void print(PrintWriter pw, Map<Object, Integer> recurse, Object val) {
51+
try {
52+
pw.print(java_name);
53+
pw.print("=");
54+
pw.print(getFieldContents(val));
55+
} catch (IllegalStateException | IOException ex) {
56+
pw.print("<non-local>");
57+
}
58+
}
59+
60+
/**
61+
* @see FieldDescriptor#readFieldIntoMap(ObjectReader, Map)
62+
*/
63+
void readFieldIntoMap(ObjectReader reader, Map<String, Object> map) throws IOException {
64+
map.put(java_name, reader.readByte());
65+
}
66+
67+
/**
68+
* @see FieldDescriptor#writeFieldFromMap(ObjectWriter, Map)
69+
*/
70+
void writeFieldFromMap(ObjectWriter writer, Map<String, Object> map) throws IOException {
71+
Byte value = (Byte) map.get(java_name);
72+
if (value == null) {
73+
writer.writeByte(0);
74+
} else {
75+
writer.writeByte(value);
76+
}
77+
}
78+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2026 IBM Corporation and others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
package org.apache.yoko.rmi.impl;
19+
20+
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
import java.lang.reflect.Field;
24+
import java.util.Map;
25+
26+
import static org.apache.yoko.util.Exceptions.as;
27+
28+
class CharFieldDescriptor extends FieldDescriptor {
29+
CharFieldDescriptor(Class<?> owner, Class<?> type, String name, Field f, TypeRepository repository) {
30+
super(owner, type, name, f, repository);
31+
}
32+
33+
public void read(ObjectReader reader, Object obj) throws IOException {
34+
char value = reader.readChar();
35+
setFieldContents(obj, value);
36+
}
37+
38+
public void write(ObjectWriter writer, Object obj) throws IOException {
39+
writer.writeChar((Character) getFieldContents(obj));
40+
}
41+
42+
void copyState(Object orig, Object copy, CopyState state) {
43+
try {
44+
setFieldContents(copy, getFieldContents(orig));
45+
} catch (IOException ex) {
46+
throw as(InternalError::new, ex, ex.getMessage());
47+
}
48+
}
49+
50+
void print(PrintWriter pw, Map<Object, Integer> recurse, Object val) {
51+
try {
52+
pw.print(java_name);
53+
pw.print("=");
54+
char ch = (Character) getFieldContents(val);
55+
pw.print(ch);
56+
pw.print('(');
57+
pw.print(Integer.toHexString(0xffff & ((int) ch)));
58+
pw.print(')');
59+
} catch (IllegalStateException | IOException ex) {
60+
pw.print("<non-local>");
61+
}
62+
}
63+
64+
/**
65+
* @see FieldDescriptor#readFieldIntoMap(ObjectReader, Map)
66+
*/
67+
void readFieldIntoMap(ObjectReader reader, Map<String, Object> map) throws IOException {
68+
map.put(java_name, reader.readChar());
69+
}
70+
71+
/**
72+
* @see FieldDescriptor#writeFieldFromMap(ObjectWriter, Map)
73+
*/
74+
void writeFieldFromMap(ObjectWriter writer, Map<String, Object> map) throws IOException {
75+
Character value = (Character) map.get(java_name);
76+
if (value == null) {
77+
writer.writeChar(0);
78+
} else {
79+
writer.writeChar(value);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)