Skip to content
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

Filter synthetic methods from the resource - bug #4005 fix #4112

Merged
merged 1 commit into from
May 21, 2019
Merged
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
@@ -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
Expand Down Expand Up @@ -45,7 +45,7 @@ public final class MethodList implements Iterable<AnnotatedMethod> {
* 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.
Expand All @@ -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.
Expand Down Expand Up @@ -89,15 +89,15 @@ private static List<Method> 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.
*/
public MethodList(Collection<Method> methods) {
List<AnnotatedMethod> 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));
}
}
Expand All @@ -109,7 +109,7 @@ public MethodList(Collection<Method> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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<Integer> {

@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);
Expand All @@ -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<String> s = new HashSet<String>();
for (AnnotatedMethod am : ml) {
Expand Down