📄 annotedelementinferreddata.java
字号:
//$Id: AnnotedElementInferredData.java,v 1.6 2004/10/08 20:55:44 epbernard Exp $package org.hibernate.cfg;import java.beans.Introspector;import java.lang.reflect.AnnotatedElement;import java.lang.reflect.Field;import java.lang.reflect.Member;import java.lang.reflect.Method;import java.lang.reflect.Type;import java.lang.reflect.ParameterizedType;import org.hibernate.MappingException;/** * Retrieve all inferred data from an annnoted element * Currently support field and getter processing. * An exception is thrown when the annoted element does not fit. * The calculation is lazied until the first access to a public getter. * This prevent useless reflection. * * @author Emmanuel Bernard */public class AnnotedElementInferredData { private String defaultAccess; private String propertyName; private Class returnedClass; private Class collectionType; private String returnedClassName; private final AnnotatedElement annotedElt; private boolean processed; private boolean annotable; private static final String EXCEPTION_MESSAGE = "The annoted inferred element should be a field or a getter"; /** * Take the annoted element for lazy process * * @param annotedElt element to process */ public AnnotedElementInferredData(AnnotatedElement annotedElt) { this.annotedElt = annotedElt; } /** * Process an annoted element and give a final * structure. * The annoted element must be either field or getter * * @throws MappingException No getter or field found or wrong JavaBean spec usage */ private void execute(boolean throwException) throws MappingException { if (! processed) { /* * guess access from annoted element type * retrieve property name. */ annotable = true; if ( ! (annotedElt instanceof Member) ) { throw new MappingException(EXCEPTION_MESSAGE); } propertyName = retrievePropertyNameFromMember( (Member) annotedElt, throwException ); if (annotedElt instanceof Field) { Field field = (Field) annotedElt; defaultAccess = "field"; returnedClass = field.getType(); collectionType = findCollectionType(field); } else if (annotedElt instanceof Method) { Method method = (Method) annotedElt; defaultAccess = "property"; returnedClass = method.getReturnType(); collectionType = findCollectionType(method); } else { throw new MappingException(EXCEPTION_MESSAGE); } if ( returnedClass.isArray() ) { returnedClass = returnedClass.getComponentType(); } returnedClassName = returnedClass.getName(); processed = true; } } private Class findCollectionType(Method method) { Type t = method.getGenericReturnType(); return extractType(t); } private Class findCollectionType(Field field) { Type t = field.getGenericType(); return extractType(t); } private Class extractType(Type t) { if (t != null && t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; Type[] genTypes = pt.getActualTypeArguments(); if (genTypes.length == 1 && genTypes[0] instanceof Class) { return (Class) genTypes[0]; } } return null; } /** * Build the property name from the Member one using JavaBean conventions * * @param member member used to build * @return property name * * @throws MappingException annoted part not on a getter method */ private String retrievePropertyNameFromMember(Member member, boolean throwException) throws MappingException { if (member instanceof Field) { return member.getName(); } else if (member instanceof Method) { //TODO: check arg nbr, no static nor return value final String methName = member.getName(); if ( methName.startsWith( "get" ) ) { return Introspector.decapitalize( methName.substring( "get".length() ) ); } else if ( methName.startsWith("is") ) { return Introspector.decapitalize( methName.substring( "is".length() ) ); } else { annotable = false; if (throwException) { throw new MappingException("Annoted Method is not a proper getter: " + methName); } else { return methName; } } } else { throw new MappingException(EXCEPTION_MESSAGE); } } /** * @return default member access (whether field or property) * @throws MappingException No getter or field found or wrong JavaBean spec usage */ public String getDefaultAccess() throws MappingException { execute(true); return defaultAccess; } /** * @return property name * @throws MappingException No getter or field found or wrong JavaBean spec usage */ public String getPropertyName() throws MappingException { execute(true); return propertyName; } /** * @return returned class * @throws MappingException No getter or field found or wrong JavaBean spec usage */ public Class getReturnedClass() throws MappingException { execute(true); return returnedClass; } /** * @return returned class name * @throws MappingException No getter or field found or wrong JavaBean spec usage */ public String getReturnedClassName() throws MappingException { execute(true); return returnedClassName; } /** * @return whether this element is an EJB3 annotable element */ public boolean isAnnotable() { execute(false); return annotable; } /** * @return collection type if any, or null otherwise */ public Class getCollectionType() { return collectionType; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -