001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.cmp.cmp2;
018:
019: import java.util.Set;
020: import java.util.HashSet;
021: import java.util.Collection;
022: import java.lang.reflect.Method;
023:
024: import org.objectweb.asm.Type;
025:
026: public enum CmrStyle {
027: SINGLE(SingleValuedCmr.class, null, null), COLLECTION(
028: SetValuedCmr.class, Collection.class, HashSet.class), SET(
029: SetValuedCmr.class, Set.class, HashSet.class);
030:
031: private final Type accessorType;
032: private final Type collectionType;
033: private final Type intiCollectionType;
034: private final String getterDescriptor;
035: private final String setterDescriptor;
036: private final String deletedDescriptor;
037:
038: CmrStyle(Class accessorClass, Class collectionClass,
039: Class initCollectionClass) {
040: this .accessorType = Type.getType(accessorClass);
041: if (collectionClass != null) {
042: this .collectionType = Type.getType(collectionClass);
043: this .intiCollectionType = Type.getType(initCollectionClass);
044: } else {
045: this .collectionType = null;
046: this .intiCollectionType = null;
047: }
048:
049: String getterDescriptor = null;
050: String setterDescriptor = null;
051: String deletedDescriptor = null;
052: for (Method method : accessorClass.getMethods()) {
053: if ("get".equals(method.getName())) {
054: getterDescriptor = Type.getMethodDescriptor(method);
055: }
056: if ("set".equals(method.getName())) {
057: setterDescriptor = Type.getMethodDescriptor(method);
058: }
059: if ("deleted".equals(method.getName())) {
060: deletedDescriptor = Type.getMethodDescriptor(method);
061: }
062: }
063: if (getterDescriptor == null) {
064: throw new AssertionError(
065: "No get method found in cmr accessor class "
066: + accessorClass.getName());
067: }
068: if (setterDescriptor == null) {
069: throw new AssertionError(
070: "No set method found in cmr accessor class "
071: + accessorClass.getName());
072: }
073: if (deletedDescriptor == null) {
074: throw new AssertionError(
075: "No deleted method found in cmr accessor class "
076: + accessorClass.getName());
077: }
078: this .getterDescriptor = getterDescriptor;
079: this .setterDescriptor = setterDescriptor;
080: this .deletedDescriptor = deletedDescriptor;
081: }
082:
083: public String getCmrFieldDescriptor(Type relatedType) {
084: String relatedDescriptor = relatedType.getDescriptor();
085: if (collectionType != null) {
086: return collectionType.getDescriptor() + "<"
087: + relatedDescriptor + ">";
088: } else {
089: return relatedDescriptor;
090: }
091: }
092:
093: public Type getAccessorType() {
094: return accessorType;
095: }
096:
097: public Type getCollectionType() {
098: return collectionType;
099: }
100:
101: public Type getIntiCollectionType() {
102: return intiCollectionType;
103: }
104:
105: public String getGetterDescriptor() {
106: return getterDescriptor;
107: }
108:
109: public String getSetterDescriptor() {
110: return setterDescriptor;
111: }
112:
113: public String getDeletedDescriptor() {
114: return deletedDescriptor;
115: }
116: }
|