/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpamodelgen.annotation;
import java.beans.Introspector;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.util.Elements;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.MetaEntity;
/**
* Captures all information about an annotated persistent attribute.
*
* @author Max Andersen
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
public abstract class AnnotationMetaAttribute implements MetaAttribute {
private final Element element;
private final AnnotationMetaEntity parent;
private final String type;
public AnnotationMetaAttribute(AnnotationMetaEntity parent, Element element, String type) {
this.element = element;
this.parent = parent;
this.type = type;
}
public String getDeclarationString() {
return new StringBuilder().append( "public static volatile " )
.append( parent.importType( getMetaType() ) )
.append( "<" )
.append( parent.importType( parent.getQualifiedName() ) )
.append( ", " )
.append( parent.importType( getTypeDeclaration() ) )
.append( "> " )
.append( getPropertyName() )
.append( ";" )
.toString();
}
public String getPropertyName() {
Elements elementsUtil = parent.getContext().getElementUtils();
if ( element.getKind() == ElementKind.FIELD ) {
return element.getSimpleName().toString();
}
else if ( element.getKind() == ElementKind.METHOD ) {
String name = element.getSimpleName().toString();
if ( name.startsWith( "get" ) ) {
return elementsUtil.getName( Introspector.decapitalize( name.substring( "get".length() ) ) ).toString();
}
else if ( name.startsWith( "is" ) ) {
return ( elementsUtil.getName( Introspector.decapitalize( name.substring( "is".length() ) ) ) ).toString();
}
return elementsUtil.getName( Introspector.decapitalize( name ) ).toString();
}
else {
return elementsUtil.getName( element.getSimpleName() + "/* " + element.getKind() + " */" ).toString();
}
}
public MetaEntity getHostingEntity() {
return parent;
}
public abstract String getMetaType();
public String getTypeDeclaration() {
return type;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "AnnotationMetaAttribute" );
sb.append( "{element=" ).append( element );
sb.append( ", type='" ).append( type ).append( '\'' );
sb.append( '}' );
return sb.toString();
}
}