Skip to content

Commit 6b97856

Browse files
committed
Refactor CDI proxy processors (separate classes / packages); Support Weld 3.1.0 and higher
1 parent ce1d90a commit 6b97856

File tree

14 files changed

+325
-76
lines changed

14 files changed

+325
-76
lines changed

net.tascalate.javaflow.spi/src/main/resources/META-INF/net.tascalate.javaflow.veto.cmf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ implements-interface:name=org/jboss/weld/bean/proxy/ProxyObject
44
implements-interface:name=org/jboss/as/ee/component/serialization/WriteReplaceInterface
55
class:name-pattern=.*\$\$EnhancerByCGLIB\$\$.*
66
# possible values are:
7+
# implements-interface:name
78
# implements-interface:name-part
89
# implements-interface:name-pattern
910
# extends-class:name
1011
# extends-class:name-part
1112
# extends-class:name-pattern
1213
# class:name
1314
# class:name-part
14-
# class:name-pattern
15+
# class:name-pattern
16+
# Classes should be named in internal format, i.e. com/company/myapp/MyClass

net.tascalate.javaflow.tools.cdi-javaagent/src/main/java/org/apache/commons/javaflow/instrumentation/cdi/CdiProxyClassAdapter.java

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424
import net.tascalate.asmx.ClassVisitor;
2525
import net.tascalate.asmx.FieldVisitor;
2626
import net.tascalate.asmx.MethodVisitor;
27-
import net.tascalate.asmx.Type;
28-
import net.tascalate.asmx.Opcodes;
2927

28+
import org.apache.commons.javaflow.instrumentation.cdi.owb.OwbProxyClassProcessor;
29+
import org.apache.commons.javaflow.instrumentation.cdi.spring.SpringProxyClassProcessor;
30+
import org.apache.commons.javaflow.instrumentation.cdi.weld.WeldProxyClassProcessor;
3031
import org.apache.commons.javaflow.spi.ContinuableClassInfo;
3132
import org.apache.commons.javaflow.spi.ContinuableClassInfoResolver;
3233
import org.apache.commons.javaflow.spi.StopException;
3334

