Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bytecode version unspecified & NPE and trySetAccessible method does not exist on Android #1636

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions rhino/src/main/java/org/mozilla/classfile/ClassFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4367,21 +4367,31 @@ private void finalizeSuperBlockStarts() {
// Based on the version numbers we scrape, we can also determine what
// bytecode features we need. For example, Java 6 bytecode (classfile
// version 50) should have stack maps generated.
int minor = 0;
int major = 48;
try (InputStream is = readClassFile()) {
byte[] header = new byte[8];
// read loop is required since JDK7 will only provide 2 bytes
// on the first read() - see bug #630111
int read = 0;
while (read < 8) {
int c = is.read(header, read, 8 - read);
if (c < 0) throw new IOException();
read += c;
if (is != null) {
byte[] header = new byte[8];
// read loop is required since JDK7 will only provide 2 bytes
// on the first read() - see bug #630111
int read = 0;
while (read < 8) {
int c = is.read(header, read, 8 - read);
if (c < 0) throw new IOException();
read += c;
}
minor = (header[4] << 8) | (header[5] & 0xff);
major = (header[6] << 8) | (header[7] & 0xff);
} else {
System.err.println(
"Warning: Unable to read ClassFileWriter.class, using default bytecode version");
}
MinorVersion = (header[4] << 8) | (header[5] & 0xff);
MajorVersion = (header[6] << 8) | (header[7] & 0xff);
GenerateStackMap = MajorVersion >= 50;
} catch (IOException ioe) {
throw new AssertionError("Can't read ClassFileWriter.class to get bytecode version");
} finally {
MinorVersion = minor;
MajorVersion = major;
GenerateStackMap = MajorVersion >= 50;
}
}

Expand Down
5 changes: 3 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ private void discoverAccessibleMethods(
if (isPublic(mods) || isProtected(mods) || includePrivate) {
MethodSignature sig = new MethodSignature(method);
if (!map.containsKey(sig)) {
if (includePrivate) method.trySetAccessible();
if (includePrivate && !method.isAccessible())
method.setAccessible(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stumbled upon this place in #1575 (which probably will cause merge conflicts)
I'm wondering, if we should replace this with VMBridge.instance.tryToMakeAccessible(method)
which will effectively call this code: https://github.com/mozilla/rhino/blob/master/rhino/src/main/java/org/mozilla/javascript/jdk18/VMBridge_jdk18.java#L57

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that's a good idea. I guess it will work on Android too.

map.put(sig, method);
}
}
Expand Down Expand Up @@ -667,7 +668,7 @@ private Field[] getAccessibleFields(boolean includeProtected, boolean includePri
for (Field field : declared) {
int mod = field.getModifiers();
if (includePrivate || isPublic(mod) || isProtected(mod)) {
field.trySetAccessible();
if (!field.isAccessible()) field.setAccessible(true);
fieldsList.add(field);
}
}
Expand Down
Loading