325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.codemodel.internal;
325N/A
325N/A
325N/Aimport java.lang.annotation.Annotation;
325N/Aimport java.util.Collections;
325N/Aimport java.util.LinkedHashMap;
325N/Aimport java.util.Map;
325N/A
325N/A/**
325N/A * Represents an annotation on a program element.
325N/A *
325N/A * TODO
325N/A * How to add enums to the annotations
325N/A * @author
325N/A * Bhakti Mehta (bhakti.mehta@sun.com)
325N/A */
325N/Apublic final class JAnnotationUse extends JAnnotationValue {
325N/A
325N/A /**
325N/A * The {@link Annotation} class
325N/A */
325N/A private final JClass clazz;
325N/A
325N/A /**
325N/A * Map of member values.
325N/A */
325N/A private Map<String,JAnnotationValue> memberValues;
325N/A
325N/A JAnnotationUse(JClass clazz){
325N/A this.clazz = clazz;
325N/A }
325N/A
325N/A public JClass getAnnotationClass() {
325N/A return clazz;
325N/A }
325N/A
325N/A public Map<String, JAnnotationValue> getAnnotationMembers() {
325N/A return Collections.unmodifiableMap(memberValues);
325N/A }
325N/A
325N/A private JCodeModel owner() {
325N/A return clazz.owner();
325N/A }
325N/A
325N/A private void addValue(String name, JAnnotationValue annotationValue) {
325N/A // Use ordered map to keep the code generation the same on any JVM.
325N/A // Lazily created.
325N/A if(memberValues==null)
325N/A memberValues = new LinkedHashMap<String, JAnnotationValue>();
325N/A memberValues.put(name,annotationValue);
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A *
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The boolean value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, boolean value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The byte member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, byte value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The char member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, char value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The double member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, double value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The float member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, float value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The long member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, long value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The short member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, short value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The int member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, int value){
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The String member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, String value){
325N/A //Escape string values with quotes so that they can
325N/A //be generated accordingly
325N/A addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * For adding class values as param
325N/A * @see #param(String, Class)
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The annotation class which is member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse annotationParam(String name, Class<? extends Annotation> value) {
325N/A JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(value));
325N/A addValue(name, annotationUse);
325N/A return annotationUse;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The enum class which is member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, final Enum<?> value) {
325N/A addValue(name, new JAnnotationValue() {
325N/A public void generate(JFormatter f) {
325N/A f.t(owner().ref(value.getDeclaringClass())).p('.').p(value.name());
325N/A }
325N/A });
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The JEnumConstant which is member value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, JEnumConstant value){
325N/A addValue(name, new JAnnotationStringValue(value));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation
325N/A * This can be used for e.g to specify
325N/A * <pre>
325N/A * &#64;XmlCollectionItem(type=Integer.class);
325N/A * <pre>
325N/A * For adding a value of Class<? extends Annotation>
325N/A * @link
325N/A * #annotationParam(java.lang.String, java.lang.Class<? extends java.lang.annotation.Annotation>)
325N/A * @param name
325N/A * The simple name for this annotation param
325N/A *
325N/A * @param value
325N/A * The class type of the param
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A *
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, final Class<?> value){
325N/A addValue(name, new JAnnotationStringValue(
325N/A new JExpressionImpl() {
325N/A public void generate(JFormatter f) {
325N/A f.p(value.getName().replace('$', '.'));
325N/A f.p(".class");
325N/A }
325N/A }));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation based on the
325N/A * type represented by the given JType
325N/A *
325N/A * @param name The simple name for this annotation param
325N/A * @param type the JType representing the actual type
325N/A * @return The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A */
325N/A public JAnnotationUse param(String name, JType type){
325N/A JClass c = type.boxify();
325N/A addValue(name, new JAnnotationStringValue ( c.dotclass() ));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair to this annotation.
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @param value
325N/A * The JExpression which provides the contant value for this annotation
325N/A * @return
325N/A * The JAnnotationUse. More member value pairs can
325N/A * be added to it using the same or the overloaded methods.
325N/A *
325N/A */
325N/A public JAnnotationUse param(String name, JExpression value){
325N/A addValue(name, new JAnnotationStringValue(value));
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Adds a member value pair which is of type array to this annotation
325N/A * @param name
325N/A * The simple name for this annotation
325N/A *
325N/A * @return
325N/A * The JAnnotationArrayMember. For adding array values
325N/A * @see JAnnotationArrayMember
325N/A *
325N/A */
325N/A public JAnnotationArrayMember paramArray(String name){
325N/A JAnnotationArrayMember arrayMember = new JAnnotationArrayMember(owner());
325N/A addValue(name, arrayMember);
325N/A return arrayMember;
325N/A }
325N/A
325N/A
325N/A// /**
325N/A// * This can be used to add annotations inside annotations
325N/A// * for e.g &#64;XmlCollection(values= &#64;XmlCollectionItem(type=Foo.class))
325N/A// * @param className
325N/A// * The classname of the annotation to be included
325N/A// * @return
325N/A// * The JAnnotationUse that can be used as a member within this JAnnotationUse
325N/A// * @deprecated
325N/A// * use {@link JAnnotationArrayMember#annotate}
325N/A// */
325N/A// public JAnnotationUse annotate(String className) {
325N/A// JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(className));
325N/A// return annotationUse;
325N/A// }
325N/A
325N/A /**
325N/A * This can be used to add annotations inside annotations
325N/A * for e.g &#64;XmlCollection(values= &#64;XmlCollectionItem(type=Foo.class))
325N/A * @param clazz
325N/A * The annotation class to be included
325N/A * @return
325N/A * The JAnnotationUse that can be used as a member within this JAnnotationUse
325N/A * @deprecated
325N/A * use {@link JAnnotationArrayMember#annotate}
325N/A */
325N/A public JAnnotationUse annotate(Class <? extends Annotation> clazz) {
325N/A JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(clazz));
325N/A return annotationUse;
325N/A }
325N/A
325N/A public void generate(JFormatter f) {
325N/A f.p('@').g(clazz);
325N/A if(memberValues!=null) {
325N/A f.p('(');
325N/A boolean first = true;
325N/A
325N/A if(isOptimizable()) {
325N/A // short form
325N/A f.g(memberValues.get("value"));
325N/A } else {
325N/A for (Map.Entry<String, JAnnotationValue> mapEntry : memberValues.entrySet()) {
325N/A if (!first) f.p(',');
325N/A f.p(mapEntry.getKey()).p('=').g(mapEntry.getValue());
325N/A first = false;
325N/A }
325N/A }
325N/A f.p(')');
325N/A }
325N/A }
325N/A
325N/A private boolean isOptimizable() {
325N/A return memberValues.size()==1 && memberValues.containsKey("value");
325N/A }
325N/A}