• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
19 
20 import java.util.Iterator;
21 
22 import org.apache.bcel.classfile.JavaClass;
23 import org.apache.bcel.classfile.Method;
24 import org.apache.bcel.generic.InstructionHandle;
25 import org.apache.bcel.generic.InstructionList;
26 import org.apache.bcel.util.InstructionFinder;
27 
28 public class InstructionFinderTestCase extends AbstractTestCase
29 {
testSearchAll()30     public void testSearchAll() throws Exception
31     {
32         final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".util.InstructionFinder");
33         final Method[] methods = clazz.getMethods();
34         Method searchM = null;
35         for (final Method m : methods)
36         {
37             if (m.getName().equals("search") && (m.getArgumentTypes().length == 3))
38             {
39                 searchM = m;
40                 break;
41             }
42         }
43 
44         if (searchM == null) {
45             throw new Exception("search method not found");
46         }
47 
48         final byte[] bytes = searchM.getCode().getCode();
49         final InstructionList il = new InstructionList(bytes);
50         final InstructionFinder finder = new InstructionFinder(il);
51         final Iterator<?> it = finder.search(".*", il.getStart(), null);
52 
53         final InstructionHandle[] ihs = (InstructionHandle[])it.next();
54         int size = 0;
55         for (final InstructionHandle ih : ihs)
56         {
57             size += ih.getInstruction().getLength();
58         }
59         assertEquals(bytes.length, size);
60 
61     }
62 }
63