001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JavaxAnnotationResourceVisitor.java 2059 2007-11-22 17:22:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.analyzer;
025:
026: import static javax.annotation.Resource.AuthenticationType.APPLICATION;
027: import static javax.annotation.Resource.AuthenticationType.CONTAINER;
028:
029: import org.ow2.easybeans.asm.Type;
030:
031: import org.ow2.easybeans.deployment.annotations.impl.JAnnotationResource;
032: import org.ow2.easybeans.deployment.annotations.metadata.interfaces.IAnnotationResource;
033:
034: /**
035: * This class manages the handling of @{@link javax.annotation.Resource}
036: * annotation.
037: * @param <T> An implementation of IAnnotationResource interface.
038: * @author Florent Benoit
039: */
040: public class JavaxAnnotationResourceVisitor<T extends IAnnotationResource>
041: extends AbsAnnotationVisitor<T> implements AnnotationType {
042:
043: /**
044: * Name attribute of the annotation.
045: */
046: private static final String NAME = "name";
047:
048: /**
049: * Class with type attribute of the annotation.
050: */
051: private static final String CLASS_TYPE = "type";
052:
053: /**
054: * Atuthentication type attribute of the annotation.
055: */
056: private static final String AUTHENTICATION_TYPE = "authenticationType";
057:
058: /**
059: * Shareable attribute of the annotation.
060: */
061: private static final String SHAREABLE = "shareable";
062:
063: /**
064: * Description of the annotation.
065: */
066: private static final String DESCRIPTION = "description";
067:
068: /**
069: * Mapped name attribute of the annotation.
070: */
071: private static final String MAPPED_NAME = "mappedName";
072:
073: /**
074: * Type of annotation.
075: */
076: public static final String TYPE = "Ljavax/annotation/Resource;";
077:
078: /**
079: * Internal object used representing @{@link javax.annotation.Resource}
080: * annotation.
081: */
082: private JAnnotationResource jAnnotationResource = null;
083:
084: /**
085: * Constructor.
086: * @param annotationMetadata linked to a class or method metadata
087: */
088: public JavaxAnnotationResourceVisitor(final T annotationMetadata) {
089: super (annotationMetadata);
090: jAnnotationResource = new JAnnotationResource();
091: }
092:
093: /**
094: * Visits a primitive value of the annotation.<br>
095: * @param name the value name.
096: * @param value the actual value, whose type must be {@link Byte},
097: * {@link Boolean}, {@link Character}, {@link Short},
098: * {@link Integer}, {@link Long}, {@link Float}, {@link Double},
099: * {@link String} or {@link org.ow2.easybeans.asm.Type}.
100: */
101: @Override
102: public void visit(final String name, final Object value) {
103: if (name.equals(NAME)) {
104: jAnnotationResource.setName((String) value);
105: } else if (name.equals(CLASS_TYPE)) {
106: Type type = (Type) value;
107: jAnnotationResource.setType(type.getClassName());
108: } else if (name.equals(SHAREABLE)) {
109: jAnnotationResource.setShareable(((Boolean) value)
110: .booleanValue());
111: } else if (name.equals(DESCRIPTION)) {
112: jAnnotationResource.setDescription((String) value);
113: } else if (name.equals(MAPPED_NAME)) {
114: jAnnotationResource.setMappedName((String) value);
115: }
116: }
117:
118: /**
119: * Visits an enumeration value of the annotation.
120: * @param name the value name.
121: * @param desc the class descriptor of the enumeration class.
122: * @param value the actual enumeration value.
123: */
124: @Override
125: public void visitEnum(final String name, final String desc,
126: final String value) {
127: if (name.equals(AUTHENTICATION_TYPE)) {
128: if (CONTAINER.name().equals(value)) {
129: jAnnotationResource.setAuthenticationType(CONTAINER);
130: } else if (APPLICATION.name().equals(value)) {
131: jAnnotationResource.setAuthenticationType(APPLICATION);
132: }
133: }
134: }
135:
136: /**
137: * Visits the end of the annotation. <br>
138: * Creates the object and store it.
139: */
140: @Override
141: public void visitEnd() {
142: // add object
143: getAnnotationMetadata().setJAnnotationResource(
144: jAnnotationResource);
145: }
146:
147: /**
148: * @return type of the annotation (its description)
149: */
150: public String getType() {
151: return TYPE;
152: }
153:
154: /**
155: * @return Internal object used representing @{@link javax.annotation.Resource}
156: * annotation.
157: */
158: protected JAnnotationResource getJAnnotationResource() {
159: return jAnnotationResource;
160: }
161:
162: /**
163: * Sets the jAnnotationResource object.
164: * @param jAnnotationResource the object which replaced the previous one.
165: */
166: protected void setJAnnotationResource(
167: final JAnnotationResource jAnnotationResource) {
168: this.jAnnotationResource = jAnnotationResource;
169: }
170:
171: }
|