this(sourceRef, format.formatted(args));
}
+ public StatementSourceException(final StatementSourceReference sourceRef, final Throwable cause,
+ final String format, final Object... args) {
+ this(sourceRef, format.formatted(args), cause);
+ }
+
private static String createMessage(final StatementSourceReference sourceRef, final String message) {
return requireNonNull(message) + " [at " + requireNonNull(sourceRef) + ']';
}
private SomeModifiersUnresolvedException addSourceExceptions(final List<SourceSpecificContext> sourcesToProgress) {
boolean addedCause = false;
SomeModifiersUnresolvedException buildFailure = null;
- for (final SourceSpecificContext failedSource : sourcesToProgress) {
- final Optional<SourceException> optSourceEx = failedSource.failModifiers(currentPhase);
+ for (var failedSource : sourcesToProgress) {
+ final var optSourceEx = failedSource.failModifiers(currentPhase);
if (optSourceEx.isEmpty()) {
continue;
}
- final SourceException sourceEx = optSourceEx.orElseThrow();
+ final var sourceEx = optSourceEx.orElseThrow();
// Workaround for broken logging implementations which ignore
// suppressed exceptions
- final Throwable cause = sourceEx.getCause() != null ? sourceEx.getCause() : sourceEx;
+ final var cause = sourceEx.getCause() != null ? sourceEx.getCause() : sourceEx;
if (LOG.isDebugEnabled()) {
LOG.error("Failed to parse YANG from source {}", failedSource, sourceEx);
} else {
LOG.error("Failed to parse YANG from source {}: {}", failedSource, cause.getMessage());
}
- final Throwable[] suppressed = sourceEx.getSuppressed();
+ final var suppressed = sourceEx.getSuppressed();
if (suppressed.length > 0) {
LOG.error("{} additional errors reported:", suppressed.length);
int count = 1;
- for (final Throwable t : suppressed) {
- LOG.error("Error {}: {}", count, t.getMessage());
+ for (var supp : suppressed) {
+ LOG.error("Error {}: {}", count, supp.getMessage());
count++;
}
}
if (!addedCause) {
addedCause = true;
- final SourceIdentifier sourceId = failedSource.identifySource();
+ final var sourceId = failedSource.identifySource();
buildFailure = new SomeModifiersUnresolvedException(currentPhase, sourceId, sourceEx);
} else {
buildFailure.addSuppressed(sourceEx);
import org.opendaylight.yangtools.yang.common.YangVersion;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceException;
import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
+ finishedPhase + "]";
}
- Optional<SourceException> failModifiers(final ModelProcessingPhase identifier) {
- final List<SourceException> exceptions = new ArrayList<>();
- for (final ModifierImpl mod : modifiers.get(identifier)) {
+ Optional<StatementSourceException> failModifiers(final ModelProcessingPhase identifier) {
+ final var exceptions = new ArrayList<StatementSourceException>();
+ for (var mod : modifiers.get(identifier)) {
try {
mod.failModifier();
- } catch (final SourceException e) {
+ } catch (StatementSourceException e) {
exceptions.add(e);
}
}
case 0 -> Optional.empty();
case 1 -> Optional.of(exceptions.get(0));
default -> {
- final String message = String.format("Yang model processing phase %s failed", identifier);
- final InferenceException ex = new InferenceException(message, root, exceptions.get(0));
+ final var ex = new InferenceException("Yang model processing phase " + identifier + " failed", root,
+ exceptions.get(0));
exceptions.listIterator(1).forEachRemaining(ex::addSuppressed);
yield Optional.of(ex);
}
import org.opendaylight.yangtools.yang.model.api.PathExpression.DerefSteps;
import org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps;
import org.opendaylight.yangtools.yang.model.api.PathExpression.Steps;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceException;
import org.opendaylight.yangtools.yang.parser.antlr.LeafRefPathLexer;
import org.opendaylight.yangtools.yang.parser.antlr.LeafRefPathParser;
import org.opendaylight.yangtools.yang.parser.antlr.LeafRefPathParser.Absolute_pathContext;
import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.SourceExceptionParser;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
import org.opendaylight.yangtools.yang.xpath.api.YangBinaryExpr;
import org.opendaylight.yangtools.yang.xpath.api.YangBinaryOperator;
import org.opendaylight.yangtools.yang.xpath.api.YangExpr;
PathExpression parseExpression(final StmtContext<?, ?, ?> ctx, final String pathArg) {
try {
return super.parseExpression(ctx, pathArg);
- } catch (IllegalStateException | SourceException e) {
+ } catch (IllegalStateException | StatementSourceException e) {
LOG.warn("Failed to parse expression '{}'", pathArg, e);
return new UnparsedPathExpression(pathArg, e);
}
import org.hamcrest.Matcher;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceException;
import org.opendaylight.yangtools.yang.model.ri.type.InvalidBitDefinitionException;
import org.opendaylight.yangtools.yang.model.ri.type.InvalidEnumDefinitionException;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
return assertInstanceOf(cause, actual);
}
- public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
+ public static <E extends StatementSourceException> @NonNull E assertException(final Class<E> cause,
final Matcher<String> matcher, final String... yangResourceName) {
final var ret = assertException(cause, yangResourceName);
assertThat(ret.getMessage(), matcher);
return ret;
}
- public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
+ public static <E extends StatementSourceException> @NonNull E assertExceptionDir(final String yangResourceName,
final Class<E> cause) {
final var ex = assertThrows(SomeModifiersUnresolvedException.class,
() -> TestUtils.loadModules(yangResourceName));
return assertInstanceOf(cause, actual);
}
- public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
+ public static <E extends StatementSourceException> @NonNull E assertExceptionDir(final String yangResourceName,
final Class<E> cause, final Matcher<String> matcher) {
final var ret = assertExceptionDir(yangResourceName, cause);
assertThat(ret.getMessage(), matcher);
package org.opendaylight.yangtools.yang.parser.spi.meta;
import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceException;
import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
/**
- * Thrown when there is an inference error.
+ * A {@link StatementSourceException} indicating an inference problem, e.g. a problem with how statements interact with
+ * each other.
*/
-public class InferenceException extends SourceException {
- private static final long serialVersionUID = 1L;
+public final class InferenceException extends StatementSourceException {
+ @java.io.Serial
+ private static final long serialVersionUID = 2L;
public InferenceException(final @NonNull String message, final @NonNull StatementSourceReference source) {
- super(message, source);
+ super(source, message);
}
public InferenceException(final @NonNull String message, final @NonNull StatementSourceReference source,
final Throwable cause) {
- super(message, source, cause);
+ super(source, message, cause);
}
public InferenceException(final @NonNull StatementSourceReference source, final @NonNull String format,
final Object... args) {
- this(String.format(format, args), source);
+ super(source, format, args);
}
public InferenceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt) {
- super(message, stmt);
+ super(stmt.sourceReference(), message);
}
public InferenceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt, final Throwable cause) {
- super(message, stmt, cause);
+ super(stmt.sourceReference(), message, cause);
}
public InferenceException(final @NonNull CommonStmtCtx stmt, final @NonNull String format,
final Object... args) {
- this(stmt.sourceReference(), format, args);
+ super(stmt.sourceReference(), format, args);
}
/**
throw new InferenceException(stmt.sourceReference(), format, args);
}
}
+
+ /**
+ * Throw an instance of this exception if an object is null. If the object is non-null, it will
+ * be returned as the result of this method.
+ *
+ * @param obj Object reference to be checked
+ * @param stmt Statement context, not retained
+ * @param format Format string, according to {@link String#format(String, Object...)}.
+ * @param args Format string arguments, according to {@link String#format(String, Object...)}
+ * @return Object if it is not null
+ * @throws InferenceException if object is null
+ */
+ public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull CommonStmtCtx stmt,
+ final @NonNull String format, final Object... args) {
+ if (obj == null) {
+ throw new InferenceException(stmt.sourceReference(), format, args);
+ }
+ return obj;
+ }
+
+ /**
+ * Throw an instance of this exception if an object is null. If the object is non-null, it will
+ * be returned as the result of this method.
+ *
+ * @param obj Object reference to be checked
+ * @param source Statement source reference
+ * @param format Format string, according to {@link String#format(String, Object...)}.
+ * @param args Format string arguments, according to {@link String#format(String, Object...)}
+ * @return Object if it is not null
+ * @throws InferenceException if object is null
+ */
+ public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
+ final @NonNull String format, final Object... args) {
+ if (obj == null) {
+ throw new InferenceException(source, format, args);
+ }
+ return obj;
+ }
}
*/
public SourceException(final @NonNull StatementSourceReference source, final @NonNull String format,
final Object... args) {
- this(String.format(format, args), source);
+ super(source, format, args);
}
/**
*/
public SourceException(final @NonNull StatementSourceReference source, final Throwable cause,
final @NonNull String format, final Object... args) {
- this(String.format(format, args), source, cause);
+ super(source, cause, format, args);
}
/**
* @param stmt Statement context, not retained
*/
public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt) {
- this(message, stmt.sourceReference());
+ super(stmt.sourceReference(), message);
}
/**
* @param cause Underlying cause of this exception
*/
public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt, final Throwable cause) {
- this(message, stmt.sourceReference(), cause);
+ super(stmt.sourceReference(), message, cause);
}
/**
* @param args Format string arguments, according to {@link String#format(String, Object...)}
*/
public SourceException(final @NonNull CommonStmtCtx stmt, final @NonNull String format, final Object... args) {
- this(stmt.sourceReference(), format, args);
+ super(stmt.sourceReference(), format, args);
}
/**