View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.jar.classes;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.commons.collections4.list.SetUniqueList;
26  
27  /**
28   * Gathered facts about the classes within a JAR file.
29   *
30   * @see org.apache.maven.shared.jar.classes.JarClassesAnalysis#analyze(org.apache.maven.shared.jar.JarAnalyzer)
31   */
32  public class JarClasses {
33      /**
34       * The list of imports in the classes in the JAR.
35       */
36      private final List<String> imports;
37  
38      /**
39       * A list of packages represented by classes in the JAR.
40       */
41      private final List<String> packages;
42  
43      /**
44       * A list of the classes that in the JAR.
45       */
46      private final List<String> classNames;
47  
48      /**
49       * A list of methods within the classes in the JAR.
50       */
51      private final List<String> methods;
52  
53      /**
54       * Whether the JAR contains any code with debug information. If there is a mix of debug and release code, this will
55       * still be true.
56       */
57      private boolean isDebugPresent;
58  
59      /**
60       * The highest JVM revision available in any class files. While the JAR may work on earlier JVMs if particular
61       * classes are not used, this is the minimum JVM that guarantees compatibility.
62       */
63      private String jdkRevision;
64  
65      /**
66       * Constructor to create an empty instance.
67       */
68      public JarClasses() {
69          // Unique list decorators are used to ensure natural ordering is retained, the list interface is availble, and
70          // that duplicates are not entered.
71          imports = SetUniqueList.setUniqueList(new ArrayList<>());
72          packages = SetUniqueList.setUniqueList(new ArrayList<>());
73          classNames = SetUniqueList.setUniqueList(new ArrayList<>());
74          methods = SetUniqueList.setUniqueList(new ArrayList<>());
75      }
76  
77      /**
78       * Add a discovered class to the record.
79       *
80       * @param name the name of the class
81       */
82      public void addClassName(String name) {
83          this.classNames.add(name);
84      }
85  
86      /**
87       * Add a discovered package to the record.
88       *
89       * @param name the name of the package
90       */
91      public void addPackage(String name) {
92          this.packages.add(name);
93      }
94  
95      /**
96       * Add a discovered method to the record.
97       *
98       * @param name the name of the method
99       */
100     public void addMethod(String name) {
101         this.methods.add(name);
102     }
103 
104     /**
105      * Add a list of discovered imports to the record.
106      *
107      * @param imports the imports to add. Each item should be a String to avoid down the line ClassCastExceptions.
108      */
109     public void addImports(List<String> imports) {
110         this.imports.addAll(imports);
111     }
112 
113     public List<String> getImports() {
114         return Collections.unmodifiableList(imports);
115     }
116 
117     public List<String> getClassNames() {
118         return Collections.unmodifiableList(classNames);
119     }
120 
121     public List<String> getPackages() {
122         return Collections.unmodifiableList(packages);
123     }
124 
125     public boolean isDebugPresent() {
126         return isDebugPresent;
127     }
128 
129     public void setDebugPresent(boolean hasDebugSymbols) {
130         this.isDebugPresent = hasDebugSymbols;
131     }
132 
133     public String getJdkRevision() {
134         return jdkRevision;
135     }
136 
137     public void setJdkRevision(String jdkRevision) {
138         this.jdkRevision = jdkRevision;
139     }
140 
141     public List<String> getMethods() {
142         return Collections.unmodifiableList(methods);
143     }
144 }