spoon.testing.AbstractAssert Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spoon-core Show documentation
Show all versions of spoon-core Show documentation
Spoon is a tool for meta-programming, analysis and transformation of Java programs.
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2023 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.testing;
import spoon.SpoonException;
import spoon.processing.Processor;
import java.util.LinkedList;
/**
* Base contract for all assertion objects: the minimum functionality that
* any assertion object should provide.
*
* @param
* the self type of this assertion class.
* @param
* the type of the actual value.
*/
public abstract class AbstractAssert, A> {
protected final LinkedList> processors = new LinkedList<>();
protected final A actual;
protected final T myself;
protected AbstractAssert(A actual, Class> selfType) {
this.myself = (T) selfType.cast(this);
this.actual = actual;
}
/**
* Applies the processor on the actual value.
*
* @param processor
* the given processor.
* @return {@code this} assertion object.
*/
public T withProcessor(Processor> processor) {
processors.add(processor);
return myself;
}
/**
* Applies the processor on the actual value.
*
* @param processor
* the class of the given processor.
* @return {@code this} assertion object.
*/
public T withProcessor(Class extends Processor>> processor) {
try {
withProcessor(processor.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Can't instantiate class processor.", e);
}
return myself;
}
/**
* Applies the processor on the actual value.
*
* @param qualifiedName
* the qualified name of the given processor.
* @return {@code this} assertion object.
*/
public T withProcessor(String qualifiedName) {
try {
withProcessor((Class extends Processor>>) Thread.currentThread().getContextClassLoader().loadClass(qualifiedName));
} catch (ClassNotFoundException e) {
throw new SpoonException("Unable to load processor \"" + qualifiedName + "\"", e);
}
return myself;
}
public int hashCode() {
return 1;
}
}