View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.internal;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  /**
11   * Identifies a class by its loader and name rather than by the <code>Class</code> object, which isn't available during
12   * initial class transformation.
13   */
14  public final class ClassIdentification {
15      @Nullable
16      public final ClassLoader loader;
17      @NonNull
18      public final String name;
19  
20      public ClassIdentification(@Nullable ClassLoader loader, @NonNull String name) {
21          this.loader = loader;
22          this.name = name;
23      }
24  
25      @NonNull
26      public Class<?> getLoadedClass() {
27          try {
28              return Class.forName(name, false, loader);
29          } catch (ClassNotFoundException e) {
30              throw new RuntimeException(e);
31          }
32      }
33  
34      @Override
35      public boolean equals(Object o) {
36          if (this == o) {
37              return true;
38          }
39          if (o == null || getClass() != o.getClass()) {
40              return false;
41          }
42  
43          ClassIdentification other = (ClassIdentification) o;
44          return loader == other.loader && name.equals(other.name);
45      }
46  
47      @Override
48      public int hashCode() {
49          return loader == null ? name.hashCode() : 31 * loader.hashCode() + name.hashCode();
50      }
51  }