diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2018-10-03 21:16:27 +0200 |
---|---|---|
committer | Evgeny Mandrikov <Godin@users.noreply.github.com> | 2018-10-03 21:16:27 +0200 |
commit | 01b961b755bd47164739896d2d9c0960e882fd40 (patch) | |
tree | d0e9a32f95837032d3c14a326a84a76435be1a6e /org.jacoco.core | |
parent | 9dfd348cc084a469e41623ee335aaaace97692e3 (diff) | |
download | jacoco-01b961b755bd47164739896d2d9c0960e882fd40.tar.gz |
No need to modify class bytes for Java 10 support (#740)
Diffstat (limited to 'org.jacoco.core')
7 files changed, 24 insertions, 105 deletions
diff --git a/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java b/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java index 92d640d2..01ffd5d3 100644 --- a/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java +++ b/org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java @@ -22,7 +22,6 @@ import java.util.zip.ZipInputStream; import org.jacoco.core.data.ExecutionData; import org.jacoco.core.data.ExecutionDataStore; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.ContentTypeDetector; import org.jacoco.core.internal.InputStreams; import org.jacoco.core.internal.Pack200Streams; @@ -113,9 +112,7 @@ public class Analyzer { private void analyzeClass(final byte[] source) { final long classId = CRC64.classId(source); - final int version = BytecodeVersion.get(source); - final byte[] b = BytecodeVersion.downgradeIfNeeded(version, source); - final ClassReader reader = new ClassReader(b); + final ClassReader reader = new ClassReader(source); if ((reader.getAccess() & Opcodes.ACC_SYNTHETIC) != 0) { return; } diff --git a/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java b/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java index da956fd9..1b18f908 100644 --- a/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java +++ b/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java @@ -21,7 +21,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.ContentTypeDetector; import org.jacoco.core.internal.InputStreams; import org.jacoco.core.internal.Pack200Streams; @@ -84,10 +83,7 @@ public class Instrumenter { private byte[] instrument(final byte[] source) { final long classId = CRC64.classId(source); - final int originalVersion = BytecodeVersion.get(source); - final byte[] b = BytecodeVersion.downgradeIfNeeded(originalVersion, - source); - final ClassReader reader = new ClassReader(b); + final ClassReader reader = new ClassReader(source); final ClassWriter writer = new ClassWriter(reader, 0) { @Override protected String getCommonSuperClass(final String type1, @@ -97,13 +93,12 @@ public class Instrumenter { }; final IProbeArrayStrategy strategy = ProbeArrayStrategyFactory .createFor(classId, reader, accessorGenerator); + final int version = InstrSupport.getVersionMajor(source); final ClassVisitor visitor = new ClassProbesAdapter( new ClassInstrumenter(strategy, writer), - InstrSupport.needsFrames(originalVersion)); + InstrSupport.needsFrames(version)); reader.accept(visitor, ClassReader.EXPAND_FRAMES); - final byte[] instrumented = writer.toByteArray(); - BytecodeVersion.set(instrumented, originalVersion); - return instrumented; + return writer.toByteArray(); } /** diff --git a/org.jacoco.core/src/org/jacoco/core/internal/BytecodeVersion.java b/org.jacoco.core/src/org/jacoco/core/internal/BytecodeVersion.java deleted file mode 100644 index c24828b3..00000000 --- a/org.jacoco.core/src/org/jacoco/core/internal/BytecodeVersion.java +++ /dev/null @@ -1,81 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Evgeny Mandrikov - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.internal; - -import org.objectweb.asm.Opcodes; - -/** - * Utilities to read and modify bytecode version in bytes of class. Main purpose - * of this class is to deal with bytecode versions which are not yet supported - * by ASM. - */ -public final class BytecodeVersion { - - private static final int VERSION_INDEX = 6; - - /** - * Version of the Java 10 class file format. - */ - public static final int V10 = Opcodes.V9 + 1; - - private BytecodeVersion() { - } - - /** - * Gets major of bytecode version number from given bytes of class. - * - * @param b - * bytes of class - * @return version of bytecode - */ - public static int get(final byte[] b) { - return (short) (((b[VERSION_INDEX] & 0xFF) << 8) - | (b[VERSION_INDEX + 1] & 0xFF)); - } - - /** - * Sets major of bytecode version in given bytes of class. - * - * @param b - * bytes of class - * @param version - * version of bytecode to set - */ - public static void set(final byte[] b, final int version) { - b[VERSION_INDEX] = (byte) (version >>> 8); - b[VERSION_INDEX + 1] = (byte) version; - } - - /** - * Returns given bytes of class if its major bytecode version is less that - * {@link #V10}, otherwise returns copy where major version set to - * {@link Opcodes#V9}. - * - * @param version - * version of bytecode - * @param source - * bytes of class - * @return given bytes of class if version is less than {@link #V10}, - * otherwise copy where version set to {@link Opcodes#V9} - */ - public static byte[] downgradeIfNeeded(final int version, - final byte[] source) { - if (V10 != version) { - return source; - } - final byte[] b = new byte[source.length]; - System.arraycopy(source, 0, b, 0, source.length); - set(b, Opcodes.V9); - return b; - } - -} diff --git a/org.jacoco.core/src/org/jacoco/core/internal/ContentTypeDetector.java b/org.jacoco.core/src/org/jacoco/core/internal/ContentTypeDetector.java index c74d3374..5ef3a9a0 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/ContentTypeDetector.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/ContentTypeDetector.java @@ -83,7 +83,7 @@ public class ContentTypeDetector { case Opcodes.V1_7: case Opcodes.V1_8: case Opcodes.V9: - case BytecodeVersion.V10: + case Opcodes.V10: case Opcodes.V11: case Opcodes.V11 | Opcodes.V_PREVIEW_EXPERIMENTAL: case Opcodes.V12: diff --git a/org.jacoco.core/src/org/jacoco/core/internal/instr/InstrSupport.java b/org.jacoco.core/src/org/jacoco/core/internal/instr/InstrSupport.java index 1d4cf2a3..c4569c28 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/instr/InstrSupport.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/instr/InstrSupport.java @@ -13,6 +13,7 @@ package org.jacoco.core.internal.instr; import static java.lang.String.format; +import org.objectweb.asm.ClassReader; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; @@ -157,6 +158,20 @@ public final class InstrSupport { */ static final int CLINIT_ACC = Opcodes.ACC_SYNTHETIC | Opcodes.ACC_STATIC; + private static final int MAJOR_VERSION_INDEX = 6; + + /** + * Gets major of bytecode version number from given bytes of class. + * + * @param b + * bytes of class + * @return version of bytecode + */ + public static int getVersionMajor(final byte[] b) { + return (short) (((b[MAJOR_VERSION_INDEX] & 0xFF) << 8) + | (b[MAJOR_VERSION_INDEX + 1] & 0xFF)); + } + /** * Determines whether the given class file version requires stackmap frames. * diff --git a/org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactory.java b/org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactory.java index a4fb82b0..4e1938f2 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactory.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactory.java @@ -11,7 +11,6 @@ *******************************************************************************/ package org.jacoco.core.internal.instr; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.flow.ClassProbesAdapter; import org.jacoco.core.runtime.IExecutionDataAccessorGenerator; import org.objectweb.asm.ClassReader; @@ -44,7 +43,7 @@ public final class ProbeArrayStrategyFactory { final IExecutionDataAccessorGenerator accessorGenerator) { final String className = reader.getClassName(); - final int version = BytecodeVersion.get(reader.b); + final int version = InstrSupport.getVersionMajor(reader.b); if (isInterfaceOrModule(reader)) { final ProbeCounter counter = getProbeCounter(reader); diff --git a/org.jacoco.core/src/org/jacoco/core/runtime/ModifiedSystemClassRuntime.java b/org.jacoco.core/src/org/jacoco/core/runtime/ModifiedSystemClassRuntime.java index e51242d8..6d31358f 100644 --- a/org.jacoco.core/src/org/jacoco/core/runtime/ModifiedSystemClassRuntime.java +++ b/org.jacoco.core/src/org/jacoco/core/runtime/ModifiedSystemClassRuntime.java @@ -19,7 +19,6 @@ import java.lang.instrument.Instrumentation; import java.lang.reflect.Field; import java.security.ProtectionDomain; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.instr.InstrSupport; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; @@ -154,10 +153,7 @@ public class ModifiedSystemClassRuntime extends AbstractRuntime { */ public static byte[] instrument(final byte[] source, final String accessFieldName) { - final int originalVersion = BytecodeVersion.get(source); - final byte[] b = BytecodeVersion.downgradeIfNeeded(originalVersion, - source); - final ClassReader reader = new ClassReader(b); + final ClassReader reader = new ClassReader(source); final ClassWriter writer = new ClassWriter(reader, 0); reader.accept(new ClassVisitor(InstrSupport.ASM_API_VERSION, writer) { @@ -168,9 +164,7 @@ public class ModifiedSystemClassRuntime extends AbstractRuntime { } }, ClassReader.EXPAND_FRAMES); - final byte[] instrumented = writer.toByteArray(); - BytecodeVersion.set(instrumented, originalVersion); - return instrumented; + return writer.toByteArray(); } private static void createDataField(final ClassVisitor visitor, |