Home | History | Annotate | Download | only in generic
      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.generic;
     19 
     20 import java.util.Arrays;
     21 import java.util.List;
     22 
     23 import org.apache.bcel.Repository;
     24 import org.apache.bcel.classfile.JavaClass;
     25 import org.apache.bcel.classfile.Method;
     26 
     27 import junit.framework.TestCase;
     28 
     29 public class MethodGenTestCase extends TestCase {
     30 
     31     public static class Foo {
     32         public void bar() {
     33             @SuppressWarnings("unused")
     34             final
     35             int a = 1;
     36         }
     37     }
     38 
     39     @interface A {
     40     }
     41 
     42     @interface B {
     43     }
     44 
     45     public static class Bar {
     46         public class Inner {
     47             public Inner(@A final Object a, @B final Object b) {
     48 
     49             }
     50         }
     51     }
     52 
     53     private MethodGen getMethod(final Class<?> cls, final String name) throws ClassNotFoundException {
     54         final JavaClass jc = Repository.lookupClass(cls);
     55         final ConstantPoolGen cp = new ConstantPoolGen(jc.getConstantPool());
     56         for (final Method method : jc.getMethods()) {
     57             if (method.getName().equals(name)) {
     58                 return new MethodGen(method, jc.getClassName(), cp);
     59             }
     60         }
     61 
     62         fail("Method " + name + " not found in class " + cls);
     63         return null;
     64     }
     65 
     66     public void testRemoveLocalVariable() throws Exception {
     67         final MethodGen mg = getMethod(Foo.class, "bar");
     68 
     69         final LocalVariableGen lv = mg.getLocalVariables()[1];
     70         assertEquals("variable name", "a", lv.getName());
     71         final InstructionHandle start = lv.getStart();
     72         final InstructionHandle end = lv.getEnd();
     73         assertNotNull("scope start", start);
     74         assertNotNull("scope end", end);
     75         assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv));
     76         assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv));
     77 
     78         // now let's remove the local variable
     79         mg.removeLocalVariable(lv);
     80 
     81         assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv));
     82         assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv));
     83         assertNull("scope start", lv.getStart());
     84         assertNull("scope end", lv.getEnd());
     85     }
     86 
     87     public void testRemoveLocalVariables() throws Exception {
     88         final MethodGen mg = getMethod(Foo.class, "bar");
     89 
     90         final LocalVariableGen lv = mg.getLocalVariables()[1];
     91         assertEquals("variable name", "a", lv.getName());
     92         final InstructionHandle start = lv.getStart();
     93         final InstructionHandle end = lv.getEnd();
     94         assertNotNull("scope start", start);
     95         assertNotNull("scope end", end);
     96         assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv));
     97         assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv));
     98 
     99         // now let's remove the local variables
    100         mg.removeLocalVariables();
    101 
    102         assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv));
    103         assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv));
    104         assertNull("scope start", lv.getStart());
    105         assertNull("scope end", lv.getEnd());
    106     }
    107 
    108     public void testAnnotationsAreUnpacked() throws Exception {
    109         final JavaClass jc = Repository.lookupClass(Bar.Inner.class);
    110         final ClassGen cg = new ClassGen(jc);
    111         final MethodGen mg = new MethodGen(cg.getMethodAt(0), cg.getClassName(), cg.getConstantPool());
    112         final List<AnnotationEntryGen> firstParamAnnotations = mg.getAnnotationsOnParameter(0);
    113         assertEquals("Wrong number of annotations in the first parameter", 1, firstParamAnnotations.size());
    114         final List<AnnotationEntryGen> secondParamAnnotations = mg.getAnnotationsOnParameter(1);
    115         assertEquals("Wrong number of annotations in the second parameter", 1, secondParamAnnotations.size());
    116     }
    117 }
    118