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  package org.apache.bcel.classfile;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
23  
24  import java.io.DataInput;
25  import java.util.Map;
26  import java.util.Optional;
27  
28  import org.apache.bcel.AbstractTest;
29  import org.apache.bcel.Repository;
30  import org.apache.bcel.generic.Type;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   *  Tests signatures, including primitives, generics, and bad signatures for exceptions.
35   */
36  class SignatureTest extends AbstractTest {
37  
38      @Test
39      void testBadSignatures() throws Exception {
40          assertThrowsExactly(IllegalArgumentException.class, () -> Signature.translate("<"));
41          assertThrowsExactly(IllegalArgumentException.class, () -> Signature.translate("<>"));
42      }
43  
44      @Test
45      void testMap() throws Exception {
46          final JavaClass jc = Repository.lookupClass(Map.class);
47          final Signature classSignature = (Signature) findAttribute("Signature", jc.getAttributes());
48          final String translatedSignature = Signature.translate(classSignature.getSignature());
49          assertEquals("<K, V>java.lang.Object", translatedSignature);
50          testMethod(jc, "(java.lang.Object)V", Map.class, "get", Object.class);
51          testMethod(jc, "(K, V)V", Map.class, "put", Object.class, Object.class);
52      }
53  
54      void testMethod(final JavaClass jc, final String expected, final Class<?> clazz, final String methodName, final Class<?>... paramTypes) throws Exception {
55          final Method method = jc.getMethod(clazz.getMethod(methodName, paramTypes));
56          final String methodSignature = Optional.ofNullable(method.getGenericSignature()).orElse(method.getSignature());
57          final String translatedMethodSignature = Signature.translate(methodSignature);
58          assertEquals(expected, translatedMethodSignature);
59      }
60  
61      @Test
62      void testString() throws Exception {
63          final JavaClass jc = Repository.lookupClass(String.class);
64          final Signature classSignature = (Signature) findAttribute("Signature", jc.getAttributes());
65          final String translatedSignature = Signature.translate(classSignature.getSignature());
66          assertEquals("java.lang.Object", translatedSignature);
67          testMethod(jc, "()java.lang.String", String.class, "trim");
68  
69      }
70  
71      @Test
72      void testType() throws Exception {
73          assertEquals("(I)I", Type.getSignature(Math.class.getMethod("abs", int.class)));
74          assertEquals("(J)J", Type.getSignature(Math.class.getMethod("abs", long.class)));
75          assertEquals("(D)D", Type.getSignature(Math.class.getMethod("abs", double.class)));
76          assertEquals("(F)F", Type.getSignature(Math.class.getMethod("abs", float.class)));
77          assertEquals("(Ljava/lang/String;)[Ljava/lang/String;", Type.getSignature(String.class.getMethod("split", String.class)));
78          assertEquals("(I)C", Type.getSignature(String.class.getMethod("charAt", int.class)));
79          assertEquals("()Ljava/lang/String;", Type.getSignature(String.class.getMethod("trim")));
80          assertEquals("()V", Type.getSignature(Object.class.getMethod("notify")));
81          assertEquals("(Ljava/lang/Object;)Z", Type.getSignature(Object.class.getMethod("equals", Object.class)));
82          assertEquals("()B", Type.getSignature(DataInput.class.getMethod("readByte")));
83          assertEquals("()S", Type.getSignature(DataInput.class.getMethod("readShort")));
84      }
85  }