Commit 8bf7e346 authored by Andy Ai's avatar Andy Ai Committed by 吴晟
Browse files

Support play 2.x (#3207)

* Support play 2.x 
parent 3d00d83f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -130,4 +130,6 @@ public class ComponentsDefine {

    public static final OfficialComponent SPRING_WEBFLUX = new OfficialComponent(67, "spring-webflux");

    public static final OfficialComponent PLAY = new OfficialComponent(68, "Play");

}
+72 −0
Original line number Diff line number Diff line
/*
 * 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.core.plugin.bytebuddy;

import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationSource;
import net.bytebuddy.matcher.CollectionItemMatcher;
import net.bytebuddy.matcher.DeclaringAnnotationMatcher;
import net.bytebuddy.matcher.ElementMatcher;

/**
 * Annotation Type match.
 * Similar with {@link net.bytebuddy.matcher.ElementMatchers#isAnnotatedWith},
 * the only different between them is this match use {@link String} to declare the type, instead of {@link Class}.
 * This can avoid the classloader risk.
 * <p>
 *
 * @author AI
 * 2019-08-15
 */
public class AnnotationTypeNameMatch<T extends AnnotationDescription> implements ElementMatcher<T> {

    /**
     * the target annotation type
     */
    private String annotationTypeName;

    /**
     * declare the match target method with the certain type.
     *
     * @param annotationTypeName target annotation type
     */
    private AnnotationTypeNameMatch(String annotationTypeName) {
        this.annotationTypeName = annotationTypeName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean matches(T target) {
        return target.getAnnotationType().asErasure().getName().equals(annotationTypeName);
    }

    /**
     * The static method to create {@link AnnotationTypeNameMatch}
     * This is a delegate method to follow byte-buddy {@link ElementMatcher}'s code style.
     *
     * @param annotationTypeName target annotation type
     * @param <T>                The type of the object that is being matched.
     * @return new {@link AnnotationTypeNameMatch} instance.
     */
    public static <T extends AnnotationSource> ElementMatcher.Junction<T> isAnnotatedWithType(String annotationTypeName) {
        final AnnotationTypeNameMatch<AnnotationDescription> matcher = new AnnotationTypeNameMatch<AnnotationDescription>(annotationTypeName);
        return new DeclaringAnnotationMatcher<T>(new CollectionItemMatcher<AnnotationDescription>(matcher));
    }
}
+68 −0
Original line number Diff line number Diff line
/*
 * 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.core.plugin.bytebuddy;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;


/**
 * Return Type match.
 * Similar with {@link net.bytebuddy.matcher.ElementMatchers#returns},
 * the only different between them is this match use {@link String} to declare the type, instead of {@link Class}.
 * This can avoid the classloader risk.
 * <p>
 *
 * @author AI
 * 2019-08-15
 */
public class ReturnTypeNameMatch implements ElementMatcher<MethodDescription> {

    /**
     * the target return type
     */
    private String returnTypeName;

    /**
     * declare the match target method with the certain type.
     *
     * @param returnTypeName target return type
     */
    private ReturnTypeNameMatch(String returnTypeName) {
        this.returnTypeName = returnTypeName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean matches(MethodDescription target) {
        return target.getReturnType().asErasure().getName().equals(returnTypeName);
    }

    /**
     * The static method to create {@link ReturnTypeNameMatch}
     * This is a delegate method to follow byte-buddy {@link ElementMatcher}'s code style.
     *
     * @param returnTypeName target return type
     * @return new {@link ReturnTypeNameMatch} instance.
     */
    public static ElementMatcher<MethodDescription> returnsWithType(String returnTypeName) {
        return new ReturnTypeNameMatch(returnTypeName);
    }
}
+38 −0
Original line number Diff line number Diff line
/*
 * 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.core.plugin.bytebuddy;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author AI
 * 2019-08-15
 */
public class AnnotationTypeNameMatchTest {

    @Test
    public void testMatch() throws Exception {
        final ElementMatcher<MethodDescription> matcher = AnnotationTypeNameMatch.isAnnotatedWithType("org.apache.skywalking.apm.agent.core.plugin.bytebuddy.Inject");
        Assert.assertTrue(matcher.matches(new MethodDescription.ForLoadedConstructor(Person.class.getConstructor(String.class))));
        Assert.assertFalse(matcher.matches(new MethodDescription.ForLoadedConstructor(Person.class.getConstructor(String.class, int.class))));
    }

}
+34 −0
Original line number Diff line number Diff line
/*
 * 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.core.plugin.bytebuddy;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author AI
 * 2019-08-15
 */
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Inject {
}
Loading