From 9d2efa5d2aa12be0f81e3b01902b37b35e5e24a0 Mon Sep 17 00:00:00 2001 From: Jan Supol Date: Wed, 24 Apr 2019 18:24:58 +0200 Subject: [PATCH] Filter synthetic methods from the resource - bug #4005 fix Signed-off-by: Jan Supol --- .../jersey/server/model/MethodList.java | 12 ++--- .../jersey/server/model/MethodListTest.java | 51 ++++++++++++++++++- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/core-server/src/main/java/org/glassfish/jersey/server/model/MethodList.java b/core-server/src/main/java/org/glassfish/jersey/server/model/MethodList.java index 6d8fc06189..65f9406efa 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/model/MethodList.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/model/MethodList.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -45,7 +45,7 @@ public final class MethodList implements Iterable { * The method list contains {@link Class#getMethods() all methods} available * on the class. * - * The {@link Method#isBridge() bridge methods} and methods declared directly + * The {@link Method#isSynthetic() synthetic methods} and methods declared directly * on the {@link Object} class are filtered out. * * @param c class from which the method list is created. @@ -61,7 +61,7 @@ public MethodList(Class c) { * on the class or {@link Class#getDeclaredMethods() declared methods} only, * depending on the value of the {@code declaredMethods} parameter. * - * The {@link Method#isBridge() bridge methods} and methods declared directly + * The {@link Method#isSynthetic() synthetic methods} and methods declared directly * on the {@link Object} class are filtered out. * * @param c class from which the method list is created. @@ -89,7 +89,7 @@ private static List getMethods(Class c) { /** * Create new method list from the given collection of methods. * - * The {@link Method#isBridge() bridge methods} and methods declared directly + * The {@link Method#isSynthetic() synthetic methods} and methods declared directly * on the {@link Object} class are filtered out. * * @param methods methods to be included in the method list. @@ -97,7 +97,7 @@ private static List getMethods(Class c) { public MethodList(Collection methods) { List l = new ArrayList<>(methods.size()); for (Method m : methods) { - if (!m.isBridge() && m.getDeclaringClass() != Object.class) { + if (!m.isSynthetic() && m.getDeclaringClass() != Object.class) { l.add(new AnnotatedMethod(m)); } } @@ -109,7 +109,7 @@ public MethodList(Collection methods) { /** * Create new method list from the given array of methods. * - * The {@link Method#isBridge() bridge methods} and methods declared directly + * The {@link Method#isSynthetic() synthetic methods} and methods declared directly * on the {@link Object} class are filtered out. * * @param methods methods to be included in the method list. diff --git a/core-server/src/test/java/org/glassfish/jersey/server/model/MethodListTest.java b/core-server/src/test/java/org/glassfish/jersey/server/model/MethodListTest.java index c78d435a58..5934f4ac07 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/model/MethodListTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/model/MethodListTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -18,8 +18,10 @@ import org.junit.Test; +import java.util.Comparator; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertTrue; @@ -54,6 +56,33 @@ private void b() {} private void c() {} } + public class CSynthetic { + class CWithField { + private int x; + } + + public int a() { + return new CWithField().x; + } + + public void b(int x) { + new CWithField().x = x; + } + } + + public class CBridgeClass implements Comparator { + + @Override + public int compare(Integer o1, Integer o2) { + return 0; + } + + @Override + public boolean equals(Object obj) { + return false; + } + } + @Test public void testClassPublicMethods() { test(CPublic.class); @@ -74,12 +103,30 @@ public void testClassPrivateMethods() { test(CPrivate.class, true); } + @Test + public void testSyntheticMethods() { + assertTrue(CSynthetic.CWithField.class.getDeclaredMethods().length == 2); + + MethodList ml = new MethodList(CSynthetic.CWithField.class, true); + assertTrue(!ml.iterator().hasNext()); + } + + @Test + public void testBridgeMethods() { + assertTrue(CBridgeClass.class.getDeclaredMethods().length == 3); + + MethodList ml = new MethodList(CBridgeClass.class, true); + AtomicInteger count = new AtomicInteger(0); + ml.forEach(x -> count.addAndGet(1)); + assertTrue(count.get() == 2); + } + private void test(Class c) { test(c, false); } private void test(Class c, boolean privateMethods) { - MethodList ml = new MethodList(CPublic.class, privateMethods); + MethodList ml = new MethodList(c, privateMethods); Set s = new HashSet(); for (AnnotatedMethod am : ml) {