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.dependency.analyzer.asm;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  import java.util.stream.Collectors;
24  
25  import org.apache.maven.shared.dependency.analyzer.DependencyUsage;
26  import org.objectweb.asm.Type;
27  
28  /**
29   * <p>ResultCollector class.</p>
30   *
31   * @author Kristian Rosenvold
32   */
33  public class ResultCollector {
34  
35      private final Set<DependencyUsage> classUsages = new HashSet<>();
36  
37      /**
38       * <p>getDependencies.</p>
39       *
40       * @return a {@link java.util.Set} object.
41       */
42      public Set<String> getDependencies() {
43          return getDependencyUsages().stream()
44                  .map(DependencyUsage::getDependencyClass)
45                  .collect(Collectors.toSet());
46      }
47  
48      /**
49       * <p>getDependencyUsages.</p>
50       *
51       * @return a {@link java.util.Set} object.
52       */
53      public Set<DependencyUsage> getDependencyUsages() {
54          return classUsages;
55      }
56  
57      /**
58       * <p>addName.</p>
59       *
60       * @param name a {@link java.lang.String} object.
61       */
62      public void addName(final String usedByClass, String name) {
63          if (name == null) {
64              return;
65          }
66  
67          // decode arrays
68          if (name.charAt(0) == '[') {
69              int i = 0;
70              do {
71                  ++i;
72              } while (name.charAt(i) == '['); // could have array of array ...
73              if (name.charAt(i) != 'L') {
74                  // ignore array of scalar types
75                  return;
76              }
77              name = name.substring(i + 1, name.length() - 1);
78          }
79  
80          // decode internal representation
81          add(usedByClass, name.replace('/', '.'));
82      }
83  
84      void addDesc(final String usedByClass, final String desc) {
85          addType(usedByClass, Type.getType(desc));
86      }
87  
88      void addType(final String usedByClass, final Type t) {
89          switch (t.getSort()) {
90              case Type.ARRAY:
91                  addType(usedByClass, t.getElementType());
92                  break;
93  
94              case Type.METHOD:
95                  addMethodDesc(usedByClass, t.getDescriptor());
96                  break;
97  
98              case Type.OBJECT:
99                  addName(usedByClass, t.getClassName());
100                 break;
101             default:
102         }
103     }
104 
105     /**
106      * <p>add.</p>
107      *
108      * @param name a {@link java.lang.String} object.
109      */
110     public void add(final String usedByClass, final String name) {
111         // inner classes have equivalent compilation requirement as container class
112         if (name.indexOf('$') < 0) {
113             classUsages.add(new DependencyUsage(name, usedByClass));
114         }
115     }
116 
117     void addNames(final String usedByClass, final String[] names) {
118         if (names == null) {
119             return;
120         }
121 
122         for (String name : names) {
123             addName(usedByClass, name);
124         }
125     }
126 
127     void addMethodDesc(final String usedByClass, final String desc) {
128         addType(usedByClass, Type.getReturnType(desc));
129 
130         Type[] types = Type.getArgumentTypes(desc);
131 
132         for (Type type : types) {
133             addType(usedByClass, type);
134         }
135     }
136 }