summaryrefslogtreecommitdiff
path: root/asm/src/test/java/org/objectweb/asm/MethodWriterTest.java
blob: 60ead10ae6cb6244a36798f4ab4ca6bce8a00a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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;

import static java.util.stream.Collectors.toSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;

/**
 * Unit tests for {@link MethodWriter}.
 *
 * @author Eric Bruneton
 */
class MethodWriterTest {

  /**
   * Tests that the attribute name fields of Constants are the expected ones. This test is designed
   * to fail each time new attributes are added to Constants, and serves as a reminder to update the
   * {@link MethodWriter#canCopyMethodAttributes} method, if needed.
   */
  @Test
  void testCanCopyMethodAttributesUpdated() {
    Set<Object> actualAttributes =
        Arrays.stream(Constants.class.getDeclaredFields())
            .filter(field -> field.getType() == String.class)
            .map(
                field -> {
                  try {
                    return field.get(null);
                  } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new RuntimeException("Can't get field value", e);
                  }
                })
            .collect(toSet());

    HashSet<String> expectedAttributes =
        new HashSet<String>(
            Arrays.asList(
                Constants.CONSTANT_VALUE,
                Constants.CODE,
                Constants.STACK_MAP_TABLE,
                Constants.EXCEPTIONS,
                Constants.INNER_CLASSES,
                Constants.ENCLOSING_METHOD,
                Constants.SYNTHETIC,
                Constants.SIGNATURE,
                Constants.SOURCE_FILE,
                Constants.SOURCE_DEBUG_EXTENSION,
                Constants.LINE_NUMBER_TABLE,
                Constants.LOCAL_VARIABLE_TABLE,
                Constants.LOCAL_VARIABLE_TYPE_TABLE,
                Constants.DEPRECATED,
                Constants.RUNTIME_VISIBLE_ANNOTATIONS,
                Constants.RUNTIME_INVISIBLE_ANNOTATIONS,
                Constants.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS,
                Constants.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS,
                Constants.RUNTIME_VISIBLE_TYPE_ANNOTATIONS,
                Constants.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS,
                Constants.ANNOTATION_DEFAULT,
                Constants.BOOTSTRAP_METHODS,
                Constants.METHOD_PARAMETERS,
                Constants.MODULE,
                Constants.MODULE_PACKAGES,
                Constants.MODULE_MAIN_CLASS,
                Constants.NEST_HOST,
                Constants.NEST_MEMBERS,
                Constants.PERMITTED_SUBCLASSES,
                Constants.RECORD));
    // IMPORTANT: if this fails, update the list AND update MethodWriter.canCopyMethodAttributes(),
    // if needed.
    assertEquals(expectedAttributes, actualAttributes);
  }

  @Test
  void testRecursiveCondyFastEnough() {
    Handle bsm =
        new Handle(
            Opcodes.H_INVOKESTATIC,
            "RT",
            "bsm",
            "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;I)Ljava/lang/invoke/CallSite;",
            false);
    ConstantDynamic chain = new ConstantDynamic("condy", "I", bsm, 0);
    for (int i = 0; i < 32; i++) {
      chain = new ConstantDynamic("condy" + i, "I", bsm, chain);
    }
    ConstantDynamic condy = chain;

    assertTimeoutPreemptively(
        Duration.ofMillis(1_000),
        () -> {
          ClassWriter classWriter = new ClassWriter(0);
          classWriter.visit(Opcodes.V11, Opcodes.ACC_SUPER, "Test", null, "java/lang/Object", null);
          MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_STATIC, "m", "()V", null, null);
          mv.visitCode();
          mv.visitLdcInsn(condy);
          mv.visitMaxs(0, 0);
          mv.visitEnd();
          classWriter.visitEnd();
          classWriter.toByteArray();
        });
  }
}