34-
class CdiProxyClassAdapter extends ClassVisitor {
35+
class CdiProxyClassAdapter extends ExtendedClassVisitor {
3536

36-
static enum CdiEnvironmentType {
37+
static enum ContainerType {
3738
WELD("org/jboss/weld/bean/proxy/ProxyObject") {
3839

3940
@Override
@@ -44,68 +45,58 @@ boolean accept(String className, String interfaceName) {
4445
}
4546

4647
@Override
47-
MethodVisitor createAdviceAdapter(CdiProxyClassAdapter ca, MethodVisitor mv, int acc, String name, String desc) {
48-
return new AroundWeldProxyInvocationAdvice(ca.api, mv, acc, ca.className, name, desc);
49-
}
48+
ProxyClassProcessor createProcessor(int api, String className, ContinuableClassInfo classInfo) {
49+
return new WeldProxyClassProcessor(api, className, classInfo);
50+
}
51+
5052
},
5153
SPRING("org/springframework/aop/framework/Advised") {
5254

5355
@Override
54-
MethodVisitor createAdviceAdapter(CdiProxyClassAdapter ca, MethodVisitor mv, int acc, String name, String desc) {
55-
return new AroundSpringProxyInvocationAdvice(ca.api, mv, acc, ca.className, name, desc);
56+
ProxyClassProcessor createProcessor(int api, String className, ContinuableClassInfo classInfo) {
57+
return new SpringProxyClassProcessor(api, className, classInfo);
5658
}
59+
5760
},
5861
OWB(
5962
"org/apache/webbeans/proxy/OwbInterceptorProxy",
6063
"org/apache/webbeans/proxy/OwbNormalScopeProxy"
6164
) {
6265

6366
@Override
64-
MethodVisitor createAdviceAdapter(CdiProxyClassAdapter ca, MethodVisitor mv, int acc, String name, String desc) {
65-
if (null != ca.owbProxiedInstanceType) {
66-
return new AroundOwbInterceptorProxyAdvice(ca.api, mv, acc, ca.className, name, desc, ca.owbProxiedInstanceType);
67-
} else if (null != ca.owbProxiedInstanceProviderType) {
68-
return new AroundOwbScopeProxyAdvice(ca.api, mv, acc, ca.className, name, desc, ca.owbProxiedInstanceProviderType);
69-
} else {
70-
return mv;
71-
}
72-
}
67+
ProxyClassProcessor createProcessor(int api, String className, ContinuableClassInfo classInfo) {
68+
return new OwbProxyClassProcessor(api, className, classInfo);
69+
}
70+
7371
}
7472
;
7573

7674
private Set<String> markerInterfaces;
7775

78-
private CdiEnvironmentType(String... markerInterfaces) {
76+
private ContainerType(String... markerInterfaces) {
7977
this.markerInterfaces =
8078
Collections.unmodifiableSet( new HashSet<String>(Arrays.asList(markerInterfaces)) );
8179
}
8280

83-
abstract MethodVisitor createAdviceAdapter(CdiProxyClassAdapter ca, MethodVisitor mv, int acc, String name, String desc);
8481
boolean accept(String className, String interfaceName) {
8582
return markerInterfaces.contains(interfaceName);
8683
}
84+
85+
abstract ProxyClassProcessor createProcessor(int api, String className, ContinuableClassInfo classInfo);
8786
}
8887

89-
String className;
90-
Type owbProxiedInstanceType;
91-
Type owbProxiedInstanceProviderType;
92-
93-
private CdiEnvironmentType cdiEnvironmentType;
94-
private ContinuableClassInfo classInfo;
95-
9688
private final ContinuableClassInfoResolver cciResolver;
89+
private ProxyClassProcessor processor;
9790

9891
CdiProxyClassAdapter(ClassVisitor delegate, ContinuableClassInfoResolver cciResolver) {
99-
super(Opcodes.ASM7, delegate);
92+
super(delegate);
10093
this.cciResolver = cciResolver;
10194
}
10295

103-
10496
@Override
10597
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
106-
className = name;
107-
CdiEnvironmentType selectedType = null;
108-
CdiEnvironmentType[] cdiEnvironmentTypes = CdiEnvironmentType.values();
98+
ContainerType selectedType = null;
99+
ContainerType[] cdiEnvironmentTypes = ContainerType.values();
109100
outerLoop:
110101
for (final String interfaze : interfaces) {
111102
for (int i = cdiEnvironmentTypes.length - 1; i >= 0; i--) {
@@ -123,8 +114,7 @@ public void visit(int version, int access, String name, String signature, String
123114
throw StopException.INSTANCE;
124115
}
125116

126-
cdiEnvironmentType = selectedType;
127-
117+
ContinuableClassInfo classInfo;
128118
try {
129119
classInfo = cciResolver.resolve(superName);
130120
if (null == classInfo) {
@@ -133,34 +123,23 @@ public void visit(int version, int access, String name, String signature, String
133123
} catch (IOException e) {
134124
throw new RuntimeException(e);
135125
}
126+
127+
processor = selectedType.createProcessor(api, name, classInfo);
136128
super.visit(version, access, name, signature, superName, interfaces);
137129
}
138130

139131
@Override
140-
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
141-
if (AroundOwbInterceptorProxyAdvice.FIELD_PROXIED_INSTANCE.equals(name)) {
142-
owbProxiedInstanceType = Type.getType(desc);
143-
}
144-
if (AroundOwbScopeProxyAdvice.FIELD_INSTANCE_PROVIDER.equals(name)) {
145-
owbProxiedInstanceProviderType = Type.getType(desc);
146-
}
147-
return super.visitField(access, name, desc, signature, value);
132+
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
133+
return processor.visitField(this, access, name, descriptor, signature, value);
148134
}
149-
135+
150136
@Override
151-
public MethodVisitor visitMethod(int acc, String name, String desc, String signature, String[] exceptions) {
152-
MethodVisitor mv = cv.visitMethod(acc, name, desc, signature, exceptions);
153-
if (isContinuableMethodProxy(acc, name, desc, signature, exceptions) && null != cdiEnvironmentType) {
154-
mv = cdiEnvironmentType.createAdviceAdapter(this, mv, acc, name, desc);
155-
}
156-
return mv;
137+
public void visitEnd() {
138+
processor.visitEnd(this);
157139
}
158-
159-
private boolean isContinuableMethodProxy(int acc, String name, String desc, String signature, String[] exceptions) {
160-
int idx = name.lastIndexOf("$$super");
161-
if (idx > 0) {
162-
name = name.substring(0, idx);
163-
}
164-
return ! "<init>".equals(name) && classInfo.isContinuableMethod(acc, name, desc, signature);
140+
141+
@Override
142+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
143+
return processor.visitMethod(this, access, name, descriptor, signature, exceptions);
165144
}
166145
}

net.tascalate.javaflow.tools.cdi-javaagent/src/main/java/org/apache/commons/javaflow/instrumentation/cdi/CdiProxyClassTransformer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ protected ContinuableClassInfoResolver getCachedResolver(ClassLoader classLoader
103103
synchronized (classLoader2resolver) {
104104
ContinuableClassInfoResolver cachedResolver = classLoader2resolver.get(classLoader);
105105
if (null == cachedResolver) {
106+
log.debug("Create classInfoResolver for class loader " + classLoader);
106107
ContinuableClassInfoResolver newResolver = resourceTransformationFactory
107108
.createResolver(new ExtendedClasspathResourceLoader(classLoader));
108109
classLoader2resolver.put(classLoader, newResolver);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2013-2019 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.javaflow.instrumentation.cdi;
17+
18+
import net.tascalate.asmx.ClassVisitor;
19+
import net.tascalate.asmx.FieldVisitor;
20+
import net.tascalate.asmx.MethodVisitor;
21+
import net.tascalate.asmx.Opcodes;
22+
23+
public abstract class ExtendedClassVisitor extends ClassVisitor {
24+
25+
ExtendedClassVisitor(ClassVisitor delegate) {
26+
super(Opcodes.ASM7, delegate);
27+
}
28+
29+
public final FieldVisitor defaultVisitField(int access, String name, String descriptor, String signature, Object value) {
30+
return super.visitField(access, name, descriptor, signature, value);
31+
}
32+
33+
public final MethodVisitor defaultVisitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
34+
return super.visitMethod(access, name, descriptor, signature, exceptions);
35+
}
36+
37+
public final void defaultVisitEnd() {
38+
super.visitEnd();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2013-2019 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.javaflow.instrumentation.cdi;
17+
18+
import org.apache.commons.javaflow.spi.ContinuableClassInfo;
19+
20+
import net.tascalate.asmx.FieldVisitor;
21+
import net.tascalate.asmx.MethodVisitor;
22+
23+
abstract public class ProxyClassProcessor {
24+
protected final int api;
25+
protected final String className;
26+
protected final ContinuableClassInfo classInfo;
27+
28+
protected ProxyClassProcessor(int api, String className, ContinuableClassInfo classInfo) {
29+
this.api = api;
30+
this.className = className;
31+
this.classInfo = classInfo;
32+
}
33+
34+
protected MethodVisitor visitMethod(ExtendedClassVisitor cv, int access, String name, String descriptor, String signature, String[] exceptions) {
35+
MethodVisitor mv = cv.defaultVisitMethod(access, name, descriptor, signature, exceptions);
36+
if (isContinuableMethodProxy(access, name, descriptor, signature, exceptions)) {
37+
mv = createAdviceAdapter(mv, access, name, descriptor);
38+
}
39+
return mv;
40+
}
41+
42+
protected FieldVisitor visitField(ExtendedClassVisitor cv, int access, String name, String descriptor, String signature, Object value) {
43+
return cv.defaultVisitField(access, name, descriptor, signature, value);
44+
}
45+
46+
protected void visitEnd(ExtendedClassVisitor cv) {
47+
cv.defaultVisitEnd();
48+
}
49+
50+
abstract protected MethodVisitor createAdviceAdapter(MethodVisitor mv, int access, String name, String descriptor);
51+
52+
protected boolean isContinuableMethodProxy(int access, String name, String descriptor, String signature, String[] exceptions) {
53+
int idx = name.lastIndexOf("$$super");
54+
if (idx > 0) {
55+
name = name.substring(0, idx);
56+
}
57+
return ! "<init>".equals(name) && classInfo.isContinuableMethod(access, name, descriptor, signature);
58+
}
59+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.apache.commons.javaflow.instrumentation.cdi;
16+
package org.apache.commons.javaflow.instrumentation.cdi.common;
1717

1818
import net.tascalate.asmx.Label;
1919
import net.tascalate.asmx.MethodVisitor;
2020
import net.tascalate.asmx.Opcodes;
2121
import net.tascalate.asmx.Type;
2222
import net.tascalate.asmx.commons.AdviceAdapter;
2323

24-
abstract class AroundCdiProxyInvocationAdvice extends AdviceAdapter {
24+
abstract public class ProxiedMethodAdvice extends AdviceAdapter {
2525
final protected String className;
2626

2727
private Label startFinally;
2828

29-
protected AroundCdiProxyInvocationAdvice(int api, MethodVisitor mv, int acc, String className, String methodName, String desc) {
30-
super(api, mv, acc, methodName, desc);
29+
protected ProxiedMethodAdvice(int api, MethodVisitor mv, int access, String className, String methodName, String descriptor) {
30+
super(api, mv, access, methodName, descriptor);
3131
this.className = className;
3232
}
3333

3434
abstract protected void loadProxiedInstance();
3535

3636
@Override
3737
protected void onMethodEnter() {
38-
loadProxiedInstance();
38+
loadProxiedInstance();
3939
mv.visitMethodInsn(
4040
Opcodes.INVOKESTATIC,
4141
INTERCEPTOR_SUPPORT_TYPE.getInternalName(),
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.apache.commons.javaflow.instrumentation.cdi;
16+
package org.apache.commons.javaflow.instrumentation.cdi.owb;
1717

1818
import net.tascalate.asmx.MethodVisitor;
1919
import net.tascalate.asmx.Type;
2020

21-
class AroundOwbInterceptorProxyAdvice extends AroundCdiProxyInvocationAdvice {
21+
import org.apache.commons.javaflow.instrumentation.cdi.common.ProxiedMethodAdvice;
22+
23+
class OwbInterceptorProxyMethodAdvice extends ProxiedMethodAdvice {
2224

2325
final private Type proxiedInstanceType;
2426

25-
AroundOwbInterceptorProxyAdvice(int api, MethodVisitor mv, int acc, String className, String methodName, String desc, Type proxiedInstanceType) {
27+
OwbInterceptorProxyMethodAdvice(int api, MethodVisitor mv, int acc, String className, String methodName, String desc, Type proxiedInstanceType) {
2628
super(api, mv, acc, className, methodName, desc);
2729
this.proxiedInstanceType = proxiedInstanceType;
2830
}

0 commit comments

Comments
 (0)