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