KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > enhancer > GetterVisitor


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  * http://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.cayenne.enhancer;
20
21 import org.apache.cayenne.ObjectContext;
22 import org.objectweb.asm.Label;
23 import org.objectweb.asm.MethodAdapter;
24 import org.objectweb.asm.MethodVisitor;
25 import org.objectweb.asm.Opcodes;
26 import org.objectweb.asm.Type;
27
28 /**
29  * @since 3.0
30  * @author Andrus Adamchik
31  */

32 public class GetterVisitor extends MethodAdapter {
33
34     private EnhancementHelper helper;
35     private String JavaDoc propertyName;
36     private boolean lazyFaulting;
37
38     public GetterVisitor(MethodVisitor mv, EnhancementHelper helper, String JavaDoc propertyName,
39             boolean lazyFaulting) {
40         super(mv);
41         this.helper = helper;
42         this.propertyName = propertyName;
43         this.lazyFaulting = lazyFaulting;
44     }
45
46     @Override JavaDoc
47     public void visitCode() {
48         super.visitCode();
49
50         String JavaDoc field = helper.getPropertyField("objectContext");
51         Type objectContextType = Type.getType(ObjectContext.class);
52
53         mv.visitVarInsn(Opcodes.ALOAD, 0);
54         mv.visitFieldInsn(
55                 Opcodes.GETFIELD,
56                 helper.getCurrentClass().getInternalName(),
57                 field,
58                 objectContextType.getDescriptor());
59         Label l1 = new Label();
60         mv.visitJumpInsn(Opcodes.IFNULL, l1);
61         mv.visitVarInsn(Opcodes.ALOAD, 0);
62         mv.visitFieldInsn(
63                 Opcodes.GETFIELD,
64                 helper.getCurrentClass().getInternalName(),
65                 field,
66                 objectContextType.getDescriptor());
67         mv.visitVarInsn(Opcodes.ALOAD, 0);
68         mv.visitLdcInsn(propertyName);
69         mv.visitInsn(lazyFaulting ? Opcodes.ICONST_1 : Opcodes.ICONST_0);
70         mv.visitMethodInsn(
71                 Opcodes.INVOKEINTERFACE,
72                 objectContextType.getInternalName(),
73                 "prepareForAccess",
74                 "(Lorg/apache/cayenne/Persistent;Ljava/lang/String;Z)V");
75         mv.visitLabel(l1);
76     }
77 }
78
Popular Tags