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