Skip to content

Commit adf2a39

Browse files
committed
Leave ExposedType annotations on processed types so they'll be visible at
runtime. Use that to detect exposed inner classes in classes being loaded. Inner classes being loaded won't have set their builder in PyType yet, so they need to be added to BOOTSTRAP_TYPES so they're created as PyType instead of PyJavaType.
1 parent 8ff1a08 commit adf2a39

5 files changed

Lines changed: 36 additions & 13 deletions

File tree

src/org/python/core/PyJavaType.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import org.python.core.util.StringUtil;
1818
import org.python.expose.ExposeAsSuperclass;
19+
import org.python.expose.ExposedType;
1920
import org.python.util.Generic;
2021

2122
public class PyJavaType extends PyType implements ExposeAsSuperclass {
@@ -245,8 +246,17 @@ public PyObject new_impl(boolean init,
245246
dict.__setitem__("__init__", reflctr);
246247
}
247248
for (Class<?> inner : underlying_class.getClasses()) {
248-
// Only add the class if there isn't something else with that name
249-
if (dict.__finditem__(inner.getSimpleName()) == null) {
249+
// Only add the class if there isn't something else with that name and it came from this
250+
// class
251+
if (inner.getDeclaringClass() == underlying_class &&
252+
dict.__finditem__(inner.getSimpleName()) == null) {
253+
// If this class is currently being loaded, any exposed types it contains won't have
254+
// set their builder in PyType yet, so add them to BOOTSTRAP_TYPES so they're
255+
// created as PyType instead of PyJavaType
256+
if (inner.getAnnotation(ExposedType.class) != null
257+
|| ExposeAsSuperclass.class.isAssignableFrom(inner)) {
258+
Py.BOOTSTRAP_TYPES.add(inner);
259+
}
250260
dict.__setitem__(inner.getSimpleName(), PyType.fromClass(inner));
251261
}
252262
}

src/org/python/expose/generate/ExposedTypeProcessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ public void visit(int version,
130130

131131
@Override
132132
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
133+
AnnotationVisitor visitor = super.visitAnnotation(desc, visible);
133134
if(desc.equals(EXPOSED_TYPE.getDescriptor())) {
134-
return new ExposedTypeVisitor(onType) {
135+
return new ExposedTypeVisitor(onType, visitor) {
135136

136137
@Override
137138
public void handleResult(String name) {
@@ -148,7 +149,7 @@ public void handleResult(boolean boolIsBaseType) {
148149
}
149150
};
150151
}
151-
return super.visitAnnotation(desc, visible);
152+
return visitor;
152153
}
153154

154155
private void throwInvalid(String msg) {

src/org/python/expose/generate/ExposedTypeVisitor.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.python.expose.generate;
22

3+
import org.python.objectweb.asm.AnnotationVisitor;
34
import org.python.objectweb.asm.Type;
45

56
/**
@@ -15,8 +16,11 @@ public abstract class ExposedTypeVisitor extends RestrictiveAnnotationVisitor {
1516

1617
private boolean isBaseType = true;
1718

18-
public ExposedTypeVisitor(Type onType) {
19+
private final AnnotationVisitor passthrough;
20+
21+
public ExposedTypeVisitor(Type onType, AnnotationVisitor passthrough) {
1922
this.onType = onType;
23+
this.passthrough = passthrough;
2024
}
2125

2226
@Override
@@ -30,6 +34,9 @@ public void visit(String name, Object value) {
3034
} else {
3135
super.visit(name, value);
3236
}
37+
if (passthrough != null) {
38+
passthrough.visit(name, value);
39+
}
3340
}
3441

3542
@Override
@@ -41,6 +48,9 @@ public void visitEnd() {
4148
handleResult(typeName);
4249
handleResult(base);
4350
handleResult(isBaseType);
51+
if (passthrough != null) {
52+
passthrough.visitEnd();
53+
}
4454
}
4555

4656
public abstract void handleResult(Type base);

src/org/python/modules/_hashlib.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,18 @@ public static class Hash extends PyObject {
121121
put("sha-512", 128);
122122
}};
123123

124-
public Hash(String name) {
125-
super(TYPE);
126-
this.name = name;
124+
private static final MessageDigest getDigest(String name) {
127125
try {
128-
digest = MessageDigest.getInstance(name);
126+
return MessageDigest.getInstance(name);
129127
} catch (NoSuchAlgorithmException nsae) {
130128
throw Py.ValueError("unsupported hash type");
131129
}
132130
}
133131

132+
public Hash(String name) {
133+
this(name, getDigest(name));
134+
}
135+
134136
private Hash(String name, MessageDigest digest) {
135137
super(TYPE);
136138
this.name = name;
@@ -192,10 +194,10 @@ final PyObject HASH_hexdigest() {
192194
// Make hex version of the digest
193195
char[] hexDigest = new char[result.length * 2];
194196
for (int i = 0, j = 0; i < result.length; i++) {
195-
int c = (int)((result[i] >> 4) & 0xf);
197+
int c = ((result[i] >> 4) & 0xf);
196198
c = c > 9 ? c + 'a' - 10 : c + '0';
197199
hexDigest[j++] = (char)c;
198-
c = (int)result[i] & 0xf;
200+
c = result[i] & 0xf;
199201
c = c > 9 ? c + 'a' - 10 : c + '0';
200202
hexDigest[j++] = (char)c;
201203
}

tests/java/org/python/expose/generate/ExposedTypeVisitorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class ExposedTypeVisitorTest extends TestCase {
1010

1111
public void setUp() {
12-
etv = new ExposedTypeVisitor(Type.getType("Lsimpletype;")) {
12+
etv = new ExposedTypeVisitor(Type.getType("Lsimpletype;"), null) {
1313

1414
@Override
1515
public void handleResult(String name) {
@@ -47,7 +47,7 @@ public void testNamedType() {
4747
ExposedTypeVisitor etv;
4848

4949
private String result;
50-
50+
5151
private Type baseResult;
5252

5353
private boolean isBaseTypeResult;

0 commit comments

Comments
 (0)