View Javadoc
1   package org.apache.maven.tools.plugin.javadoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Objects;
23  import java.util.Optional;
24  
25  /**
26   * Wraps a fully qualified (and resolved) code reference used in javadoc tags {@code see}, {@code link} and
27   * {@code linkplain}. Similar to {@link JavadocReference} but can distinguish between package names and class names. The
28   * package name is always set for a resolved reference (except for references to modules). The member is always the
29   * normalized form containing only fully qualified type names (without argument names), separated by {@code ,} without
30   * any whitespace characters. Also the member type is always resolved to one of {@link MemberType} (in case the
31   * reference contains a member part).
32   */
33  public class FullyQualifiedJavadocReference
34      extends JavadocReference
35  {
36  
37      /** if false, points to a class/package which is part of the current classloader (and not any of its parents) */
38      private final boolean isExternal;
39  
40      private final Optional<String> packageName;
41  
42      private final Optional<MemberType> memberType;
43  
44      /** The type of the member part of the reference. */
45      public enum MemberType
46      {
47          FIELD, METHOD, CONSTRUCTOR
48      }
49  
50      public FullyQualifiedJavadocReference( String packageName, boolean isExternal )
51      {
52          this( packageName, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), isExternal );
53      }
54  
55      public FullyQualifiedJavadocReference( String packageName, Optional<String> label, boolean isExternal )
56      {
57          this( packageName, Optional.empty(), Optional.empty(), Optional.empty(), label, isExternal );
58      }
59  
60      public FullyQualifiedJavadocReference( String packageName, String className, boolean isExternal )
61      {
62          this( packageName, Optional.of( className ), Optional.empty(), Optional.empty(), Optional.empty(),
63                isExternal );
64      }
65  
66      public FullyQualifiedJavadocReference( String packageName, String className, String member, MemberType memberType,
67                                             boolean isExternal )
68      {
69          this( packageName, Optional.of( className ), Optional.of( member ), Optional.of( memberType ),
70                Optional.empty(), isExternal );
71      }
72  
73      public FullyQualifiedJavadocReference( String packageName, Optional<String> className, Optional<String> member,
74                                             Optional<MemberType> memberType, Optional<String> label, boolean isExternal )
75      {
76          this( Optional.empty(), Optional.of( packageName ), className, member, memberType, label, isExternal );
77      }
78  
79      public FullyQualifiedJavadocReference( Optional<String> moduleName, Optional<String> packageName,
80                                             Optional<String> className, Optional<String> member,
81                                             Optional<MemberType> memberType, Optional<String> label,
82                                             boolean isExternal )
83      {
84          super( moduleName, className, member, label );
85          this.packageName = packageName;
86          this.isExternal = isExternal;
87          if ( !moduleName.isPresent() && !packageName.isPresent() )
88          {
89              throw new IllegalArgumentException( "At least one of module name or package name needs to be set" );
90          }
91          if ( member.isPresent() )
92          {
93              if ( !memberType.isPresent() )
94              {
95                  throw new IllegalArgumentException( "When member is set, also the member type needs to be set" );
96              }
97              if ( member.get().matches( ".*\\s.*" ) )
98              {
99                  throw new IllegalArgumentException( "member must not contain any whitespace characters!" );
100             }
101         }
102         this.memberType = memberType;
103     }
104 
105     /**
106      * 
107      * @return {@code true} in case this class/package is part of another classloader
108      */
109     public boolean isExternal()
110     {
111         return isExternal;
112     }
113 
114     /** @return the package name of the referenced class */
115     public Optional<String> getPackageName()
116     {
117         return packageName;
118     }
119 
120     /**
121      * @return the simple class name of the referenced class, may be prefixed by the declaring class names, separated by
122      *         '.' (for inner classes)
123      */
124     public Optional<String> getClassName()
125     {
126         return getPackageNameClassName();
127     }
128 
129     /** @return the type of the member. Only empty if no member is set. */
130     public Optional<MemberType> getMemberType()
131     {
132         return memberType;
133     }
134 
135     public Optional<String> getFullyQualifiedClassName()
136     {
137         if ( getClassName().isPresent() && getPackageName().isPresent() )
138         {
139             return Optional.of( getPackageName().get() + "." + getClassName().get() );
140         }
141         else
142         {
143             return Optional.empty();
144         }
145     }
146 
147     @Override
148     public String toString()
149     {
150         return "FullyQualifiedJavadocReference [moduleName=" + getModuleName() + ", packageName=" + packageName
151             + ", className=" + getClassName() + ", memberType=" + memberType + ", member=" + getMember() + ", label="
152             + getLabel() + ", isExternal=" + isExternal + "]";
153     }
154 
155     @Override
156     public int hashCode()
157     {
158         final int prime = 31;
159         int result = super.hashCode();
160         result = prime * result + Objects.hash( memberType, packageName, isExternal );
161         return result;
162     }
163 
164     @Override
165     public boolean equals( Object obj )
166     {
167         if ( this == obj )
168         {
169             return true;
170         }
171         if ( !super.equals( obj ) )
172         {
173             return false;
174         }
175         if ( getClass() != obj.getClass() )
176         {
177             return false;
178         }
179         FullyQualifiedJavadocReference other = (FullyQualifiedJavadocReference) obj;
180         return Objects.equals( memberType, other.memberType ) && Objects.equals( packageName, other.packageName )
181                         && isExternal == other.isExternal;
182     }
183 
184 }