/
ClassPathEntry.java
129 lines (112 loc) · 4.25 KB
/
ClassPathEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.tools.opensource.classpath;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import com.google.cloud.tools.opensource.dependencies.Artifacts;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath.ClassInfo;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Objects;
import org.apache.bcel.classfile.JavaClass;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
/** An entry in a class path. */
public final class ClassPathEntry {
// Either jar or artifact is non-null.
private Path jar;
private Artifact artifact;
private ImmutableSet<String> classFileNames;
/** An entry for a JAR file without association with a Maven artifact. */
ClassPathEntry(Path jar) {
this.jar = checkNotNull(jar);
}
/** An entry for a Maven artifact. */
public ClassPathEntry(Artifact artifact) {
checkNotNull(artifact.getFile());
this.artifact = artifact;
}
/** Returns the path to JAR file. */
Path getJar() {
if (artifact != null) {
return artifact.getFile().toPath();
} else {
return jar;
}
}
/**
* Returns Maven artifact associated with the JAR file. If the JAR file does not have an artifact,
* {@code null}.
*/
Artifact getArtifact() {
return artifact;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
ClassPathEntry that = (ClassPathEntry) other;
return Objects.equals(jar, that.jar) && Objects.equals(artifact, that.artifact);
}
@Override
public int hashCode() {
return Objects.hash(jar, artifact);
}
@Override
public String toString() {
if (artifact != null) {
// Group ID, artifact ID and version. No extension such as "jar" or "tar.gz", because Linkage
// Checker uses only JAR artifacts.
return Artifacts.toCoordinates(artifact);
} else {
return jar.toString();
}
}
@VisibleForTesting
public static ClassPathEntry of(String coordinates, String filePath) {
Artifact artifact = new DefaultArtifact(coordinates);
return new ClassPathEntry(artifact.setFile(new File(filePath)));
}
/**
* Returns a list of class file names in {@link #jar} as in {@link JavaClass#getFileName()}. This
* class file name is a path ("." as element separator) that locates a class file in a class path.
* Usually the class name and class file name are the same. However a class file name may have a
* framework-specific prefix. Example: {@code BOOT-INF.classes.com.google.Foo}.
*/
// Could do this on construction but that makes construction slow and throw an IOException.
// Is this OK? Or maybe lazy load in getClassFileNames?
public void listClassFileNames() throws IOException {
URL jarUrl = getJar().toUri().toURL();
// Setting parent as null because we don't want other classes than this jar file
URLClassLoader classLoaderFromJar = new URLClassLoader(new URL[] {jarUrl}, null);
// Leveraging Google Guava reflection as BCEL doesn't have API to list classes in a jar file
com.google.common.reflect.ClassPath classPath =
com.google.common.reflect.ClassPath.from(classLoaderFromJar);
classFileNames = classPath.getAllClasses().stream().map(ClassInfo::getName).collect(toImmutableSet());
}
public ImmutableSet<String> getClassFileNames() {
return classFileNames;
}
}