NationalSecurityAgency/ghidra · error · ClassCastException

file {} does not contain {}. got {} instead.

Error message

file {} does not contain {}. got {} instead.

What it means

Thrown by the OpenedDomainFile constructor as a ClassCastException when the DomainFile's underlying domain object class is not assignable to the requested contentType. The caller asked to open a file as one DomainObject subtype (e.g. Program) but the file actually stores another (e.g. DataTypeManager or a different content type). The message reports the expected type and the actual class.

Source

Thrown at Ghidra/Debug/ProposedUtils/src/main/java/ghidra/framework/data/OpenedDomainFile.java:45

	public final T content;

	public static <T extends DomainObject> OpenedDomainFile<T> open(Class<T> contentType,
			DomainFile file, boolean okToUpgrade, boolean okToRecover, TaskMonitor monitor)
			throws VersionException, CancelledException, IOException {
		return new OpenedDomainFile<>(contentType, file, okToUpgrade, okToRecover, monitor);
	}

	public static <T extends DomainObject> OpenedDomainFile<T> open(Class<T> contentType,
			DomainFile file, TaskMonitor monitor)
			throws VersionException, CancelledException, IOException {
		return new OpenedDomainFile<>(contentType, file, false, false, monitor);
	}

	public OpenedDomainFile(Class<T> contentType, DomainFile file, boolean okToUpgrade,
			boolean okToRecover, TaskMonitor monitor)
			throws VersionException, CancelledException, IOException {
		if (!contentType.isAssignableFrom(file.getDomainObjectClass())) {
			throw new ClassCastException("file " + file + " does not contain " + contentType +
				". got " + file.getDomainObjectClass() + " instead.");
		}
		content = contentType.cast(file.getDomainObject(this, okToUpgrade, okToRecover, monitor));
	}

	@Override
	public void close() {
		if (content != null) {
			content.release(this);
		}
	}
}

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Before opening, check file.getDomainObjectClass() and verify contentType.isAssignableFrom(it).
  2. Pass the correct contentType matching what the file actually stores (inspect the file's content type metadata).
  3. Branch on the file's content type and open with the appropriate class literal for each.
  4. Guard UI/file-chooser flows to only offer files of the expected content type.

Example fix

// before
try (OpenedDomainFile<Program> f = OpenedDomainFile.open(Program.class, file, monitor)) {
  ... // throws if file is not a Program
}

// after
Class<?> doc = file.getDomainObjectClass();
if (!Program.class.isAssignableFrom(doc)) {
  throw new IOException("File " + file + " is not a program; got " + doc);
}
try (OpenedDomainFile<Program> f = OpenedDomainFile.open(Program.class, file, monitor)) {
  ...
}
Defensive patterns

Strategy: validation

Validate before calling

Class<?> doc = file.getDomainObjectClass();
if (!contentType.isAssignableFrom(doc)) {
  throw new IOException("File " + file + " stores " + doc + ", not " + contentType);
}
OpenedDomainFile.open(contentType, file, monitor);

Try / catch

try {
  return OpenedDomainFile.open(contentType, file, monitor);
} catch (ClassCastException e) {
  throw new IOException("Wrong content type for " + file, e);
}

Prevention

When it happens

Trigger: Calling OpenedDomainFile.open(Program.class, file, monitor) on a file that is a ByteProvider/DataTypeArchive rather than a Program. Passing a DomainFile obtained from a project folder that holds non-program content. Requesting a subtype the file's class does not implement.

Common situations: Tooling that assumes all domain files are programs; opening a file selected from a mixed-type project folder; version/format mismatches where the domain object class resolved differently; passing the wrong class literal.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/c88b29f0c3b8aded. Report an issue: GitHub.