Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1479)

Unified Diff: build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java

Issue 2971063003: Handle Java assert using a customized function in base (Closed)
Patch Set: just refactor, do not report exception Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java
diff --git a/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java b/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java
index beb26facda44e40dc31ec9ced6e18415803a29c9..063ccd6491508cf19707ee333c717f13b1f9ebfb 100644
--- a/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java
+++ b/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java
@@ -26,15 +26,37 @@ import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
- * An application that enables Java ASSERT statements by modifying Java bytecode. It takes in a JAR
- * file, modifies bytecode of classes that use ASSERT, and outputs the bytecode to a new JAR file.
+ * An application that replace Java ASSERT statements with a function by modifying Java bytecode. It
+ * takes in a JAR file, modifies bytecode of classes that use ASSERT, and outputs the bytecode to a
+ * new JAR file.
+ * First enable assert:
+ * The following bytecode is generated for each class with ASSERT statements:
+ * 0: ldc #8 // class CLASSNAME
+ * 2: invokevirtual #9 // Method java/lang/Class.desiredAssertionStatus:()Z
+ * 5: ifne 12
+ * 8: iconst_1
+ * 9: goto 13
+ * 12: iconst_0
+ * 13: putstatic #2 // Field $assertionsDisabled:Z
+ * Replaces line #13 to the following:
+ * 13: pop
+ * Consequently, $assertionsDisabled is assigned the default value FALSE.
+ *
+ * To replace assert statement with a function:
+ * The followed instructions are generated by a java assert statement:
+ * getstatic #3 // Field $assertionsDisabled:Z
+ * ...
+ * ifne 19
+ * new #4 // class java/lang/AssertionError
+ * dup
+ * ldc #5 // String (don't have this line if no assert message given)
+ * invokespecial #6 // Method java/lang/AssertionError.
+ * athrow
+ * We replace athrow with a call to a function that handles the AssertionError.
*/
class AssertionEnabler {
- static final String ASSERTION_DISABLED_NAME = "$assertionsDisabled";
static final String CLASS_FILE_SUFFIX = ".class";
- static final String STATIC_INITIALIZER_NAME = "<clinit>";
static final String TEMPORARY_FILE_SUFFIX = ".temp";
-
static final int BUFFER_SIZE = 16384;
static class AssertionEnablerVisitor extends ClassVisitor {
@@ -45,33 +67,45 @@ class AssertionEnabler {
@Override
public MethodVisitor visitMethod(final int access, final String name, String desc,
String signature, String[] exceptions) {
- // Patch static initializer.
- if ((access & Opcodes.ACC_STATIC) != 0 && name.equals(STATIC_INITIALIZER_NAME)) {
- return new MethodVisitor(Opcodes.ASM5,
- super.visitMethod(access, name, desc, signature, exceptions)) {
- // The following bytecode is generated for each class with ASSERT statements:
- // 0: ldc #8 // class CLASSNAME
- // 2: invokevirtual #9 // Method java/lang/Class.desiredAssertionStatus:()Z
- // 5: ifne 12
- // 8: iconst_1
- // 9: goto 13
- // 12: iconst_0
- // 13: putstatic #2 // Field $assertionsDisabled:Z
- //
- // This function replaces line #13 to the following:
- // 13: pop
- // Consequently, $assertionsDisabled is assigned the default value FALSE.
- @Override
- public void visitFieldInsn(int opcode, String owner, String name, String desc) {
- if (opcode == Opcodes.PUTSTATIC && name.equals(ASSERTION_DISABLED_NAME)) {
- mv.visitInsn(Opcodes.POP);
- } else {
- super.visitFieldInsn(opcode, owner, name, desc);
- }
- }
- };
+ return new RewriteAssertMethodVisitorWriter(
+ Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {};
+ }
+ }
+
+ static class RewriteAssertMethodVisitorWriter extends MethodVisitor {
+ static final String ASSERTION_DISABLED_NAME = "$assertionsDisabled";
+ static final String INSERT_INSTRUCTION_OWNER = "org/chromium/base/JavaExceptionReporter";
+ static final String INSERT_INSTRUCTION_NAME = "assertFailureHandler";
+ static final String INSERT_INSTRUCTION_DESC = "(Ljava/lang/AssertionError;)V";
+ static final boolean INSERT_INSTRUCTION_ITF = false;
+
+ boolean mStartLoadingAssert;
+
+ public RewriteAssertMethodVisitorWriter(int api, MethodVisitor mv) {
+ super(api, mv);
+ }
+
+ @Override
+ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
+ if (opcode == Opcodes.PUTSTATIC && name.equals(ASSERTION_DISABLED_NAME)) {
+ super.visitInsn(Opcodes.POP); // enable assert
+ } else if (opcode == Opcodes.GETSTATIC && name.equals(ASSERTION_DISABLED_NAME)) {
+ mStartLoadingAssert = true;
+ super.visitFieldInsn(opcode, owner, name, desc);
+ } else {
+ super.visitFieldInsn(opcode, owner, name, desc);
+ }
+ }
+
+ @Override
+ public void visitInsn(int opcode) {
+ if (!mStartLoadingAssert || opcode != Opcodes.ATHROW) {
+ super.visitInsn(opcode);
+ } else {
+ super.visitMethodInsn(Opcodes.INVOKESTATIC, INSERT_INSTRUCTION_OWNER,
+ INSERT_INSTRUCTION_NAME, INSERT_INSTRUCTION_DESC, INSERT_INSTRUCTION_ITF);
+ mStartLoadingAssert = false;
}
- return super.visitMethod(access, name, desc, signature, exceptions);
}
}
@@ -82,16 +116,15 @@ class AssertionEnabler {
while ((numRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, numRead);
}
-
return buffer.toByteArray();
}
static void enableAssertionInJar(String inputJarPath, String outputJarPath) {
String tempJarPath = outputJarPath + TEMPORARY_FILE_SUFFIX;
try (ZipInputStream inputStream = new ZipInputStream(
- new BufferedInputStream(new FileInputStream(inputJarPath)));
- ZipOutputStream tempStream = new ZipOutputStream(
- new BufferedOutputStream(new FileOutputStream(tempJarPath)))) {
+ new BufferedInputStream(new FileInputStream(inputJarPath)));
+ ZipOutputStream tempStream = new ZipOutputStream(
+ new BufferedOutputStream(new FileOutputStream(tempJarPath)))) {
ZipEntry entry = null;
while ((entry = inputStream.getNextEntry()) != null) {
« build/android/java_assertion_enabler/OWNERS ('K') | « build/android/java_assertion_enabler/OWNERS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698