View Javadoc

1   /*
2    * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
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 de.smartics.maven.util.report.link;
17  
18  import java.io.Serializable;
19  
20  import javax.annotation.CheckForNull;
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  import de.smartics.util.source.MethodInfo;
25  
26  /**
27   * A reference to a Java element. This is string based since the class is not
28   * necessarily on the class path of the report.
29   *
30   * @todo Support nested classes.
31   * @todo Support parameter lists.
32   * @todo check for serial tampering
33   */
34  public class JavaElementRef implements Serializable
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    /**
41     * The class version identifier.
42     */
43    private static final long serialVersionUID = 1L;
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The name of the package.
49     *
50     * @serial
51     */
52    private final String packageName;
53  
54    /**
55     * The name of the Java class.
56     *
57     * @serial
58     */
59    private final String typeName;
60  
61    /**
62     * The name of the element.
63     *
64     * @serial
65     */
66    private final MethodInfo elementName;
67  
68    // ****************************** Initializer *******************************
69  
70    // ****************************** Constructors ******************************
71  
72    /**
73     * Default constructor.
74     *
75     * @param packageName the name of the package.
76     * @param typeName the name of the Java class.
77     * @param elementName the name of the element.
78     * @throws IllegalArgumentException if <code>typeName</code> is blank or
79     *           <code>packageName</code> is empty or consists only of
80     *           whitespaces.
81     * @todo All elements are currently provided to be fully URL encoded.
82     */
83    public JavaElementRef(final String packageName, final String typeName,
84        final MethodInfo elementName) throws IllegalArgumentException
85    {
86      checkArguments(packageName, typeName);
87  
88      this.packageName = packageName;
89      this.typeName = typeName;
90      this.elementName = elementName;
91    }
92  
93    // ****************************** Inner Classes *****************************
94  
95    // ********************************* Methods ********************************
96  
97    // --- init -----------------------------------------------------------------
98  
99    private static void checkArguments(final String packageName,
100       final String typeName) throws IllegalArgumentException
101   {
102     if (StringUtils.isBlank(typeName))
103     {
104       throw new IllegalArgumentException("The type name must not be blank.");
105     }
106     if (packageName != null && StringUtils.isBlank(packageName))
107     {
108       throw new IllegalArgumentException(
109           "The package name may be null, but must not be blank.");
110     }
111   }
112 
113   // --- get&set --------------------------------------------------------------
114 
115   /**
116    * Returns the name of the package.
117    *
118    * @return the name of the package.
119    */
120   public String getPackageName()
121   {
122     return packageName;
123   }
124 
125   /**
126    * Returns the name of the Java class.
127    *
128    * @return the name of the Java class.
129    */
130   public String getTypeName()
131   {
132     return typeName;
133   }
134 
135   /**
136    * Returns the name of the element.
137    *
138    * @return the name of the element. May be <code>null</code>.
139    */
140   @CheckForNull
141   public MethodInfo getElementName()
142   {
143     return elementName;
144   }
145 
146   // --- business -------------------------------------------------------------
147 
148   // --- object basics --------------------------------------------------------
149 
150   /**
151    * Returns the string representation of the object.
152    *
153    * @return the string representation of the object.
154    */
155   @Override
156   public String toString()
157   {
158     final StringBuilder buffer = new StringBuilder(64);
159 
160     if (packageName != null)
161     {
162       buffer.append(packageName).append('.');
163     }
164 
165     buffer.append(typeName);
166 
167     if (elementName != null)
168     {
169       buffer.append('.').append(elementName.getMethodSignature());
170     }
171 
172     return buffer.toString();
173   }
174 }