|
| 1 | +/* |
| 2 | + * Copyright 2017 original authors |
| 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.particleframework.aop.writer; |
| 17 | + |
| 18 | +import org.objectweb.asm.ClassWriter; |
| 19 | +import org.objectweb.asm.MethodVisitor; |
| 20 | +import org.objectweb.asm.Opcodes; |
| 21 | +import org.objectweb.asm.Type; |
| 22 | +import org.objectweb.asm.commons.GeneratorAdapter; |
| 23 | +import org.objectweb.asm.commons.Method; |
| 24 | +import org.particleframework.aop.Intercepted; |
| 25 | +import org.particleframework.aop.Interceptor; |
| 26 | +import org.particleframework.inject.writer.AbstractClassFileWriter; |
| 27 | +import org.particleframework.inject.writer.BeanDefinitionWriter; |
| 28 | +import org.particleframework.inject.writer.ClassGenerationException; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.OutputStream; |
| 32 | +import java.util.*; |
| 33 | + |
| 34 | +import static org.particleframework.inject.writer.BeanDefinitionWriter.DEFAULT_MAX_STACK; |
| 35 | + |
| 36 | +/** |
| 37 | + * A class that generates AOP classes at compile time |
| 38 | + * |
| 39 | + * @author Graeme Rocher |
| 40 | + * @since 1.0 |
| 41 | + */ |
| 42 | +public class AopProxyWriter extends AbstractClassFileWriter { |
| 43 | + private final String packageName; |
| 44 | + private final String targetClassShortName; |
| 45 | + private final ClassWriter classWriter; |
| 46 | + private final String targetClassFullName; |
| 47 | + private final String proxyFullName; |
| 48 | + private final BeanDefinitionWriter proxyBeanDefinitionWriter; |
| 49 | + private final String proxyInternalName; |
| 50 | + private final Object[] interceptorTypes; |
| 51 | + |
| 52 | + private MethodVisitor constructorWriter; |
| 53 | + |
| 54 | + public AopProxyWriter(String packageName, String targetClassShortName, Object... interceptorTypes) { |
| 55 | + this.packageName = packageName; |
| 56 | + this.targetClassShortName = targetClassShortName; |
| 57 | + this.targetClassFullName = packageName + '.' + targetClassShortName; |
| 58 | + this.classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); |
| 59 | + String proxyShortName = targetClassShortName + "$Intercepted"; |
| 60 | + this.proxyFullName = packageName + '.' + proxyShortName; |
| 61 | + this.proxyInternalName = getInternalName(this.proxyFullName); |
| 62 | + this.interceptorTypes = interceptorTypes; |
| 63 | + // TODO: propagate scopes |
| 64 | + this.proxyBeanDefinitionWriter = new BeanDefinitionWriter(packageName, proxyShortName, null, true); |
| 65 | + startClass(classWriter, proxyFullName, getTypeReference(targetClassFullName)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return The bean definition writer for this proxy |
| 70 | + */ |
| 71 | + public BeanDefinitionWriter getProxyBeanDefinitionWriter() { |
| 72 | + return proxyBeanDefinitionWriter; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Visits a no arguments constructor. Either this method or {@link #visitConstructor(Map, Map, Map)} should be called at least once |
| 77 | + */ |
| 78 | + public void visitConstructor() { |
| 79 | + visitConstructor(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Visits a constructor with arguments. Either this method or {@link #visitConstructor()} should be called at least once |
| 84 | + * |
| 85 | + * @param argumentTypes The argument names and types. Should be an ordered map should as {@link LinkedHashMap} |
| 86 | + * @param qualifierTypes The argument names and qualifier types. Should be an ordered map should as {@link LinkedHashMap} |
| 87 | + * @param genericTypes The argument names and generic types. Should be an ordered map should as {@link LinkedHashMap} |
| 88 | + */ |
| 89 | + public void visitConstructor(Map<String, Object> argumentTypes, Map<String, Object> qualifierTypes, Map<String, List<Object>> genericTypes) { |
| 90 | + Map<String,Object> newArgumentTypes = new LinkedHashMap<>(argumentTypes); |
| 91 | + newArgumentTypes.put("interceptors", Interceptor[].class); |
| 92 | + |
| 93 | + String constructorDescriptor = getConstructorDescriptor(newArgumentTypes.values()); |
| 94 | + this.constructorWriter = classWriter.visitMethod(ACC_PUBLIC, "<init>", constructorDescriptor, null, null); |
| 95 | + GeneratorAdapter adapter = new GeneratorAdapter(constructorWriter, Opcodes.ACC_PUBLIC, "<init>", constructorDescriptor); |
| 96 | + adapter.loadThis(); |
| 97 | + Collection<Object> existingArguments = argumentTypes.values(); |
| 98 | + for (int i = 0; i < existingArguments.size(); i++) { |
| 99 | + adapter.loadArg(i); |
| 100 | + } |
| 101 | + String superConstructorDescriptor = getConstructorDescriptor(existingArguments); |
| 102 | + adapter.invokeConstructor(getTypeReference(targetClassFullName), new Method("<init>", superConstructorDescriptor)); |
| 103 | + proxyBeanDefinitionWriter.visitBeanDefinitionConstructor(newArgumentTypes, qualifierTypes, genericTypes); |
| 104 | + } |
| 105 | + |
| 106 | + public void visitProxyEnd() { |
| 107 | + classWriter.visit(V1_8, ACC_PUBLIC, |
| 108 | + proxyInternalName, |
| 109 | + null, |
| 110 | + getTypeReference(targetClassFullName).getInternalName(), |
| 111 | + new String[]{Type.getInternalName(Intercepted.class)}); |
| 112 | + |
| 113 | + if(constructorWriter == null) { |
| 114 | + throw new IllegalStateException("The method visitConstructor(..) should be called at least once"); |
| 115 | + } |
| 116 | + |
| 117 | + constructorWriter.visitInsn(RETURN); |
| 118 | + constructorWriter.visitMaxs(DEFAULT_MAX_STACK, 1); |
| 119 | + |
| 120 | + this.constructorWriter.visitEnd(); |
| 121 | + proxyBeanDefinitionWriter.visitBeanDefinitionEnd(); |
| 122 | + classWriter.visitEnd(); |
| 123 | + } |
| 124 | + /** |
| 125 | + * Write the class to the target directory |
| 126 | + * |
| 127 | + * @param targetDir The target directory |
| 128 | + */ |
| 129 | + public void writeTo(File targetDir) { |
| 130 | + try { |
| 131 | + |
| 132 | + writeClassToDisk(targetDir, classWriter, proxyInternalName); |
| 133 | + |
| 134 | + } catch (Throwable e) { |
| 135 | + throw new ClassGenerationException("Error generating proxy definition class for bean definition ["+targetClassFullName+"]: " + e.getMessage(), e); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Write the class to the output stream, such a JavaFileObject created from a java annotation processor Filer object |
| 141 | + * |
| 142 | + * @param outputStream the output stream pointing to the target class file |
| 143 | + */ |
| 144 | + public void writeTo(OutputStream outputStream) { |
| 145 | + try { |
| 146 | + writeClassToDisk(outputStream, classWriter); |
| 147 | + |
| 148 | + } catch (Throwable e) { |
| 149 | + throw new ClassGenerationException("Error generating bean definition class for bean definition ["+targetClassFullName+"]: " + e.getMessage(), e); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments