Skip to content

Commit

Permalink
Simplify Generation of Checked and Unchecked Casts (#2322)
Browse files Browse the repository at this point in the history
  • Loading branch information
InsertCreativityHere authored Jun 24, 2024
1 parent 355bea2 commit 968ed6b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 240 deletions.
64 changes: 42 additions & 22 deletions cpp/src/slice2java/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4230,6 +4230,8 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)

const string package = getPackage(p);
const string contextParam = "java.util.Map<String, String> context";
const string prxName = p->name() + "Prx";
const string prxIName = "_" + prxName + "I";

out << sp;
writeDocComment(
Expand All @@ -4238,10 +4240,9 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
"Raises a local exception if a communication error occurs.\n"
"@param obj The untyped proxy.\n"
"@return A proxy for this type, or null if the object does not support this type.");
out << nl << "static " << p->name() << "Prx checkedCast(com.zeroc.Ice.ObjectPrx obj)";
out << nl << "static " << prxName << " checkedCast(com.zeroc.Ice.ObjectPrx obj)";
out << sb;
out << nl << "return com.zeroc.Ice.ObjectPrx._checkedCast(obj, ice_staticId(), " << p->name() << "Prx.class, _"
<< p->name() << "PrxI.class);";
out << nl << "return checkedCast(obj, noExplicitContext);";
out << eb;

out << sp;
Expand All @@ -4252,10 +4253,18 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
"@param obj The untyped proxy.\n"
"@param context The Context map to send with the invocation.\n"
"@return A proxy for this type, or null if the object does not support this type.");
out << nl << "static " << p->name() << "Prx checkedCast(com.zeroc.Ice.ObjectPrx obj, " << contextParam << ')';
out << nl << "static " << prxName << " checkedCast(com.zeroc.Ice.ObjectPrx obj, " << contextParam << ')';
out << sb;
out << nl << "return com.zeroc.Ice.ObjectPrx._checkedCast(obj, context, ice_staticId(), " << p->name()
<< "Prx.class, _" << p->name() << "PrxI.class);";
out << nl << "if (obj != null)";
out << sb;
out << nl << "try";
out << sb;
out << nl << "boolean ok = obj.ice_isA(ice_staticId(), context);";
out << nl << "return ok ? new " << prxIName << "(obj) : null;";
out << eb;
out << nl << "catch (com.zeroc.Ice.FacetNotExistException ex)" << sb << eb;
out << eb;
out << nl << "return null;";
out << eb;

out << sp;
Expand All @@ -4266,10 +4275,9 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
"@param obj The untyped proxy.\n"
"@param facet The name of the desired facet.\n"
"@return A proxy for this type, or null if the object does not support this type.");
out << nl << "static " << p->name() << "Prx checkedCast(com.zeroc.Ice.ObjectPrx obj, String facet)";
out << nl << "static " << prxName << " checkedCast(com.zeroc.Ice.ObjectPrx obj, String facet)";
out << sb;
out << nl << "return com.zeroc.Ice.ObjectPrx._checkedCast(obj, facet, ice_staticId(), " << p->name()
<< "Prx.class, _" << p->name() << "PrxI.class);";
out << nl << "return checkedCast(obj, facet, noExplicitContext);";
out << eb;

out << sp;
Expand All @@ -4281,11 +4289,10 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
"@param facet The name of the desired facet.\n"
"@param context The Context map to send with the invocation.\n"
"@return A proxy for this type, or null if the object does not support this type.");
out << nl << "static " << p->name() << "Prx checkedCast(com.zeroc.Ice.ObjectPrx obj, String facet, " << contextParam
out << nl << "static " << prxName << " checkedCast(com.zeroc.Ice.ObjectPrx obj, String facet, " << contextParam
<< ')';
out << sb;
out << nl << "return com.zeroc.Ice.ObjectPrx._checkedCast(obj, facet, context, ice_staticId(), " << p->name()
<< "Prx.class, _" << p->name() << "PrxI.class);";
out << nl << "return (obj == null) ? null : checkedCast(obj.ice_facet(facet), context);";
out << eb;

