From 8195ef9e7a574a8b2e62d1fda3c0fe6a3249d21d Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sat, 14 May 2016 18:00:58 +0200 Subject: [PATCH] painless: Transform more try-catch-ClassCastException to isAssignableFrom --- .../painless/AnalyzerCaster.java | 19 +------ .../elasticsearch/painless/Definition.java | 56 ++++++------------- .../elasticsearch/painless/WriterUtility.java | 4 +- .../elasticsearch/painless/node/LBrace.java | 12 ++-- 4 files changed, 25 insertions(+), 66 deletions(-) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java index 5042608d063..d7275909858 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java @@ -467,23 +467,10 @@ public final class AnalyzerCaster { } } - try { - actual.clazz.asSubclass(expected.clazz); - + if (expected.clazz.isAssignableFrom(actual.clazz) || (explicit && actual.clazz.isAssignableFrom(expected.clazz))) { return cast; - } catch (final ClassCastException cce0) { - try { - if (explicit) { - expected.clazz.asSubclass(actual.clazz); - - return cast; - } else { - throw new ClassCastException( - "Error" + location + ": Cannot cast from [" + actual.name + "] to [" + expected.name + "]."); - } - } catch (final ClassCastException cce1) { - throw new ClassCastException("Error" + location + ": Cannot cast from [" + actual.name + "] to [" + expected.name + "]."); - } + } else { + throw new ClassCastException("Error" + location + ": Cannot cast from [" + actual.name + "] to [" + expected.name + "]."); } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java index e2f312be7b4..ae70fef59ee 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Definition.java @@ -1481,9 +1481,7 @@ public final class Definition { for (int count = 0; count < classes.length; ++count) { if (genargs != null) { - try { - genargs[count].clazz.asSubclass(args[count].clazz); - } catch (final ClassCastException exception) { + if (!args[count].clazz.isAssignableFrom(genargs[count].clazz)) { throw new ClassCastException("Generic argument [" + genargs[count].name + "]" + " is not a sub class of [" + args[count].name + "] in the constructor" + " [" + name + " ] from the struct [" + owner.name + "]."); @@ -1550,9 +1548,7 @@ public final class Definition { } if (genrtn != null) { - try { - genrtn.clazz.asSubclass(rtn.clazz); - } catch (final ClassCastException exception) { + if (!rtn.clazz.isAssignableFrom(genrtn.clazz)) { throw new ClassCastException("Generic return [" + genrtn.clazz.getCanonicalName() + "]" + " is not a sub class of [" + rtn.clazz.getCanonicalName() + "] in the method" + " [" + name + " ] from the struct [" + owner.name + "]."); @@ -1569,9 +1565,7 @@ public final class Definition { for (int count = 0; count < classes.length; ++count) { if (genargs != null) { - try { - genargs[count].clazz.asSubclass(args[count].clazz); - } catch (final ClassCastException exception) { + if (!args[count].clazz.isAssignableFrom(genargs[count].clazz)) { throw new ClassCastException("Generic argument [" + genargs[count].name + "] is not a sub class" + " of [" + args[count].name + "] in the " + (statik ? "function" : "method") + " [" + name + " ] from the struct [" + owner.name + "]."); @@ -1666,9 +1660,7 @@ public final class Definition { } if (generic != null) { - try { - generic.clazz.asSubclass(type.clazz); - } catch (final ClassCastException exception) { + if (!type.clazz.isAssignableFrom(generic.clazz)) { throw new ClassCastException("Generic type [" + generic.clazz.getCanonicalName() + "]" + " is not a sub class of [" + type.clazz.getCanonicalName() + "] for the field" + " [" + name + " ] from the struct [" + owner.name + "]."); @@ -1736,9 +1728,7 @@ public final class Definition { " not defined for copy to owner struct [" + owner.name + "]."); } - try { - owner.clazz.asSubclass(child.clazz); - } catch (final ClassCastException exception) { + if (!child.clazz.isAssignableFrom(owner.clazz)) { throw new ClassCastException("Child struct [" + child.name + "]" + " is not a super type of owner struct [" + owner.name + "] in copy."); } @@ -1844,13 +1834,10 @@ public final class Definition { Type argument = method.arguments.get(0); - try { - from.clazz.asSubclass(argument.clazz); - } catch (final ClassCastException cce0) { - try { - argument.clazz.asSubclass(from.clazz); + if (!argument.clazz.isAssignableFrom(from.clazz)) { + if (from.clazz.isAssignableFrom(argument.clazz)) { upcast = argument; - } catch (final ClassCastException cce1) { + } else { throw new ClassCastException("Transform with owner struct [" + owner.name + "]" + " and cast type from [" + from.name + "] to cast type to [" + to.name + "] using" + " function [" + name + "] cannot cast from type to the function input argument type."); @@ -1859,13 +1846,10 @@ public final class Definition { final Type rtn = method.rtn; - try { - rtn.clazz.asSubclass(to.clazz); - } catch (final ClassCastException cce0) { - try { - to.clazz.asSubclass(rtn.clazz); + if (!to.clazz.isAssignableFrom(rtn.clazz)) { + if (rtn.clazz.isAssignableFrom(to.clazz)) { downcast = to; - } catch (final ClassCastException cce1) { + } else { throw new ClassCastException("Transform with owner struct [" + owner.name + "]" + " and cast type from [" + from.name + "] to cast type to [" + to.name + "] using" + " function [" + name + "] cannot cast to type to the function return argument type."); @@ -1886,13 +1870,10 @@ public final class Definition { "] using method [" + name + "] does not have a single type argument."); } - try { - from.clazz.asSubclass(owner.clazz); - } catch (final ClassCastException cce0) { - try { - owner.clazz.asSubclass(from.clazz); + if (!owner.clazz.isAssignableFrom(from.clazz)) { + if (from.clazz.isAssignableFrom(owner.clazz)) { upcast = getType(owner.name); - } catch (final ClassCastException cce1) { + } else { throw new ClassCastException("Transform with owner struct [" + owner.name + "]" + " and cast type from [" + from.name + "] to cast type to [" + to.name + "] using" + " method [" + name + "] cannot cast from type to the method input argument type."); @@ -1901,13 +1882,10 @@ public final class Definition { final Type rtn = method.rtn; - try { - rtn.clazz.asSubclass(to.clazz); - } catch (final ClassCastException cce0) { - try { - to.clazz.asSubclass(rtn.clazz); + if (!to.clazz.isAssignableFrom(rtn.clazz)) { + if (rtn.clazz.isAssignableFrom(to.clazz)) { downcast = to; - } catch (final ClassCastException cce1) { + } else { throw new ClassCastException("Transform with owner struct [" + owner.name + "]" + " and cast type from [" + from.name + "] to cast type to [" + to.name + "]" + " using method [" + name + "] cannot cast to type to the method return argument type."); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterUtility.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterUtility.java index bb0b303764d..c3c26f309ea 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterUtility.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterUtility.java @@ -137,9 +137,7 @@ public final class WriterUtility { if (from.sort.numeric && from.sort.primitive && to.sort.numeric && to.sort.primitive) { adapter.cast(from.type, to.type); } else { - try { - from.clazz.asSubclass(to.clazz); - } catch (ClassCastException exception) { + if (!to.clazz.isAssignableFrom(from.clazz)) { adapter.checkCast(to.type); } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/LBrace.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/LBrace.java index c0f9ddebc68..38d31d65a93 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/LBrace.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/LBrace.java @@ -59,14 +59,10 @@ public final class LBrace extends ALink { return this; } else if (sort == Sort.DEF) { return new LDefArray(line, location, index).copy(this).analyze(settings, definition, variables); - } else { - if (Map.class.isAssignableFrom(before.clazz)) { - return new LMapShortcut(line, location, index).copy(this).analyze(settings, definition, variables); - } - - if (List.class.isAssignableFrom(before.clazz)) { - return new LListShortcut(line, location, index).copy(this).analyze(settings, definition, variables); - } + } else if (Map.class.isAssignableFrom(before.clazz)) { + return new LMapShortcut(line, location, index).copy(this).analyze(settings, definition, variables); + } else if (List.class.isAssignableFrom(before.clazz)) { + return new LListShortcut(line, location, index).copy(this).analyze(settings, definition, variables); } throw new IllegalArgumentException(error("Illegal array access on type [" + before.name + "]."));