View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.verifier;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  
25  import org.apache.bcel.AbstractTest;
26  import org.apache.bcel.classfile.ClassParser;
27  import org.apache.bcel.classfile.JavaClass;
28  import org.apache.bcel.classfile.Method;
29  import org.apache.bcel.generic.ConstantPoolGen;
30  import org.apache.bcel.generic.EmptyVisitor;
31  import org.apache.bcel.generic.Instruction;
32  import org.apache.bcel.generic.LDC;
33  import org.apache.bcel.generic.MethodGen;
34  import org.junit.jupiter.params.ParameterizedTest;
35  import org.junit.jupiter.params.provider.ValueSource;
36  
37  /**
38   * Tests BCEL-370.
39   */
40  class JiraBcel370Test extends AbstractTest {
41      @ParameterizedTest
42      @ValueSource(strings = {
43      // @formatter:off
44          "target/test-classes/com/puppycrawl/tools/checkstyle/grammar/java/JavaLanguageParser$ClassBlockContext.class",
45          "target/test-classes/com/foo/Foo.class"
46      })
47      // @formatter:on
48      void testLdcGetType(final String classFileName) throws Exception {
49          try (FileInputStream file = new FileInputStream(classFileName)) {
50              final ClassParser parser = new ClassParser(file, new File(classFileName).getName());
51              final JavaClass clazz = parser.parse();
52  
53              final Method[] methods = clazz.getMethods();
54  
55              final ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
56              final MethodGen methodGen = new MethodGen(methods[0], classFileName, cp);
57  
58              // The first instruction is an LDC CONSTANT_Dynamic added by Jacoco
59              final Instruction instruction = methodGen.getInstructionList().getInstructions()[0];
60  
61              instruction.accept(new EmptyVisitor() {
62                  @Override
63                  public void visitLDC(final LDC ldc) {
64                      // Without the change to LDC.getType() this fails because the tag is CONSTANT_Dynamic
65                      ldc.getType(cp);
66                  }
67              });
68          }
69      }
70  
71      @ParameterizedTest
72      @ValueSource(strings = {
73      // @formatter:off
74          "com.foo.Foo"
75      })
76      // @formatter:on
77      void testVerify(final String className) throws ClassNotFoundException {
78          // Without the changes to the verifier this fails because it doesn't allow LDC CONSTANT_Dynamic
79          Verifier.verifyType(className);
80      }
81  }