out << sp;
Expand All @@ -4294,10 +4301,9 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
"Downcasts the given proxy to this type without contacting the remote server.\n"
"@param obj The untyped proxy.\n"
"@return A proxy for this type.");
out << nl << "static " << p->name() << "Prx uncheckedCast(com.zeroc.Ice.ObjectPrx obj)";
out << nl << "static " << prxName << " uncheckedCast(com.zeroc.Ice.ObjectPrx obj)";
out << sb;
out << nl << "return com.zeroc.Ice.ObjectPrx._uncheckedCast(obj, " << p->name() << "Prx.class, _" << p->name()
<< "PrxI.class);";
out << nl << "return (obj == null) ? null : new " << prxIName << "(obj);";
out << eb;

out << sp;
Expand All @@ -4307,10 +4313,9 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
"@param obj The untyped proxy.\n"
"@param facet The name of the desired facet.\n"
"@return A proxy for this type.");
out << nl << "static " << p->name() << "Prx uncheckedCast(com.zeroc.Ice.ObjectPrx obj, String facet)";
out << nl << "static " << prxName << " uncheckedCast(com.zeroc.Ice.ObjectPrx obj, String facet)";
out << sb;
out << nl << "return com.zeroc.Ice.ObjectPrx._uncheckedCast(obj, facet, " << p->name() << "Prx.class, _"
<< p->name() << "PrxI.class);";
out << nl << "return (obj == null) ? null : new " << prxIName << "(obj.ice_facet(facet));";
out << eb;

// Generate overrides for all the methods on `ObjectPrx` with covariant return types.
Expand Down Expand Up @@ -4341,7 +4346,7 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
{
out << sp;
out << nl << "@Override";
out << nl << p->name() << "Prx " << method << ";";
out << nl << prxName << " " << method << ";";
}

out << sp;
Expand All @@ -4365,10 +4370,25 @@ Slice::Gen::ProxyVisitor::visitInterfaceDefEnd(const InterfaceDefPtr& p)
{
outi << nl << "@Deprecated";
}
outi << nl << "public class _" << p->name() << "PrxI";
outi << " extends com.zeroc.Ice._ObjectPrxFactoryMethods<" << p->name() << "Prx>";
outi << " implements " << p->name() << "Prx";
outi << nl << "public class " << prxIName;
outi << " extends com.zeroc.Ice._ObjectPrxFactoryMethods<" << prxName << ">";
outi << " implements " << prxName;
outi << sb;

// TODO: eventually remove this default constructor.
// Default constructor
outi << sp;
outi << nl << "public " << prxIName << "()";
outi << sb;
outi << nl << "super();";
outi << eb;

// Copy constructor
outi << sp;
outi << nl << "public " << prxIName << "(com.zeroc.Ice.ObjectPrx obj)";
outi << sb;
outi << nl << "super(obj);";
outi << eb;

