/*
  This file is licensed 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.xmlunit.assertj3;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.error.AssertionErrorFactory;
import org.assertj.core.internal.Failures;

import java.util.List;

import static org.assertj.core.util.Lists.newArrayList;

/**
 * AbstractAssert allow only to throw errors extending ErrorMessageFactory.
 * CustomAbstractAssert allow to throw errors that based on AssertionErrorFactory.
 *
 * @see AbstractAssert#throwAssertionError(org.assertj.core.error.ErrorMessageFactory)
 * @see org.xmlunit.assertj3.error.ComparisonFailureErrorFactory
 * @since XMLUnit 2.8.1
 */
abstract class CustomAbstractAssert<SELF extends CustomAbstractAssert<SELF, ACTUAL>, ACTUAL> extends AbstractAssert<SELF, ACTUAL> {

    private static final String ORG_XMLUNIT_ASSERTJ_ERROR = "org.xmlunit.assertj3.error";

    CustomAbstractAssert(ACTUAL actual, Class<?> selfType) {
        super(actual, selfType);
    }

    void throwAssertionError(AssertionErrorFactory assertionErrorFactory) {
        AssertionError assertionError = assertionErrorFactory.newAssertionError(info.description(), info.representation());
        Failures.instance().removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError);
        removeCustomAssertRelatedElementsFromStackTraceIfNeeded(assertionError);
        throw assertionError;
    }

    private void removeCustomAssertRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError) {

        if (!Failures.instance().isRemoveAssertJRelatedElementsFromStackTrace()) return;

        List<StackTraceElement> filtered = newArrayList(assertionError.getStackTrace());
        for (StackTraceElement element : assertionError.getStackTrace()) {
            if (isElementOfCustomAssert(element)) {
                filtered.remove(element);
            }
        }
        StackTraceElement[] newStackTrace = filtered.toArray(new StackTraceElement[0]);
        assertionError.setStackTrace(newStackTrace);
    }

    private boolean isElementOfCustomAssert(StackTraceElement stackTraceElement) {

        Class<?> currentAssertClass = getClass();
        while (currentAssertClass != AbstractAssert.class) {
            if (stackTraceElement.getClassName().equals(currentAssertClass.getName())) {
                return true;
            }
            if (stackTraceElement.getClassName().contains(ORG_XMLUNIT_ASSERTJ_ERROR)) {
                return true;
            }
            currentAssertClass = currentAssertClass.getSuperclass();
        }
        return false;
    }
}

@@ -637,503 +637,131 @@
 rt;%0A
-import org.assertj.core.error.AssertionErrorFactory;%0Aimport org.assertj.core.internal.Failures;%0A%0Aimport java.util.List;%0A%0Aimport static org.assertj.core.util.Lists.newArrayList;%0A%0A/**%0A * AbstractAssert allow only to throw errors extending ErrorMessageFactory.%0A * CustomAbstractAssert allow to throw e
+%0A/**%0A * Ensures XMLUnit's stack trace lines get removed from AssertionE
 rror
+'
 s 
-that based on AssertionErrorFactory.%0A *%0A * @see AbstractAssert#throwAssertionError(org.assertj.core.error.ErrorMessageFactory)%0A * @see org.xmlunit.assertj3.error.ComparisonFailureErrorFactory
+stack traces if those of AssertJ are removed.
 %0A * 
@@ -1118,1089 +1118,73 @@
     
-void throwAssertionError(AssertionErrorFactory assertionErrorFactory) %7B%0A        AssertionError assertionError = assertionErrorFactory.newAssertionError(info.description(), info.representation());%0A        Failures.instance().removeAssertJRelatedElementsFromStackTraceIfNeeded(assertionError);%0A        removeCustomAssertRelatedElementsFromStackTraceIfNeeded(assertionError);%0A        throw assertionError;%0A    %7D%0A%0A    private void removeCustomAssertRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError) %7B%0A%0A        if (!Failures.instance().isRemoveAssertJRelatedElementsFromStackTrace()) return;%0A%0A        List%3CStackTraceElement%3E filtered = newArrayList(assertionError.getStackTrace());%0A        for (StackTraceElement element : assertionError.getStackTrace()) %7B%0A            if (isElementOfCustomAssert(element)) %7B%0A                filtered.remove(element);%0A            %7D%0A        %7D%0A        StackTraceElement%5B%5D newStackTrace = filtered.toArray(new StackTraceElement%5B0%5D);%0A        assertionError.setStackTrace(newStackTrace);%0A    %7D%0A%0A    private boolean isElementOfCustomAssert(
+// @Override%0A    protected boolean isElementOfCustomAssert(final 
 Stac
@@ -1222,256 +1222,8 @@
 ) %7B%0A
-%0A
-        Class%3C?%3E currentAssertClass = getClass();%0A        while (currentAssertClass != AbstractAssert.class) %7B%0A            if (stackTraceElement.getClassName().equals(currentAssertClass.getName())) %7B%0A                return true;%0A            %7D%0A    
     
@@ -1310,28 +1310,24 @@
             
-    
 return true;
@@ -1331,28 +1331,24 @@
 ue;%0A        
-  
-  
 %7D%0A          
@@ -1349,78 +1349,67 @@
     
-    currentAssertClass = currentAssertClass.getSuperclass();%0A        %7D
+// return super.isElementOfCustomAssert(stackTraceElement);
 %0A   
@@ -1433,10 +1433,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

prepare for isElementOfCustomAssert becoming protected - see #203