Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CLJ-1248 - Include type information in reflection warning messages

Signed-off-by: Stuart Halloway <stu@cognitect.com>
  • Loading branch information...
commit e8fc421d9e63be6c48797b4a3275e4551651b467 1 parent 762db74
@qerub qerub authored Stuart Halloway committed
Showing with 66 additions and 16 deletions.
  1. +46 −12 src/jvm/clojure/lang/Compiler.java
  2. +20 −4 test/clojure/test_clojure/rt.clj
View
58 src/jvm/clojure/lang/Compiler.java
@@ -1095,9 +1095,18 @@ public InstanceFieldExpr(int line, int column, Expr target, String fieldName, Sy
this.tag = tag;
if(field == null && RT.booleanCast(RT.WARN_ON_REFLECTION.deref()))
{
- RT.errPrintWriter()
- .format("Reflection warning, %s:%d:%d - reference to field %s can't be resolved.\n",
- SOURCE_PATH.deref(), line, column, fieldName);
+ if(targetClass == null)
+ {
+ RT.errPrintWriter()
+ .format("Reflection warning, %s:%d:%d - reference to field %s can't be resolved.\n",
+ SOURCE_PATH.deref(), line, column, fieldName);
+ }
+ else
+ {
+ RT.errPrintWriter()
+ .format("Reflection warning, %s:%d:%d - reference to field %s on %s can't be resolved.\n",
+ SOURCE_PATH.deref(), line, column, fieldName, targetClass.getName());
+ }
}
}
@@ -1396,8 +1405,15 @@ public InstanceMethodExpr(String source, int line, int column, Symbol tag, Expr
{
List methods = Reflector.getMethods(target.getJavaClass(), args.count(), methodName, false);
if(methods.isEmpty())
+ {
method = null;
- //throw new IllegalArgumentException("No matching method found");
+ if(RT.booleanCast(RT.WARN_ON_REFLECTION.deref()))
+ {
+ RT.errPrintWriter()
+ .format("Reflection warning, %s:%d:%d - call to method %s on %s can't be resolved (no such method).\n",
+ SOURCE_PATH.deref(), line, column, methodName, target.getJavaClass().getName());
+ }
+ }
else
{
int methodidx = 0;
@@ -1421,16 +1437,23 @@ public InstanceMethodExpr(String source, int line, int column, Symbol tag, Expr
m = Reflector.getAsMethodOfPublicBase(m.getDeclaringClass(), m);
}
method = m;
+ if(method == null && RT.booleanCast(RT.WARN_ON_REFLECTION.deref()))
+ {
+ RT.errPrintWriter()
+ .format("Reflection warning, %s:%d:%d - call to method %s on %s can't be resolved (argument types: %s).\n",
+ SOURCE_PATH.deref(), line, column, methodName, target.getJavaClass().getName(), getTypeStringForArgs(args));
+ }
}
}
else
- method = null;
-
- if(method == null && RT.booleanCast(RT.WARN_ON_REFLECTION.deref()))
{
- RT.errPrintWriter()
- .format("Reflection warning, %s:%d:%d - call to %s can't be resolved.\n",
- SOURCE_PATH.deref(), line, column, methodName);
+ method = null;
+ if(RT.booleanCast(RT.WARN_ON_REFLECTION.deref()))
+ {
+ RT.errPrintWriter()
+ .format("Reflection warning, %s:%d:%d - call to method %s can't be resolved (target class is unknown).\n",
+ SOURCE_PATH.deref(), line, column, methodName);
+ }
}
}
@@ -1580,8 +1603,8 @@ public StaticMethodExpr(String source, int line, int column, Symbol tag, Class c
if(method == null && RT.booleanCast(RT.WARN_ON_REFLECTION.deref()))
{
RT.errPrintWriter()
- .format("Reflection warning, %s:%d:%d - call to %s can't be resolved.\n",
- SOURCE_PATH.deref(), line, column, methodName);
+ .format("Reflection warning, %s:%d:%d - call to static method %s on %s can't be resolved (argument types: %s).\n",
+ SOURCE_PATH.deref(), line, column, methodName, c.getName(), getTypeStringForArgs(args));
}
}
@@ -2328,6 +2351,17 @@ static public boolean subsumes(Class[] c1, Class[] c2){
return better;
}
+static String getTypeStringForArgs(IPersistentVector args){
+ StringBuilder sb = new StringBuilder();
+ for(int i = 0; i < args.count(); i++)
+ {
+ Expr arg = (Expr) args.nth(i);
+ if (i > 0) sb.append(", ");
+ sb.append(arg.hasJavaClass() ? arg.getJavaClass().getName() : "unknown");
+ }
+ return sb.toString();
+}
+
static int getMatchingParams(String methodName, ArrayList<Class[]> paramlists, IPersistentVector argexprs,
List<Class> rets)
{
View
24 test/clojure/test_clojure/rt.clj
@@ -35,17 +35,33 @@
(should-print-err-message
#"Reflection warning, .*:\d+:\d+ - reference to field blah can't be resolved\.\r?\n"
(defn foo [x] (.blah x))))
- (testing "reflection cannot resolve instance method"
+ (testing "reflection cannot resolve field on known class"
(should-print-err-message
- #"Reflection warning, .*:\d+:\d+ - call to zap can't be resolved\.\r?\n"
+ #"Reflection warning, .*:\d+:\d+ - reference to field blah on java\.lang\.String can't be resolved\.\r?\n"
+ (defn foo [^String x] (.blah x))))
+ (testing "reflection cannot resolve instance method because it is missing"
+ (should-print-err-message
+ #"Reflection warning, .*:\d+:\d+ - call to method zap on java\.lang\.String can't be resolved \(no such method\)\.\r?\n"
+ (defn foo [^String x] (.zap x 1))))
+ (testing "reflection cannot resolve instance method because it has incompatible argument types"
+ (should-print-err-message
+ #"Reflection warning, .*:\d+:\d+ - call to method getBytes on java\.lang\.String can't be resolved \(argument types: java\.util\.regex\.Pattern\)\.\r?\n"
+ (defn foo [^String x] (.getBytes x #"boom"))))
+ (testing "reflection cannot resolve instance method because it has unknown argument types"
+ (should-print-err-message
+ #"Reflection warning, .*:\d+:\d+ - call to method getBytes on java\.lang\.String can't be resolved \(argument types: unknown\)\.\r?\n"
+ (defn foo [^String x y] (.getBytes x y))))
+ (testing "reflection cannot resolve instance method because target class is unknown"
+ (should-print-err-message
+ #"Reflection warning, .*:\d+:\d+ - call to method zap can't be resolved \(target class is unknown\)\.\r?\n"
(defn foo [x] (.zap x 1))))
(testing "reflection cannot resolve static method"
(should-print-err-message
- #"Reflection warning, .*:\d+:\d+ - call to valueOf can't be resolved\.\r?\n"
+ #"Reflection warning, .*:\d+:\d+ - call to static method valueOf on java\.lang\.Integer can't be resolved \(argument types: java\.util\.regex\.Pattern\)\.\r?\n"
(defn foo [] (Integer/valueOf #"boom"))))
(testing "reflection cannot resolve constructor"
(should-print-err-message
- #"Reflection warning, .*:\d+:\d+ - call to java.lang.String ctor can't be resolved\.\r?\n"
+ #"Reflection warning, .*:\d+:\d+ - call to java\.lang\.String ctor can't be resolved\.\r?\n"
(defn foo [] (String. 1 2 3)))))
(def example-var)
Please sign in to comment.
Something went wrong with that request. Please try again.