Skip to content

Commit 41bc9d5

Browse files
committed
Refactor irrelevant code from ClasspathResourceLoader to InstrumentationUtils
1 parent 23bb014 commit 41bc9d5

File tree

6 files changed

+93
-61
lines changed

6 files changed

+93
-61
lines changed

net.tascalate.javaflow.spi/src/main/java/org/apache/commons/javaflow/spi/ClasspathResourceLoader.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,4 @@ public ClassMatcher createVeto() throws IOException {
7575
}
7676
return matchers.isEmpty() ? ClassMatchers.MATCH_NONE : ClassMatchers.whenAny(matchers);
7777
}
78-
79-
public static String packageNameOfClass(Class<?> clazz) {
80-
String className = clazz.getName();
81-
int lastDot = className.lastIndexOf('.');
82-
if (lastDot > 0) {
83-
return className.substring(0, lastDot);
84-
} else {
85-
return null;
86-
}
87-
}
88-
89-
/**
90-
* Check if <code>maybeParent</code> is a parent (probably inderect) of the <code>classLoader</code>
91-
* @param classLoader The classloader whose parents are checked, may not be null
92-
* @param maybeParent Possible parent, may be null for boot class loader
93-
* @return
94-
*/
95-
public static boolean isClassLoaderParent(ClassLoader classLoader, ClassLoader maybeParent) {
96-
ClassLoader cl = classLoader;
97-
do {
98-
cl = cl.getParent();
99-
if (maybeParent == cl) {
100-
// Check includes null == null for bootstrap classloader
101-
return true;
102-
}
103-
} while (cl != null);
104-
return false;
105-
}
10678
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Original work: copyright 1999-2004 The Apache Software Foundation
3+
* (http://www.apache.org/)
4+
*
5+
* This project is based on the work licensed to the Apache Software
6+
* Foundation (ASF) under one or more contributor license agreements.
7+
* See the NOTICE file distributed with this work for additional
8+
* information regarding copyright ownership.
9+
*
10+
* Modified work: copyright 2013-2019 Valery Silaev (http://vsilaev.com)
11+
*
12+
* Licensed under the Apache License, Version 2.0 (the "License");
13+
* you may not use this file except in compliance with the License.
14+
* You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
package org.apache.commons.javaflow.spi;
25+
26+
import java.util.HashSet;
27+
import java.util.Set;
28+
29+
public final class InstrumentationUtils {
30+
31+
private InstrumentationUtils() {}
32+
33+
public static String packageNameOf(Class<?> clazz) {
34+
String className = clazz.getName();
35+
int lastDot = className.lastIndexOf('.');
36+
if (lastDot > 0) {
37+
return className.substring(0, lastDot);
38+
} else {
39+
return null;
40+
}
41+
}
42+
43+
public static Set<String> packagePrefixesOf(Class<?>... classes) {
44+
Set<String> packagePrefixes = new HashSet<String>();
45+
for (Class<?> clazz : classes) {
46+
packagePrefixes.add( packageNameOf(clazz) + '.');
47+
}
48+
return packagePrefixes;
49+
}
50+
51+
/**
52+
* Check if <code>maybeParent</code> is a parent (probably inderect) of the <code>classLoader</code>
53+
* @param classLoader The classloader whose parents are checked, may not be null
54+
* @param maybeParent Possible parent, may be null for boot class loader
55+
* @return
56+
*/
57+
public static boolean isClassLoaderParent(ClassLoader classLoader, ClassLoader maybeParent) {
58+
ClassLoader cl = classLoader;
59+
do {
60+
cl = cl.getParent();
61+
if (maybeParent == cl) {
62+
// Check includes null == null for bootstrap classloader
63+
return true;
64+
}
65+
} while (cl != null);
66+
return false;
67+
}
68+
69+
}

net.tascalate.javaflow.tools.instrument/src/main/java/org/apache/commons/javaflow/instrumentation/common/AbstractInstrumentationAgent.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@
1515
*/
1616
package org.apache.commons.javaflow.instrumentation.common;
1717

18+
import static org.apache.commons.javaflow.spi.InstrumentationUtils.isClassLoaderParent;
19+
import static org.apache.commons.javaflow.spi.InstrumentationUtils.packageNameOf;
20+
import static org.apache.commons.javaflow.spi.InstrumentationUtils.packagePrefixesOf;
21+
1822
import java.lang.instrument.ClassFileTransformer;
1923
import java.lang.instrument.Instrumentation;
2024
import java.util.Collection;
2125
import java.util.Collections;
2226
import java.util.HashSet;
2327
import java.util.Set;
2428

29+
import org.apache.commons.javaflow.spi.ClasspathResourceLoader;
2530
import org.slf4j.Logger;
2631
import org.slf4j.LoggerFactory;
2732

28-
import org.apache.commons.javaflow.spi.ClasspathResourceLoader;
2933

3034
public abstract class AbstractInstrumentationAgent {
3135

@@ -45,7 +49,7 @@ protected void attach(String args, Instrumentation instrumentation) throws Excep
4549

4650
// Collect classes before ever adding transformer!
4751
Set<String> ownPackages = new HashSet<String>(FIXED_OWN_PACKAGES);
48-
ownPackages.add(ClasspathResourceLoader.packageNameOfClass(getClass()) + '.');
52+
ownPackages.add(packageNameOf(getClass()) + '.');
4953

5054
ClassFileTransformer transformer = createTransformer();
5155
instrumentation.addTransformer(transformer);
@@ -67,7 +71,7 @@ protected void retransformClasses(Instrumentation instrumentation, Set<String> o
6771
for (Class<?> clazz : instrumentation.getAllLoadedClasses()) {
6872
String className = clazz.getName();
6973
if (instrumentation.isModifiableClass(clazz)) {
70-
if (ClasspathResourceLoader.isClassLoaderParent(systemClassLoader, clazz.getClassLoader())) {
74+
if (isClassLoaderParent(systemClassLoader, clazz.getClassLoader())) {
7175
if (log.isTraceEnabled()) {
7276
log.trace("Skip re-transforming boot or extension/platform class: " + className);
7377
}
@@ -108,18 +112,11 @@ protected void retransformClasses(Instrumentation instrumentation, Set<String> o
108112

109113
protected abstract ClassFileTransformer createTransformer();
110114

111-
private static final Collection<String> FIXED_OWN_PACKAGES;
112-
static {
113-
Class<?>[] sampleClasses = {
114-
Logger.class,
115-
ClasspathResourceLoader.class,
116-
AbstractInstrumentationAgent.class
117-
};
118-
119-
Set<String> fixedOwnPackages = new HashSet<String>();
120-
for (Class<?> sampleClass : sampleClasses) {
121-
fixedOwnPackages.add(ClasspathResourceLoader.packageNameOfClass(sampleClass) + '.');
122-
}
123-
FIXED_OWN_PACKAGES = Collections.unmodifiableSet(fixedOwnPackages);
124-
}
115+
private static final Collection<String> FIXED_OWN_PACKAGES = Collections.unmodifiableSet(
116+
packagePrefixesOf(
117+
Logger.class,
118+
ClasspathResourceLoader.class,
119+
AbstractInstrumentationAgent.class
120+
)
121+
);
125122
}

net.tascalate.javaflow.tools.instrument/src/main/java/org/apache/commons/javaflow/instrumentation/common/ConfigurableClassFileTransformer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
24+
2425
import org.apache.commons.javaflow.spi.Cache;
2526
import org.apache.commons.javaflow.spi.ClasspathResourceLoader;
27+
import org.apache.commons.javaflow.spi.InstrumentationUtils;
2628
import org.apache.commons.javaflow.spi.MorphingResourceLoader;
2729
import org.apache.commons.javaflow.spi.ResourceTransformationFactory;
2830
import org.apache.commons.javaflow.spi.ResourceTransformer;
@@ -104,7 +106,7 @@ protected ClassLoader getSafeClassLoader(ClassLoader classLoader) {
104106
}
105107

106108
private boolean isSystemClassLoaderParent(ClassLoader maybeParent) {
107-
return ClasspathResourceLoader.isClassLoaderParent(systemClassLoader, maybeParent);
109+
return InstrumentationUtils.isClassLoaderParent(systemClassLoader, maybeParent);
108110
}
109111

110112
private String resolveClassName(String className,

net.tascalate.javaflow.tools.runtime/src/main/java/org/apache/commons/javaflow/tools/runtime/ApplicationWeaver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.HashSet;
3232
import java.util.Set;
3333

34-
import org.apache.commons.javaflow.spi.ClasspathResourceLoader;
34+
import org.apache.commons.javaflow.spi.InstrumentationUtils;
3535
import org.apache.commons.javaflow.spi.ResourceTransformationFactory;
3636

3737
public final class ApplicationWeaver {
@@ -161,7 +161,7 @@ private static boolean _trampoline(ResourceTransformationFactory factory,
161161
Set<String> filteredPackageRoots = new HashSet<String>(continuablePackageRoots);
162162
for (String s : filteredPackageRoots) {
163163
if ("*".equals(s)) {
164-
s = ClasspathResourceLoader.packageNameOfClass(originalClass);
164+
s = InstrumentationUtils.packageNameOf(originalClass);
165165
}
166166
builder.addLoaderPackageRoot(s);
167167
}

net.tascalate.javaflow.tools.runtime/src/main/java/org/apache/commons/javaflow/tools/runtime/ContinuableClassLoader.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@
3535
import java.util.ArrayList;
3636
import java.util.Collection;
3737
import java.util.Collections;
38-
import java.util.HashSet;
3938
import java.util.List;
40-
import java.util.Set;
4139

4240
import org.slf4j.Logger;
4341
import org.slf4j.LoggerFactory;
4442

4543
import org.apache.commons.javaflow.spi.ClasspathResourceLoader;
4644
import org.apache.commons.javaflow.spi.FastByteArrayOutputStream;
45+
import org.apache.commons.javaflow.spi.InstrumentationUtils;
4746
import org.apache.commons.javaflow.spi.MorphingResourceLoader;
4847
import org.apache.commons.javaflow.spi.ResourceTransformationFactory;
4948
import org.apache.commons.javaflow.spi.ResourceTransformer;
@@ -675,18 +674,11 @@ private static boolean findInPackages(String resourceName, Collection<String> su
675674
return false;
676675
}
677676

678-
private static Collection<String> OWN_PACKAGES;
679-
static {
680-
Class<?>[] ownClasses = {
677+
private static Collection<String> OWN_PACKAGES = Collections.unmodifiableSet(
678+
InstrumentationUtils.packagePrefixesOf(
681679
ContinuableClassLoader.class,
682680
ResourceTransformer.class,
683681
Logger.class
684-
};
685-
686-
Set<String> ownPackages = new HashSet<String>();
687-
for (Class<?> cls : ownClasses) {
688-
ownPackages.add( ClasspathResourceLoader.packageNameOfClass(cls) + '.');
689-
}
690-
OWN_PACKAGES = Collections.unmodifiableSet(ownPackages);
691-
}
682+
)
683+
);
692684
}

0 commit comments

Comments
 (0)