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  package org.apache.bcel.generic;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import org.apache.bcel.Repository;
24  import org.apache.bcel.classfile.Code;
25  import org.apache.bcel.classfile.JavaClass;
26  import org.apache.bcel.classfile.Method;
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.params.ParameterizedTest;
29  import org.junit.jupiter.params.provider.ValueSource;
30  
31  public class TypeTestCase {
32      @Test
33      public void testBCEL243() {
34          // expectedValue = "Ljava/util/Map<TX;Ljava/util/List<TY;>;>;";
35          // The line commented out above is the correct expected value; however,
36          // the constructor for ObjectType is yet another place where BCEL does
37          // not understand generics so we need to substitute the modified value below.
38          final String expectedValue = "Ljava/util/Map<X, java/util/List<Y>>;";
39          final String actualValue = Type.getType("Ljava/util/Map<TX;Ljava/util/List<TY;>;>;").getSignature();
40          assertEquals(expectedValue, actualValue, "Type.getType");
41      }
42  
43      @Test
44      public void testInternalTypeNametoSignature() {
45          assertNull(Type.internalTypeNameToSignature(null));
46          assertEquals("", Type.internalTypeNameToSignature(""));
47          assertEquals("TT;", Type.internalTypeNameToSignature("TT;"));
48          assertEquals("Ljava/lang/String;", Type.internalTypeNameToSignature("Ljava/lang/String;"));
49          assertEquals("[Ljava/lang/String;", Type.internalTypeNameToSignature("[Ljava/lang/String;"));
50          assertEquals("Ljava/lang/String;", Type.internalTypeNameToSignature("java/lang/String"));
51          assertEquals("I", Type.internalTypeNameToSignature("I"));
52          assertEquals("LT;", Type.internalTypeNameToSignature("T"));
53      }
54  
55      @ParameterizedTest
56      @ValueSource(strings = {
57      // @formatter:off
58          "java/io/Externalizable",
59          "java/io/ObjectOutputStream",
60          "java/io/Serializable",
61          "java/lang/Cloneable",
62          "java/lang/RuntimeException",
63          "java/lang/String",
64          "java/lang/System",
65          "java/lang/Throwable",
66          "java/net/URI",
67          "java/sql/Statement",
68          "java/util/ArrayList",
69          "java/util/Calendar",
70          "java/util/EnumMap",
71          "java/util/HashSet",
72          "java/util/Iterator",
73          "java/util/LinkedList",
74          "java/util/List",
75          "java/util/Map",
76          "java/util/concurrent/ConcurrentMap",
77          "java/util/concurrent/ExecutorService",
78          "org/apache/bcel/classfile/JavaClass",
79          "org/apache/bcel/classfile/Method",
80          "org/apache/bcel/classfile/Synthetic",
81          "org/apache/bcel/generic/ConstantPoolGen",
82          "org/apache/bcel/generic/MethodGen",
83          "com/foo/Foo"})
84      // @formatter:on
85      public void testLDC(final String className) throws Exception {
86          final JavaClass jc = Repository.lookupClass(className);
87          final ConstantPoolGen cpg = new ConstantPoolGen(jc.getConstantPool());
88          for (final Method method : jc.getMethods()) {
89              final Code code = method.getCode();
90              if (code != null) {
91                  final InstructionList instructionList = new InstructionList(code.getCode());
92                  for (final InstructionHandle instructionHandle : instructionList) {
93                      instructionHandle.accept(new EmptyVisitor() {
94                          @Override
95                          public void visitLDC(final LDC obj) {
96                              assertNotNull(obj.getValue(cpg));
97                          }
98                      });
99                  }
100             }
101         }
102     }
103 }