01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.core.cmp.cmp2;
17:
18: import org.objectweb.asm.ClassWriter;
19: import org.objectweb.asm.MethodVisitor;
20: import org.objectweb.asm.Opcodes;
21: import org.objectweb.asm.Type;
22: import org.objectweb.asm.FieldVisitor;
23:
24: public class Cmp1Generator implements Opcodes {
25: private String implClassName;
26: private String beanClassName;
27: private ClassWriter cw;
28: private boolean unknownPk;
29:
30: public Cmp1Generator(String cmpImplClass, Class beanClass) {
31: beanClassName = Type.getInternalName(beanClass);
32: implClassName = cmpImplClass.replace('.', '/');
33:
34: cw = new ClassWriter(true);
35: }
36:
37: public byte[] generate() {
38: cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, implClassName, null,
39: beanClassName, new String[0]);
40:
41: // if we have an unknown pk, we need to add a field for the pk
42: if (unknownPk) {
43: // public Long OpenEJB_pk;
44: FieldVisitor fv = cw.visitField(ACC_PUBLIC, "OpenEJB_pk",
45: "Ljava/lang/Long;", null, null);
46: fv.visitEnd();
47: }
48:
49: createConstructor();
50:
51: cw.visitEnd();
52:
53: return cw.toByteArray();
54: }
55:
56: private void createConstructor() {
57: MethodVisitor mv = mv = cw.visitMethod(ACC_PUBLIC, "<init>",
58: "()V", null, null);
59: mv.visitCode();
60: mv.visitVarInsn(ALOAD, 0);
61: mv.visitMethodInsn(INVOKESPECIAL, beanClassName, "<init>",
62: "()V");
63:
64: mv.visitInsn(RETURN);
65: mv.visitMaxs(0, 0);
66: mv.visitEnd();
67: }
68:
69: public boolean isUnknownPk() {
70: return unknownPk;
71: }
72:
73: public void setUnknownPk(boolean unknownPk) {
74: this.unknownPk = unknownPk;
75: }
76: }
|