outi << sp;
outi << nl << "private static final long serialVersionUID = 0L;";
Expand Down
220 changes: 11 additions & 209 deletions java/src/Ice/src/main/java/com/zeroc/Ice/ObjectPrx.java
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ static String ice_staticId() {
* @return <code>obj</code>.
*/
static ObjectPrx checkedCast(ObjectPrx obj) {
return obj;
return checkedCast(obj, noExplicitContext);
}

/**
Expand All @@ -661,7 +661,14 @@ static ObjectPrx checkedCast(ObjectPrx obj) {
* @return <code>obj</code>.
*/
static ObjectPrx checkedCast(ObjectPrx obj, java.util.Map<String, String> context) {
return obj;
if (obj != null) {
try {
boolean ok = obj.ice_isA(ice_staticId, context);
return ok ? obj : null;
} catch (FacetNotExistException ex) {
}
}
return null;
}

/**
Expand All @@ -688,18 +695,7 @@ static ObjectPrx checkedCast(ObjectPrx obj, String facet) {
* @return The new proxy with the specified facet.
*/
static ObjectPrx checkedCast(ObjectPrx obj, String facet, java.util.Map<String, String> context) {
ObjectPrx r = null;
if (obj != null) {
ObjectPrx p = obj.ice_facet(facet);
try {
boolean ok = p.ice_isA(ice_staticId, context);
assert (ok);
r = new _ObjectPrxI();
r._copyFrom(p);
} catch (FacetNotExistException ex) {
}
}
return r;
return (obj == null) ? null : checkedCast(obj.ice_facet(facet), context);
}

/**
Expand All @@ -721,13 +717,7 @@ static ObjectPrx uncheckedCast(ObjectPrx obj) {
* @return The new proxy with the specified facet.
*/
static ObjectPrx uncheckedCast(ObjectPrx obj, String facet) {
ObjectPrx r = null;
if (obj != null) {
ObjectPrx p = obj.ice_facet(facet);
r = new _ObjectPrxI();
r._copyFrom(p);
}
return r;
return (obj == null) ? null : new _ObjectPrxI(obj.ice_facet(facet));
}

/**
Expand All @@ -750,200 +740,12 @@ static ObjectPrx read(InputStream istr) {
return istr.readProxy();
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param id -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _checkedCast(ObjectPrx obj, String id, Class<T> proxy, Class<?> impl) {
return _checkedCast(obj, false, null, noExplicitContext, id, proxy, impl);
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param ctx -
* @param id -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _checkedCast(
ObjectPrx obj, java.util.Map<String, String> ctx, String id, Class<T> proxy, Class<?> impl) {
return _checkedCast(obj, false, null, ctx, id, proxy, impl);
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param facet -
* @param id -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _checkedCast(ObjectPrx obj, String facet, String id, Class<T> proxy, Class<?> impl) {
return _checkedCast(obj, true, facet, noExplicitContext, id, proxy, impl);
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param facet -
* @param ctx -
* @param id -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _checkedCast(
ObjectPrx obj,
String facet,
java.util.Map<String, String> ctx,
String id,
Class<T> proxy,
Class<?> impl) {
return _checkedCast(obj, true, facet, ctx, id, proxy, impl);
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param explicitFacet -
* @param facet -
* @param ctx -
* @param id -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _checkedCast(
ObjectPrx obj,
boolean explicitFacet,
String facet,
java.util.Map<String, String> ctx,
String id,
Class<T> proxy,
Class<?> impl) {
T r = null;
if (obj != null) {
if (explicitFacet) {
obj = obj.ice_facet(facet);
}
if (proxy.isInstance(obj)) {
r = proxy.cast(obj);
} else {
try {
boolean ok = obj.ice_isA(id, ctx);
if (ok) {
ObjectPrx h = null;
try {
h = _ObjectPrxI.class.cast(impl.getDeclaredConstructor().newInstance());
} catch (NoSuchMethodException ex) {
throw new SyscallException(ex);
} catch (java.lang.reflect.InvocationTargetException ex) {
throw new SyscallException(ex);
} catch (InstantiationException ex) {
throw new SyscallException(ex);
} catch (IllegalAccessException ex) {
throw new SyscallException(ex);
}
h._copyFrom(obj);
r = proxy.cast(h);
}
} catch (FacetNotExistException ex) {
}
}
}
return r;
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _uncheckedCast(ObjectPrx obj, Class<T> proxy, Class<?> impl) {
return _uncheckedCast(obj, false, null, proxy, impl);
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param facet -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _uncheckedCast(ObjectPrx obj, String facet, Class<T> proxy, Class<?> impl) {
return _uncheckedCast(obj, true, facet, proxy, impl);
}

/**
* @hidden
* @param <T> -
* @param obj -
* @param explicitFacet -
* @param facet -
* @param proxy -
* @param impl -
* @return -
*/
static <T> T _uncheckedCast(
ObjectPrx obj, boolean explicitFacet, String facet, Class<T> proxy, Class<?> impl) {
T r = null;
if (obj != null) {
try {
if (explicitFacet) {
ObjectPrx h = _ObjectPrxI.class.cast(impl.getDeclaredConstructor().newInstance());
h._copyFrom(obj.ice_facet(facet));
r = proxy.cast(h);
} else {
if (proxy.isInstance(obj)) {
r = proxy.cast(obj);
} else {
ObjectPrx h = _ObjectPrxI.class.cast(impl.getDeclaredConstructor().newInstance());
h._copyFrom(obj);
r = proxy.cast(h);
}
}
} catch (NoSuchMethodException ex) {
throw new SyscallException(ex);
} catch (java.lang.reflect.InvocationTargetException ex) {
throw new SyscallException(ex);
} catch (InstantiationException ex) {
throw new SyscallException(ex);
} catch (IllegalAccessException ex) {
throw new SyscallException(ex);
}
}
return r;
}

/**
* @hidden
* @param os -
*/
void _write(OutputStream os);

/**
* @hidden
* @param p -
*/
void _copyFrom(ObjectPrx p);

/**
* @hidden
* @return -
Expand Down
Loading

0 comments on commit 968ed6b

Please sign in to comment.