From 9ba7bd4df452a1d005fbf770141076e901ad61d6 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 28 Jun 2010 10:37:08 -0700 Subject: [PATCH] Throw IncorrectObjectTypeException on bad type hints If the type hint isn't OBJ_ANY and it doesn't match the actual type observed from the object store, define the reader to throw back an IncorrectObjectTypeException. This way the caller doesn't have to perform this check itself before it evaluates the object data, and we can simplify quite a few call sites. Change-Id: I9f0dfa033857f439c94245361fcae515bc0a6533 Signed-off-by: Shawn O. Pearce --- org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java | 6 +++++- .../src/org/eclipse/jgit/storage/file/WindowCursor.java | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java index 29d2eb6f3b..e8961bdce2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java @@ -45,6 +45,7 @@ package org.eclipse.jgit.lib; import java.io.IOException; +import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.storage.pack.ObjectReuseAsIs; @@ -103,10 +104,13 @@ public abstract class ObjectReader { * @return a {@link ObjectLoader} for accessing the object. * @throws MissingObjectException * the object does not exist. + * @throws IncorrectObjectTypeException + * typeHint was not OBJ_ANY, and the object's actual type does + * not match typeHint. * @throws IOException */ public abstract ObjectLoader openObject(AnyObjectId objectId, int typeHint) - throws MissingObjectException, IOException; + throws MissingObjectException, IncorrectObjectTypeException, IOException; /** * Release any resources used by this reader. diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WindowCursor.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WindowCursor.java index d5c2fd79dd..0272201e22 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WindowCursor.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/WindowCursor.java @@ -48,6 +48,7 @@ import java.io.IOException; import java.util.zip.DataFormatException; import java.util.zip.Inflater; +import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException; import org.eclipse.jgit.lib.AnyObjectId; @@ -81,13 +82,16 @@ final class WindowCursor extends ObjectReader implements ObjectReuseAsIs { } public ObjectLoader openObject(AnyObjectId objectId, int typeHint) - throws MissingObjectException, IOException { + throws MissingObjectException, IncorrectObjectTypeException, + IOException { final ObjectLoader ldr = db.openObject(this, objectId); if (ldr == null) { if (typeHint == OBJ_ANY) throw new MissingObjectException(objectId.copy(), "unknown"); throw new MissingObjectException(objectId.copy(), typeHint); } + if (typeHint != OBJ_ANY && ldr.getType() != typeHint) + throw new IncorrectObjectTypeException(objectId.copy(), typeHint); return ldr; } -- 2.39.5