From e4bbfdaaa6e203e2fd83415b0ea54bc15d2632be Mon Sep 17 00:00:00 2001 From: Andreas Gudian Date: Mon, 9 Nov 2015 22:50:28 +0100 Subject: [PATCH] #688 Fix handling of overridden methods in adjacent interface hierarchies. --- .../ap/internal/util/Executables.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java b/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java index 3ba3aadb8..36619ffd9 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java @@ -18,10 +18,15 @@ */ package org.mapstruct.ap.internal.util; +import static javax.lang.model.util.ElementFilter.methodsIn; +import static org.mapstruct.ap.internal.util.workarounds.SpecificCompilerWorkarounds.replaceTypeElementIfNecessary; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import java.util.ListIterator; + import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; @@ -37,9 +42,6 @@ import org.mapstruct.ap.internal.services.Services; import org.mapstruct.ap.spi.AccessorNamingStrategy; import org.mapstruct.ap.spi.MethodType; -import static javax.lang.model.util.ElementFilter.methodsIn; -import static org.mapstruct.ap.internal.util.workarounds.SpecificCompilerWorkarounds.replaceTypeElementIfNecessary; - /** * Provides functionality around {@link ExecutableElement}s. * @@ -213,19 +215,26 @@ public class Executables { /** * @param elementUtils the elementUtils - * @param alreadyAdded the list of already collected methods of one type hierarchy (order is from sub-types to + * @param alreadyCollected the list of already collected methods of one type hierarchy (order is from sub-types to * super-types) * @param executable the method to check * @param parentType the type for which elements are collected * @return {@code true}, iff the given executable was not yet overridden by a method in the given list. */ - private static boolean wasNotYetOverridden(Elements elementUtils, List alreadyAdded, + private static boolean wasNotYetOverridden(Elements elementUtils, List alreadyCollected, ExecutableElement executable, TypeElement parentType) { - for ( ExecutableElement executableInSubtype : alreadyAdded ) { + for ( ListIterator it = alreadyCollected.listIterator(); it.hasNext(); ) { + ExecutableElement executableInSubtype = it.next(); if ( elementUtils.overrides( executableInSubtype, executable, parentType ) ) { return false; } + else if ( elementUtils.overrides( executable, executableInSubtype, parentType ) ) { + // remove the method from another interface hierarchy that is overridden by the executable to add + it.remove(); + return true; + } } + return true; }