Skip to content

Commit

Permalink
Add initial support for new Java 9 class file format (JDK-8148785) (#97)
Browse files Browse the repository at this point in the history
Add initial support for new Java 9 class file format (Java 9 b119, see JDK-8148785, JDK-8150011)
  • Loading branch information
uschindler committed May 22, 2016
1 parent de96b66 commit 93a276b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/main/java/de/thetaphi/forbiddenapis/AsmUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@

package de.thetaphi.forbiddenapis;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Locale;
import java.util.regex.Pattern;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;

/** Some static utilities for analyzing with ASM, also constants. */
public final class AsmUtils {

Expand Down Expand Up @@ -146,5 +155,27 @@ public static String getModuleName(URL jrtUrl) {
return null;
}
}

private static void patchClassMajorVersion(byte[] header, int versionFrom, int versionTo) {
final ByteBuffer buf = ByteBuffer.wrap(header).order(ByteOrder.BIG_ENDIAN);
if (buf.getShort(6) == versionFrom) {
buf.putShort(6, (short) versionTo);
}
}

/** Utility method to load class files of Java 9 by patching them, so ASM can read them. */
public static ClassReader readAndPatchClass(InputStream in) throws IOException {
final byte[] b = new byte[8];
final PushbackInputStream pbin = new PushbackInputStream(in, b.length);
for (int upto = 0; upto < b.length;) {
final int read = pbin.read(b, upto, b.length - upto);
if (read == -1)
throw new EOFException("Not enough bytes available to read header of class file.");
upto += read;
}
patchClassMajorVersion(b, Opcodes.V1_8 + 1, Opcodes.V1_8);
pbin.unread(b);
return new ClassReader(pbin);
}

}
6 changes: 3 additions & 3 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private ClassSignature loadClassFromJigsaw(String classname) throws IOException
}

try {
return new ClassSignature(new ClassReader(in), AsmUtils.isRuntimeModule(moduleName), false);
return new ClassSignature(AsmUtils.readAndPatchClass(in), AsmUtils.isRuntimeModule(moduleName), false);
} finally {
in.close();
}
Expand Down Expand Up @@ -297,7 +297,7 @@ private ClassSignature getClassFromClassLoader(final String clazz) throws ClassN
final boolean isRuntimeClass = isRuntimeClass(conn);
final InputStream in = conn.getInputStream();
try {
classpathClassCache.put(clazz, c = new ClassSignature(new ClassReader(in), isRuntimeClass, false));
classpathClassCache.put(clazz, c = new ClassSignature(AsmUtils.readAndPatchClass(in), isRuntimeClass, false));
} finally {
in.close();
}
Expand Down Expand Up @@ -528,7 +528,7 @@ private void parseSignaturesFile(Reader reader, boolean isBundled) throws IOExce
public void addClassToCheck(final InputStream in) throws IOException {
final ClassReader reader;
try {
reader = new ClassReader(in);
reader = AsmUtils.readAndPatchClass(in);
} finally {
in.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected boolean isDeprecated(int access) {
protected void parseClass(InputStream in) throws IOException {
final ClassReader reader;
try {
reader = new ClassReader(in);
reader = AsmUtils.readAndPatchClass(in);
} catch (IllegalArgumentException iae) {
// unfortunately the ASM IAE has no message, so add good info!
throw new IllegalArgumentException("The class file format of your runtime seems to be too recent to be parsed by ASM (may need to be upgraded).");
Expand Down

0 comments on commit 93a276b

Please sign in to comment.