KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.cayenne.map.EntityResolver;
25 import org.apache.cayenne.map.ObjEntity;
26 import org.objectweb.asm.ClassVisitor;
27 import org.objectweb.asm.commons.SerialVersionUIDAdder;
28
29 /**
30  * A factory of enhacing visitors.
31  *
32  * @since 3.0
33  * @author Andrus Adamchik
34  */

35 public class CayenneEnhancerVisitorFactory implements EnhancerVisitorFactory {
36
37     protected Map JavaDoc<String JavaDoc, ObjEntity> entitiesByClass;
38
39     public CayenneEnhancerVisitorFactory(EntityResolver entityResolver) {
40         indexEntities(entityResolver);
41     }
42
43     protected void indexEntities(EntityResolver entityResolver) {
44         // EntityResolver doesn't have an index by class name, (let alone using
45
// "internal" class names with slashes as keys), so we have to build it
46
// manually
47

48         this.entitiesByClass = new HashMap JavaDoc<String JavaDoc, ObjEntity>();
49         for (Object JavaDoc object : entityResolver.getObjEntities()) {
50             ObjEntity entity = (ObjEntity) object;
51             entitiesByClass.put(entity.getClassName(), entity);
52         }
53     }
54
55     public ClassVisitor createVisitor(String JavaDoc className, ClassVisitor out) {
56         ObjEntity entity = entitiesByClass.get(className.replace('/', '.'));
57         if (entity == null) {
58             return null;
59         }
60
61         // create enhancer chain
62
PersistentInterfaceVisitor e1 = new PersistentInterfaceVisitor(out);
63         PersistentAccessorVisitor e2 = new PersistentAccessorVisitor(e1, entity);
64
65         // this ensures that both enhanced and original classes have compatible serialized
66
// format even if no serialVersionUID is defined by the user
67
SerialVersionUIDAdder e3 = new SerialVersionUIDAdder(e2);
68         return e3;
69     }
70 }
71
Popular Tags