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