Commit e700b2d2 authored by Eric Bruneton's avatar Eric Bruneton

Add more tests for the commons package.

parent 6969d6eb
Pipeline #953 passed with stage
in 6 minutes and 18 seconds
......@@ -193,8 +193,8 @@ public class AnalyzerAdapter extends MethodVisitor {
final int nStack,
final Object[] stack) {
if (type != Opcodes.F_NEW) { // uncompressed frame
throw new IllegalStateException(
"ClassReader.accept() should be called with EXPAND_FRAMES flag");
throw new IllegalArgumentException(
"LocalVariablesSorter only accepts expanded frames (see ClassReader.EXPAND_FRAMES)");
}
super.visitFrame(type, nLocal, local, nStack, stack);
......
......@@ -183,8 +183,8 @@ public class LocalVariablesSorter extends MethodVisitor {
final int nStack,
final Object[] stack) {
if (type != Opcodes.F_NEW) { // uncompressed frame
throw new IllegalStateException(
"ClassReader.accept() should be called with EXPAND_FRAMES flag");
throw new IllegalArgumentException(
"LocalVariablesSorter only accepts expanded frames (see ClassReader.EXPAND_FRAMES)");
}
// creates a copy of newLocals
......
......@@ -138,7 +138,7 @@ public class Method {
int space = method.indexOf(' ');
int start = method.indexOf('(', space) + 1;
int end = method.indexOf(')', start);
if (space == -1 || start == -1 || end == -1) {
if (space == -1 || start == 0 || end == -1) {
throw new IllegalArgumentException();
}
String returnType = method.substring(0, space);
......
......@@ -58,6 +58,9 @@ public class TryCatchBlockSorter extends MethodNode {
final String signature,
final String[] exceptions) {
this(Opcodes.ASM6, mv, access, name, desc, signature, exceptions);
if (getClass() != TryCatchBlockSorter.class) {
throw new IllegalStateException();
}
}
protected TryCatchBlockSorter(
......
......@@ -33,6 +33,7 @@ import static org.objectweb.asm.test.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
......@@ -52,6 +53,24 @@ import org.objectweb.asm.test.AsmTest;
*/
public class AnalyzerAdapterTest extends AsmTest {
@Test
public void testConstructor() {
new AnalyzerAdapter("pkg/Class", Opcodes.ACC_PUBLIC, "name", "()V", null);
assertThrows(
IllegalStateException.class,
() -> new AnalyzerAdapter("pkg/Class", Opcodes.ACC_PUBLIC, "name", "()V", null) {});
}
@Test
public void testVisitFrame() {
AnalyzerAdapter analyzerAdapter =
new AnalyzerAdapter("pkg/Class", Opcodes.ACC_PUBLIC, "name", "()V", null);
analyzerAdapter.visitFrame(Opcodes.F_NEW, 0, null, 0, null);
assertThrows(
IllegalArgumentException.class,
() -> analyzerAdapter.visitFrame(Opcodes.F_FULL, 0, null, 0, null));
}
/**
* Tests that classes with additional frames inserted at each instruction, using the results of an
* AnalyzerAdapter, can be instantiated and loaded. This makes sure the intermediate frames
......
......@@ -27,16 +27,27 @@
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.test.AsmTest;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceMethodVisitor;
/**
* InstructionAdapter tests.
......@@ -45,6 +56,134 @@ import org.objectweb.asm.test.AsmTest;
*/
public class InstructionAdapterTest extends AsmTest {
@Test
public void testConstructor() {
new InstructionAdapter(new MethodNode());
assertThrows(IllegalStateException.class, () -> new InstructionAdapter(new MethodNode()) {});
}
@Test
public void testIllegalVisitInsn() {
assertThrows(
IllegalArgumentException.class,
() -> new InstructionAdapter(new MethodNode()).visitInsn(Opcodes.GOTO));
}
@Test
public void testIllegalVisitIntInsn() {
assertThrows(
IllegalArgumentException.class,
() -> new InstructionAdapter(new MethodNode()).visitIntInsn(Opcodes.GOTO, 0));
assertThrows(
IllegalArgumentException.class,
() -> new InstructionAdapter(new MethodNode()).visitIntInsn(Opcodes.NEWARRAY, 0));
}
@Test
public void testIllegalVisitVarInsn() {
assertThrows(
IllegalArgumentException.class,
() -> new InstructionAdapter(new MethodNode()).visitVarInsn(Opcodes.GOTO, 0));
}
@Test
public void testIllegalVisitTypeInsn() {
assertThrows(
IllegalArgumentException.class,
() -> new InstructionAdapter(new MethodNode()).visitTypeInsn(Opcodes.GOTO, "pkg/Class"));
}
@Test
public void testIllegalVisitFieldInsn() {
assertThrows(
IllegalArgumentException.class,
() ->
new InstructionAdapter(new MethodNode())
.visitFieldInsn(Opcodes.INVOKEVIRTUAL, "pkg/Class", "name", "I"));
}
@Test
public void testIllegalVisitMethodInsn() {
assertThrows(
IllegalArgumentException.class,
() ->
new InstructionAdapter(new MethodNode())
.visitMethodInsn(Opcodes.GETFIELD, "pkg/Class", "name", "I"));
}
@Test
public void testIllegalVisitJumpInsn() {
assertThrows(
IllegalArgumentException.class,
() -> new InstructionAdapter(new MethodNode()).visitJumpInsn(Opcodes.NOP, new Label()));
}
@Test
public void testVisitLdcInsn() {
Textifier textifier = new Textifier();
InstructionAdapter instructionAdapter =
new InstructionAdapter(new TraceMethodVisitor(textifier));
instructionAdapter.visitLdcInsn(Boolean.FALSE);
instructionAdapter.visitLdcInsn(Boolean.TRUE);
instructionAdapter.visitLdcInsn(Byte.valueOf((byte) 2));
instructionAdapter.visitLdcInsn(Character.valueOf('3'));
instructionAdapter.visitLdcInsn(Short.valueOf((short) 4));
instructionAdapter.visitLdcInsn(Integer.valueOf(5));
instructionAdapter.visitLdcInsn(Long.valueOf(6));
instructionAdapter.visitLdcInsn(Float.valueOf(7.0f));
instructionAdapter.visitLdcInsn(Double.valueOf(8.0));
instructionAdapter.visitLdcInsn("9");
instructionAdapter.visitLdcInsn(Type.getObjectType("pkg/Class"));
instructionAdapter.visitLdcInsn(
new Handle(Opcodes.H_GETFIELD, "pkg/Class", "name", "I", /* isInterface= */ false));
assertEquals(
"ICONST_0 ICONST_1 ICONST_2 BIPUSH 51 ICONST_4 ICONST_5 LDC 6 LDC 7.0 LDC 8.0 LDC \"9\" "
+ "LDC Lpkg/Class;.class LDC pkg/Class.nameI (1)",
textifier
.text
.stream()
.map(text -> text.toString().trim())
.collect(Collectors.joining(" ")));
}
@Test
public void testInvokeSpecial() {
new InstructionAdapter(new MethodNode()).invokespecial("pkg/Class", "name", "()V");
assertThrows(
IllegalArgumentException.class,
() ->
new InstructionAdapter(Opcodes.ASM4, null)
.invokespecial("pkg/Class", "name", "()V", /* isInterface= */ true));
}
@Test
public void testInvokeVirtual() {
new InstructionAdapter(new MethodNode()).invokevirtual("pkg/Class", "name", "()V");
assertThrows(
IllegalArgumentException.class,
() ->
new InstructionAdapter(Opcodes.ASM4, null)
.invokevirtual("pkg/Class", "name", "()V", /* isInterface= */ true));
}
@Test
public void testInvokeStatic() {
new InstructionAdapter(new MethodNode()).invokestatic("pkg/Class", "name", "()V");
assertThrows(
IllegalArgumentException.class,
() ->
new InstructionAdapter(Opcodes.ASM4, null)
.invokestatic("pkg/Class", "name", "()V", /* isInterface= */ true));
}
@Test
public void testIllegalVisitLdcInsn() {
assertThrows(
IllegalArgumentException.class,
() -> new InstructionAdapter(new MethodNode()).visitLdcInsn(new Object()));
}
/** Tests that classes transformed with an InstructionAdapter are unchanged. */
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_ALL_APIS)
......
......@@ -28,6 +28,7 @@
package org.objectweb.asm.commons;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.objectweb.asm.test.Assertions.assertThat;
......@@ -183,6 +184,14 @@ public class JSRInlinerAdapterTest extends AsmTest {
}
}
@Test
public void testConstructor() {
new JSRInlinerAdapter(null, Opcodes.ACC_PUBLIC, "name", "()V", null, null);
assertThrows(
IllegalStateException.class,
() -> new JSRInlinerAdapter(null, Opcodes.ACC_PUBLIC, "name", "()V", null, null) {});
}
/**
* Tests a method which has the most basic <code>try{}finally</code> form imaginable:
*
......
......@@ -27,6 +27,7 @@
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.objectweb.asm.test.Assertions.assertThat;
......@@ -42,7 +43,9 @@ import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.test.AsmTest;
import org.objectweb.asm.tree.MethodNode;
/**
* LocalVariablesSorter tests.
......@@ -51,6 +54,51 @@ import org.objectweb.asm.test.AsmTest;
*/
public class LocalVariablesSorterTest extends AsmTest {
@Test
public void testConstructor() {
new LocalVariablesSorter(Opcodes.ACC_PUBLIC, "()V", new MethodNode());
assertThrows(
IllegalStateException.class,
() -> new LocalVariablesSorter(Opcodes.ACC_PUBLIC, "()V", new MethodNode()) {});
}
@Test
public void testVisitFrame() {
LocalVariablesSorter localVariablesSorter =
new LocalVariablesSorter(Opcodes.ACC_PUBLIC, "()V", new MethodNode());
localVariablesSorter.visitFrame(Opcodes.F_NEW, 0, null, 0, null);
assertThrows(
IllegalArgumentException.class,
() -> localVariablesSorter.visitFrame(Opcodes.F_FULL, 0, null, 0, null));
}
@Test
public void testNewLocal() {
LocalVariablesSorter localVariablesSorter =
new LocalVariablesSorter(Opcodes.ACC_STATIC, "()V", new MethodNode());
localVariablesSorter.newLocal(Type.BOOLEAN_TYPE);
assertEquals(1, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.BYTE_TYPE);
assertEquals(2, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.CHAR_TYPE);
assertEquals(3, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.SHORT_TYPE);
assertEquals(4, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.INT_TYPE);
assertEquals(5, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.FLOAT_TYPE);
assertEquals(6, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.LONG_TYPE);
assertEquals(8, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.DOUBLE_TYPE);
assertEquals(10, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.getObjectType("pkg/Class"));
assertEquals(11, localVariablesSorter.nextLocal);
localVariablesSorter.newLocal(Type.getType("[I"));
assertEquals(12, localVariablesSorter.nextLocal);
}
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_ALL_APIS)
public void testSortLocalVariablesAndInstantiate(
......
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.Type;
/**
* Method tests.
*
* @author Eric Bruneton
*/
public class MethodTest {
@Test
public void testConstructor1() {
Method method = new Method("name", "(I)J");
assertEquals("name", method.getName());
assertEquals("(I)J", method.getDescriptor());
assertEquals(Type.LONG_TYPE, method.getReturnType());
assertArrayEquals(new Type[] {Type.INT_TYPE}, method.getArgumentTypes());
assertEquals("name(I)J", method.toString());
}
@Test
public void testConstructor2() {
Method method = new Method("name", Type.LONG_TYPE, new Type[] {Type.INT_TYPE});
assertEquals("name", method.getName());
assertEquals("(I)J", method.getDescriptor());
assertEquals(Type.LONG_TYPE, method.getReturnType());
assertArrayEquals(new Type[] {Type.INT_TYPE}, method.getArgumentTypes());
assertEquals("name(I)J", method.toString());
}
@Test
public void testGetReflectMethod() throws NoSuchMethodException, SecurityException {
Method method = Method.getMethod(Object.class.getMethod("equals", Object.class));
assertEquals("equals", method.getName());
assertEquals("(Ljava/lang/Object;)Z", method.getDescriptor());
}
@Test
public void testGetReflectConstructor() throws NoSuchMethodException, SecurityException {
Method method = Method.getMethod(Object.class.getConstructor());
assertEquals("<init>", method.getName());
assertEquals("()V", method.getDescriptor());
}
@Test
public void testGetMethod() throws NoSuchMethodException, SecurityException {
Method method =
Method.getMethod(
"boolean name(byte, char, short, int, float, long, double, pkg.Class, pkg.Class[])");
assertEquals("name", method.getName());
assertEquals("(BCSIFJDLpkg/Class;[Lpkg/Class;)Z", method.getDescriptor());
assertThrows(IllegalArgumentException.class, () -> Method.getMethod("name()"));
assertThrows(IllegalArgumentException.class, () -> Method.getMethod("void name"));
assertThrows(IllegalArgumentException.class, () -> Method.getMethod("void name(]"));
}
@Test
public void testGetMethodWithDefaultPackage() throws NoSuchMethodException, SecurityException {
assertEquals(
"(Ljava/lang/Object;)V",
Method.getMethod("void name(Object)", /* defaultPackage= */ false).getDescriptor());
assertEquals(
"(LObject;)V",
Method.getMethod("void name(Object)", /* defaultPackage= */ true).getDescriptor());
}
@Test
public void testEquals() {
assertFalse(new Method("name", "()V").equals(null));
assertFalse(new Method("name", "()V").equals(new Method("other", "()V")));
assertFalse(new Method("name", "()V").equals(new Method("name", "(I)J")));
assertTrue(new Method("name", "()V").equals(Method.getMethod("void name()")));
}
@Test
public void testHashCode() {
assertTrue(new Method("name", "()V").hashCode() != 0);
}
}
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
/**
* ModuleHashesAttribute tests.
*
* @author Eric Bruneton
*/
public class ModuleHashesAttributeTest {
private static final byte[] HASH1 = {0x1, 0x2, 0x3};
private static final byte[] HASH2 = {0x4, 0x5, 0x6};
@Test
public void testWriteAndRead() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visitAttribute(
new ModuleHashesAttribute(
"algorithm",
Arrays.asList(new String[] {"module1", "module2"}),
Arrays.asList(new byte[][] {HASH1, HASH2})));
ModuleHashesAttribute moduleHashesAttribute = new ModuleHashesAttribute();
new ClassReader(classWriter.toByteArray())
.accept(
new ClassVisitor(Opcodes.ASM6) {
@Override
public void visitAttribute(Attribute attribute) {
if (attribute instanceof ModuleHashesAttribute) {
moduleHashesAttribute.algorithm = ((ModuleHashesAttribute) attribute).algorithm;
moduleHashesAttribute.modules = ((ModuleHashesAttribute) attribute).modules;
moduleHashesAttribute.hashes = ((ModuleHashesAttribute) attribute).hashes;
}
}
},
new Attribute[] {new ModuleHashesAttribute()},
0);
assertEquals("algorithm", moduleHashesAttribute.algorithm);
assertEquals(2, moduleHashesAttribute.modules.size());
assertEquals("module1", moduleHashesAttribute.modules.get(0));
assertEquals("module2", moduleHashesAttribute.modules.get(1));
assertEquals(2, moduleHashesAttribute.hashes.size());
assertArrayEquals(HASH1, moduleHashesAttribute.hashes.get(0));
assertArrayEquals(HASH2, moduleHashesAttribute.hashes.get(1));
}
}
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
/**
* ModuleResolutionAttribute tests.
*
* @author Eric Bruneton
*/
public class ModuleResolutionAttributeTest {
@Test
public void testWriteAndRead() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visitAttribute(new ModuleResolutionAttribute(123));
ModuleResolutionAttribute moduleResolutionAttribute = new ModuleResolutionAttribute();
new ClassReader(classWriter.toByteArray())
.accept(
new ClassVisitor(Opcodes.ASM6) {
@Override
public void visitAttribute(Attribute attribute) {
if (attribute instanceof ModuleResolutionAttribute) {
moduleResolutionAttribute.resolution =
((ModuleResolutionAttribute) attribute).resolution;
}
}
},
new Attribute[] {new ModuleResolutionAttribute()},
0);
assertEquals(123, moduleResolutionAttribute.resolution);
}
}
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.commons;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
/**
* ModuleTargetAttribute tests.
*
* @author Eric Bruneton
*/
public class ModuleTargetAttributeTest {
@Test
public void testWriteAndRead() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visitAttribute(new ModuleTargetAttribute("platform"));
ModuleTargetAttribute moduleTargetAttribute = new ModuleTargetAttribute();
new ClassReader(classWriter.toByteArray())
.accept(
new ClassVisitor(Opcodes.ASM6) {
@Override
public void visitAttribute(Attribute attribute) {
if (attribute instanceof ModuleTargetAttribute) {
moduleTargetAttribute.platform = ((ModuleTargetAttribute) attribute).platform;
}
}
},
new Attribute[] {new ModuleTargetAttribute()},
0);