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

Add initial support for new Java 9 class file format (JDK-8148785) #97

Merged
merged 3 commits into from
May 22, 2016
Merged
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
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 @@ -71,7 +71,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