1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * 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, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.openejb.core.cmp; 19 20 import java.lang.reflect.Field; 21 import java.lang.reflect.Modifier; 22 23 import javax.ejb.EJBException; 24 25 import org.apache.openejb.OpenEJBException; 26 27 public abstract class AbstractKeyGenerator implements KeyGenerator { 28 public static boolean isValidPkField(Field field) { 29 int modifiers = field.getModifiers(); 30 return Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers); 31 } 32 33 public static Field getField(Class clazz, String fieldName) throws OpenEJBException { 34 try { 35 return clazz.getField(fieldName); 36 } catch (NoSuchFieldException e) { 37 throw new OpenEJBException("Unable to get primary key field from entity bean class: " + clazz.getName(), e); 38 } 39 } 40 41 public static Object getFieldValue(Field field, Object object) throws EJBException { 42 if (field == null) throw new NullPointerException("field is null"); 43 if (object == null) throw new NullPointerException("object is null"); 44 try { 45 return field.get(object); 46 } catch (Exception e) { 47 throw new EJBException("Could not get field value for field " + field, e); 48 } 49 } 50 51 public static void setFieldValue(Field field, Object object, Object value) throws EJBException { 52 if (field == null) throw new NullPointerException("field is null"); 53 if (object == null) throw new NullPointerException("object is null"); 54 try { 55 field.set(object, value); 56 } catch (Exception e) { 57 throw new EJBException("Could not set field value for field " + field, e); 58 } 59 } 60 }