yu199195 commented on a change in pull request #8307: URL: https://github.com/apache/shardingsphere/pull/8307#discussion_r528681470
########## File path: shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/MethodAroundInterceptor.java ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.core.plugin.advice; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import net.bytebuddy.implementation.bind.annotation.AllArguments; +import net.bytebuddy.implementation.bind.annotation.Origin; +import net.bytebuddy.implementation.bind.annotation.RuntimeType; +import net.bytebuddy.implementation.bind.annotation.This; +import net.bytebuddy.implementation.bind.annotation.SuperCall; + +import java.lang.reflect.Method; +import java.util.concurrent.Callable; + +/** + * A proxy class for ByteBuddy to intercept methods of target and weave pre- and post-method around the target method. + */ +@Slf4j +public class MethodAroundInterceptor { + private final MethodAroundAdvice advice; + + public MethodAroundInterceptor(final MethodAroundAdvice advice) { + this.advice = advice; + } + + /** + * Only intercept instance method. + * @param target the target object + * @param method the intercepted method + * @param args the all arguments of method Review comment: keep a space ########## File path: shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/LoggingListener.java ########## @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.core; + +import lombok.extern.slf4j.Slf4j; +import net.bytebuddy.agent.builder.AgentBuilder; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.dynamic.DynamicType; +import net.bytebuddy.utility.JavaModule; + +/** + * A listener to log what is informed about events that occur during an instrumentation process. Review comment: remove A ########## File path: shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/MethodAroundInterceptor.java ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.core.plugin.advice; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import net.bytebuddy.implementation.bind.annotation.AllArguments; +import net.bytebuddy.implementation.bind.annotation.Origin; +import net.bytebuddy.implementation.bind.annotation.RuntimeType; +import net.bytebuddy.implementation.bind.annotation.This; +import net.bytebuddy.implementation.bind.annotation.SuperCall; + +import java.lang.reflect.Method; +import java.util.concurrent.Callable; + +/** + * A proxy class for ByteBuddy to intercept methods of target and weave pre- and post-method around the target method. Review comment: remove A ########## File path: shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/MethodAroundAdvice.java ########## @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.core.plugin.advice; + +import java.lang.reflect.Method; + +/** + * Weaving the advice around the target method. + */ +public interface MethodAroundAdvice { + + /** + * Intercept the target method and weave the method before origin method. It will invoke before the origin calling. + * @param target the target object + * @param method the target method + * @param args the all method arguments + * @param result A wrapped class of result to detect whether or not to execute the origin method. + */ + void beforeMethod(TargetObject target, Method method, Object[] args, MethodInvocationResult result); + + /** + * Intercept the target method and weave the method after origin method. It will invoke after the origin calling. + * @param target the target object + * @param method the target method + * @param args the all method arguments + * @param result A wrapped class of result to detect whether or not to execute the origin method. + */ + void afterMethod(TargetObject target, Method method, Object[] args, MethodInvocationResult result); + + /** + * Weaving the method after origin method throwing. + * @param target the target object + * @param method the target method Review comment: keep a space ########## File path: shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereTransformer.java ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.core; + +import lombok.extern.slf4j.Slf4j; +import net.bytebuddy.agent.builder.AgentBuilder; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.dynamic.DynamicType; +import net.bytebuddy.implementation.FieldAccessor; +import net.bytebuddy.implementation.MethodDelegation; +import net.bytebuddy.implementation.SuperMethodCall; +import net.bytebuddy.jar.asm.Opcodes; +import net.bytebuddy.utility.JavaModule; +import org.apache.shardingsphere.agent.core.plugin.PluginAdviceDefine; +import org.apache.shardingsphere.agent.core.plugin.PluginLoader; +import org.apache.shardingsphere.agent.core.plugin.advice.ConstructorMethodInterceptor; +import org.apache.shardingsphere.agent.core.plugin.advice.MethodAroundInterceptor; +import org.apache.shardingsphere.agent.core.plugin.advice.StaticMethodAroundInterceptor; +import org.apache.shardingsphere.agent.core.plugin.advice.TargetObject; +import org.apache.shardingsphere.agent.core.plugin.point.ClassStaticMethodPoint; +import org.apache.shardingsphere.agent.core.plugin.point.ConstructorPoint; +import org.apache.shardingsphere.agent.core.plugin.point.InstanceMethodPoint; + +import java.util.Map; + +@Slf4j Review comment: add doc ########## File path: shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/PluginLoader.java ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.core.plugin; + +import lombok.SneakyThrows; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; + +/** + * Plugins loader. + * TODO not-implemented yet + */ +public class PluginLoader extends ClassLoader { + + private final ConcurrentHashMap<String, Object> objectPool = new ConcurrentHashMap<>(); + + private final ReentrantLock lock = new ReentrantLock(); + + /** + * to find all intercepting target classes then to build TypeMatcher. + * + * @return TypeMatcher + */ + public ElementMatcher<? super TypeDescription> typeMatcher() { + return null; + } + + /** + * to detect the type whether or not exists. + * + * @param typeDescription TypeDescription + * @return contains when it is true. + */ + public boolean containsType(final TypeDescription typeDescription) { + return false; + } + + /** + * Load the definition configuration by TypeDescription. + * + * @param typeDescription TypeDescription + * @return the plugin definition configurations. + */ + public PluginAdviceDefine loadPluginAdviceDefine(final TypeDescription typeDescription) { + return null; + } + + /** + * get or create instance of the advice class. Create new one and caching when it is not exist. + * + * @param classNameOfAdvice the class name of advice + * @param <T> the advice type. Review comment: keep a space ########## File path: shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/ConstructorAdvice.java ########## @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.core.plugin.advice; + +/** + * Weaving the advice around the constructor of target class. + */ +public interface ConstructorAdvice { + + /** + * Intercept the target's constructor. This method is weaved after the constructor execution. + * @param target Intercepted target object Review comment: keep a space ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
