/*
 * Copyright 2014 NAVER Corp.
 *
 * 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.navercorp.pinpoint.profiler.transformer;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

import com.navercorp.pinpoint.bootstrap.instrument.RequestHandle;
import com.navercorp.pinpoint.profiler.ProfilerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformRequestListener;
import com.navercorp.pinpoint.bootstrap.instrument.DynamicTransformTrigger;
import java.util.Objects;

/**
 * @author emeroad
 */
public class DynamicTransformService implements DynamicTransformTrigger {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final Instrumentation instrumentation;

    private DynamicTransformRequestListener dynamicTransformRequestListener;

    public DynamicTransformService(Instrumentation instrumentation, DynamicTransformRequestListener listener) {
        this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation");
        this.dynamicTransformRequestListener = Objects.requireNonNull(listener, "listener");
    }

    @Override
    public void retransform(Class<?> target, ClassFileTransformer transformer) {
        if (this.logger.isDebugEnabled()) {
            logger.debug("retransform request class:{}", target.getName());
        }
        assertClass(target);

        final RequestHandle requestHandle = this.dynamicTransformRequestListener.onRetransformRequest(target, transformer);
        boolean success = false;
        try {
            triggerRetransform(target);
            success = true;
        } finally {
            if (!success) {
                requestHandle.cancel();
            }
        }
    }
    
    @Override
    public void addClassFileTransformer(ClassLoader classLoader, String targetClassName, ClassFileTransformer transformer) {
        if (this.logger.isDebugEnabled()) {
            logger.debug("Add dynamic transform. classLoader={}, class={}", classLoader, targetClassName);
        }
        
        this.dynamicTransformRequestListener.onTransformRequest(classLoader, targetClassName, transformer);
    }

    private void assertClass(Class<?> target) {
        if (!instrumentation.isModifiableClass(target)) {
            throw new ProfilerException("Target class " + target + " is not modifiable");
        }
    }

    private void triggerRetransform(Class<?> target) {
        try {
            instrumentation.retransformClasses(target);
        } catch (UnmodifiableClassException e) {
            throw new ProfilerException(e);
        }
    }

    public void setTransformRequestEventListener(DynamicTransformRequestListener retransformEventListener) {
        if (retransformEventListener == null) {
            throw new NullPointerException("dynamicTransformRequestListener");
        }
        this.dynamicTransformRequestListener = retransformEventListener;
    }

}

@@ -861,16 +861,122 @@
 Handle;%0A
+import com.navercorp.pinpoint.common.util.JvmUtils;%0Aimport com.navercorp.pinpoint.common.util.JvmVersion;%0A
 import c
@@ -1021,24 +1021,24 @@
 rException;%0A
-
 import org.s
@@ -1239,24 +1239,25 @@
 ormTrigger;%0A
+%0A
 import java.
@@ -1271,16 +1271,153 @@
 jects;%0A%0A
+import static com.navercorp.pinpoint.common.util.JvmVersion.JAVA_8;%0Aimport static com.navercorp.pinpoint.common.util.JvmVersion.JAVA_9;%0A%0A
 /**%0A * @
@@ -2632,21 +2632,17 @@
 %7D%0A    %7D%0A
-    
 %0A
+
     @Ove
@@ -2937,17 +2937,9 @@
   %7D%0A
-        
 %0A
+
     
@@ -3151,24 +3151,24 @@
 (target)) %7B%0A
-
             
@@ -3247,32 +3247,584 @@
 le%22);%0A        %7D%0A
+        final JvmVersion version = JvmUtils.getVersion();%0A        if (JAVA_8.compareTo(version) == 0) %7B%0A            // If the version is java 8%0A            // Java 8 bug - NoClassDefFound error in transforming lambdas(https://bugs.openjdk.java.net/browse/JDK-8145964)%0A            final String className = target.getName();%0A            if (className != null && className.contains(%22$$Lambda$%22)) %7B%0A                throw new ProfilerException(%22Target class %22 + target + %22 is lambda class, Causes NoClassDefFound error in java 8.%22);%0A            %7D%0A        %7D%0A
     %7D%0A%0A    priva

Fix assert class of retransform