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.apache.bcel.generic.InstructionFactory.createArrayLoad;
20  import static org.apache.bcel.generic.InstructionFactory.createArrayStore;
21  import static org.apache.bcel.generic.InstructionFactory.createBinaryOperation;
22  import static org.apache.bcel.generic.InstructionFactory.createBranchInstruction;
23  import static org.apache.bcel.generic.InstructionFactory.createLoad;
24  import static org.apache.bcel.generic.InstructionFactory.createNull;
25  import static org.apache.bcel.generic.InstructionFactory.createReturn;
26  import static org.apache.bcel.generic.InstructionFactory.createStore;
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
29  
30  import org.apache.bcel.AbstractTestCase;
31  import org.apache.bcel.Const;
32  import org.apache.bcel.Repository;
33  import org.junit.jupiter.api.Test;
34  
35  public class InstructionFactoryTestCase extends AbstractTestCase {
36  
37      @Test
38      public void testArrayLoad() throws Exception {
39          assertEquals(InstructionConst.BALOAD, createArrayLoad(Type.BOOLEAN));
40          assertEquals(InstructionConst.BALOAD, createArrayLoad(Type.BYTE));
41          assertEquals(InstructionConst.CALOAD, createArrayLoad(Type.CHAR));
42          assertEquals(InstructionConst.SALOAD, createArrayLoad(Type.SHORT));
43          assertEquals(InstructionConst.IALOAD, createArrayLoad(Type.INT));
44          assertEquals(InstructionConst.FALOAD, createArrayLoad(Type.FLOAT));
45          assertEquals(InstructionConst.DALOAD, createArrayLoad(Type.DOUBLE));
46          assertEquals(InstructionConst.LALOAD, createArrayLoad(Type.LONG));
47          assertEquals(InstructionConst.AALOAD, createArrayLoad(Type.OBJECT));
48          assertEquals(InstructionConst.AALOAD, createArrayLoad(Type.getType("[I")));
49      }
50  
51      @Test
52      public void testArrayStore() throws Exception {
53          assertEquals(InstructionConst.BASTORE, createArrayStore(Type.BOOLEAN));
54          assertEquals(InstructionConst.BASTORE, createArrayStore(Type.BYTE));
55          assertEquals(InstructionConst.CASTORE, createArrayStore(Type.CHAR));
56          assertEquals(InstructionConst.SASTORE, createArrayStore(Type.SHORT));
57          assertEquals(InstructionConst.IASTORE, createArrayStore(Type.INT));
58          assertEquals(InstructionConst.FASTORE, createArrayStore(Type.FLOAT));
59          assertEquals(InstructionConst.DASTORE, createArrayStore(Type.DOUBLE));
60          assertEquals(InstructionConst.LASTORE, createArrayStore(Type.LONG));
61          assertEquals(InstructionConst.AASTORE, createArrayStore(Type.OBJECT));
62          assertEquals(InstructionConst.AASTORE, createArrayStore(Type.getType("[I")));
63      }
64  
65      @Test
66      public void testCreateInvokeNullArgTypes() throws Exception {
67          final InstructionFactory factory = new InstructionFactory(new ClassGen(Repository.lookupClass(Object.class)));
68          factory.createInvoke("", "", Type.VOID, null, Const.INVOKESPECIAL, false); // Mustn't throw an NPE
69      }
70  
71      @Test
72      public void testExceptions() throws Exception {
73          final InstructionFactory factory = new InstructionFactory(new ClassGen(Repository.lookupClass(Object.class)));
74          assertThrowsExactly(IllegalArgumentException.class, () -> createArrayLoad(Type.UNKNOWN));
75          assertThrowsExactly(IllegalArgumentException.class, () -> createArrayStore(Type.UNKNOWN));
76          assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.DOUBLE));
77          assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.FLOAT));
78          assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.INT));
79          assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("$", Type.LONG));
80          assertThrowsExactly(IllegalArgumentException.class, () -> createBinaryOperation("*", Type.OBJECT));
81          assertThrowsExactly(IllegalArgumentException.class, () -> createBranchInstruction(Short.MIN_VALUE, null));
82          assertThrowsExactly(IllegalArgumentException.class, () -> createLoad(Type.UNKNOWN, 0));
83          assertThrowsExactly(IllegalArgumentException.class, () -> createNull(Type.UNKNOWN));
84          assertThrowsExactly(IllegalArgumentException.class, () -> createReturn(Type.UNKNOWN));
85          assertThrowsExactly(IllegalArgumentException.class, () -> createStore(Type.UNKNOWN, 0));
86          assertThrowsExactly(IllegalArgumentException.class, () -> factory.createAppend(Type.UNKNOWN));
87          assertThrowsExactly(IllegalArgumentException.class, () -> factory.createCast(Type.UNKNOWN, Type.UNKNOWN));
88          assertThrowsExactly(IllegalArgumentException.class, () -> factory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Const.NOP));
89          assertThrowsExactly(IllegalArgumentException.class, () -> factory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.STRING }, Const.NOP));
90      }
91  
92      @Test
93      public void testNull() throws Exception {
94          assertEquals(InstructionConst.ICONST_0, createNull(Type.BOOLEAN));
95          assertEquals(InstructionConst.ICONST_0, createNull(Type.BYTE));
96          assertEquals(InstructionConst.ICONST_0, createNull(Type.CHAR));
97          assertEquals(InstructionConst.ICONST_0, createNull(Type.SHORT));
98          assertEquals(InstructionConst.ICONST_0, createNull(Type.INT));
99          assertEquals(InstructionConst.FCONST_0, createNull(Type.FLOAT));
100         assertEquals(InstructionConst.DCONST_0, createNull(Type.DOUBLE));
101         assertEquals(InstructionConst.LCONST_0, createNull(Type.LONG));
102         assertEquals(InstructionConst.NOP, createNull(Type.VOID));
103         assertEquals(InstructionConst.ACONST_NULL, createNull(Type.OBJECT));
104         assertEquals(InstructionConst.ACONST_NULL, createNull(Type.getType("[I")));
105     }
106 }