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.OBJECT:
82                  addName(t.getClassName());
83                  break;
84  
85              default:
86          }
87      }
88  
89      /**
90       * <p>add.</p>
91       *
92       * @param name a {@link java.lang.String} object.
93       */
94      public void add(String name) {
95          // inner classes have equivalent compilation requirement as container class
96          if (name.indexOf('$') < 0) {
97              classes.add(name);
98          }
99      }
100 
101     void addNames(final String[] names) {
102         if (names == null) {
103             return;
104         }
105 
106         for (String name : names) {
107             addName(name);
108         }
109     }
110 
111     void addMethodDesc(final String desc) {
112         addType(Type.getReturnType(desc));
113 
114         Type[] types = Type.getArgumentTypes(desc);
115 
116         for (Type type : types) {
117             addType(type);
118         }
119     }
120 }