KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > InvalidAttributeTargetError


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Thrown when an attribute has a {@link Target} declaration that forbids
23  * it being applied to the program element it has been applied to.
24  *
25  * <p>For example:
26  *
27  * <pre><code>
28  * / **
29  * * This attribute can only be applied to Classes.
30  * * Target(Target.CLASS)
31  * * /
32  * public class MyAttribute {}
33  *
34  * public class MyClass {
35  * / **
36  * * Error: Can't apply MyAttribute to a field!
37  * * MyAttribute()
38  * * /
39  * private String myField;
40  * }
41  * </code></pre>
42  */

43 public class InvalidAttributeTargetError extends Error JavaDoc {
44     
45     public InvalidAttributeTargetError (String JavaDoc attributeClass, String JavaDoc element, int targetFlags) {
46         super ("Attributes of type " + attributeClass + " can't be applied to " + element + ". " +
47             "They can only be applied to: " + flagsToString (targetFlags));
48     }
49     
50     private final static String JavaDoc flagsToString (int flags) {
51         List JavaDoc targetNames = new ArrayList JavaDoc ();
52         if ((flags & Target.CLASS) > 0) {
53             targetNames.add ("CLASS");
54         }
55         if ((flags & Target.FIELD) > 0) {
56             targetNames.add ("FIELD");
57         }
58         if ((flags & Target.METHOD) > 0) {
59             targetNames.add ("METHOD");
60         }
61         if ((flags & Target.CONSTRUCTOR) > 0) {
62             targetNames.add ("CONSTRUCTOR");
63         }
64         if ((flags & Target.METHOD_PARAMETER) > 0) {
65             targetNames.add ("METHOD_PARAMETER");
66         }
67         if ((flags & Target.CONSTRUCTOR_PARAMETER) > 0) {
68             targetNames.add ("CONSTRUCTOR_PARAMETER");
69         }
70         if ((flags & Target.RETURN) > 0) {
71             targetNames.add ("RETURN");
72         }
73         
74         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
75         for (int i = 0; i < targetNames.size (); i++) {
76             sb.append (targetNames.get (i));
77             if (i < targetNames.size () - 1) {
78                 sb.append (" | ");
79             }
80         }
81         return sb.toString ();
82     }
83     
84 }
Popular Tags