Skip to content

Fix child/parent class enhancement issue during retransform #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ static class SWTypeDescriptionWrapper extends TypeDescription.AbstractBase imple

private TypeDescription delegate;

private Generic superClass;

public SWTypeDescriptionWrapper(TypeDescription delegate, String nameTrait, ClassLoader classLoader, String typeName) {
this.delegate = delegate;
this.nameTrait = nameTrait;
Expand Down Expand Up @@ -325,7 +327,17 @@ public AnnotationList getDeclaredAnnotations() {

@Override
public Generic getSuperClass() {
return delegate.getSuperClass();
if (this.superClass == null) {
Generic delegateSuperClass = delegate.getSuperClass();
if (delegateSuperClass == null) {
return null;
}
this.superClass = new ForLoadedSuperClassWrapper(
new SWTypeDescriptionWrapper(delegateSuperClass.asErasure(), this.nameTrait, null, delegateSuperClass.asErasure().getName()),
delegateSuperClass
);
}
return this.superClass;
}

@Override
Expand Down Expand Up @@ -354,6 +366,32 @@ public int getModifiers() {
}
}

static class ForLoadedSuperClassWrapper extends TypeDescription.Generic.LazyProjection.ForLoadedSuperClass {
private final TypeDescription delegation;
private final TypeDescription.Generic superGeneric;

protected ForLoadedSuperClassWrapper(TypeDescription delegation, TypeDescription.Generic superGeneric) {
super(null); // HACK: a trick here since type is not used anywhere in the wrapper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has the wrapper overrided all methods of ForLoadedSuperClass? I want to guarantee that the null would not be used unexpectedly.

If yes, could you add a UT to check that? This could prevent upgrade issue if byte-buddy core adds a method in the future.

this.delegation = delegation;
this.superGeneric = superGeneric;
}

@Override
protected TypeDescription.Generic resolve() {
return this.delegation.asGenericType();
}

@Override
public TypeDescription asErasure() {
return this.delegation;
}

@Override
public AnnotationList getDeclaredAnnotations() {
return superGeneric.getDeclaredAnnotations();
}
}

static class TypeCache {
private String typeName;
private Set<Integer> methodCodes;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.skywalking.apm.agent.bytebuddy.biz;

public class ChildBar extends ParentBar {
public String sayHelloChild() {
return "Joe";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.skywalking.apm.agent.bytebuddy.biz;

public class ParentBar {
public String sayHelloParent() {
return "Joe";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.skywalking.apm.agent.bytebuddy.SWAuxiliaryTypeNamingStrategy;
import org.apache.skywalking.apm.agent.bytebuddy.SWClassFileLocator;
import org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo;
import org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.junit.Assert;
import org.junit.BeforeClass;
Expand All @@ -63,6 +64,8 @@ public class AbstractInterceptTest {
public static final String BIZ_FOO_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo";
public static final String PROJECT_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ProjectService";
public static final String DOC_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.DocService";
public static final String PARENT_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar";
public static final String CHILD_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar";
public static final String SAY_HELLO_METHOD = "sayHello";
public static final int BASE_INT_VALUE = 100;
public static final String CONSTRUCTOR_INTERCEPTOR_CLASS = "constructorInterceptorClass";
Expand All @@ -85,6 +88,20 @@ protected void failed(Throwable e, Description description) {
}
};

protected static void callBar(int round) {
Log.info("-------------");
Log.info("callChildBar: " + round);
// load target class
String strResultChild = new ChildBar().sayHelloChild();
Log.info("result: " + strResultChild);

String strResultParent = new ChildBar().sayHelloParent();
Log.info("result: " + strResultParent);

Assert.assertEquals("String value is unexpected", "John", strResultChild);
Assert.assertEquals("String value is unexpected", "John", strResultParent);
}

protected static void callBizFoo(int round) {
Log.info("-------------");
Log.info("callBizFoo: " + round);
Expand Down Expand Up @@ -115,7 +132,7 @@ protected static void checkConstructorInterceptor(String className, int round) {

protected static void checkInterface(Class testClass, Class interfaceCls) {
Assert.assertTrue("Check interface failure, the test class: " + testClass + " does not implement the expected interface: " + interfaceCls,
EnhancedInstance.class.isAssignableFrom(BizFoo.class));
EnhancedInstance.class.isAssignableFrom(testClass));
}

protected static void checkErrors() {
Expand All @@ -139,8 +156,7 @@ protected void installMethodInterceptorWithMethodDelegation(String className, St
return builder
.method(ElementMatchers.nameContainsIgnoreCase(methodName))
.intercept(MethodDelegation.withDefaultConfiguration()
.to(new InstMethodsInter(interceptorClassName, classLoader), fieldName))
;
.to(new InstMethodsInter(interceptorClassName, classLoader), fieldName));
}
)
.with(getListener(interceptorClassName))
Expand Down Expand Up @@ -223,7 +239,7 @@ protected void installTraceClassTransformer(String msg) {
ClassFileTransformer classFileTransformer = new ClassFileTransformer() {
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
if (className.endsWith("BizFoo") || className.endsWith("ProjectService") || className.endsWith("DocService")) {
if (className.endsWith("BizFoo") || className.endsWith("ProjectService") || className.endsWith("DocService") || className.endsWith("ChildBar") || className.endsWith("ParentBar")) {
Log.error(msg + className);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ClassReader cr = new ClassReader(classfileBuffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.skywalking.apm.agent.bytebuddy.cases;

import net.bytebuddy.agent.ByteBuddyAgent;
import org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar;
import org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.junit.Test;

import java.lang.instrument.Instrumentation;

public class ReTransform4Test extends AbstractReTransformTest {

@Test
public void testEnhancedInstanceForMultiShotRetransform() throws Exception {
Instrumentation instrumentation = ByteBuddyAgent.install();

// install transformer
installMethodInterceptor(PARENT_BAR_CLASS_NAME, "sayHelloParent", 1);
installMethodInterceptor(CHILD_BAR_CLASS_NAME, "sayHelloChild", 1);
// implement EnhancedInstance
installInterface(PARENT_BAR_CLASS_NAME);
installInterface(CHILD_BAR_CLASS_NAME);

callBar(1);

// check interceptors
checkInterface(ParentBar.class, EnhancedInstance.class);
checkInterface(ChildBar.class, EnhancedInstance.class);
checkErrors();

installTraceClassTransformer("Trace class: ");

// do retransform
reTransform(instrumentation, ChildBar.class);

// check interceptors
checkInterface(ParentBar.class, EnhancedInstance.class);
checkInterface(ChildBar.class, EnhancedInstance.class);
checkErrors();
}

}

Loading