[Java.Interop.Tools.JavaCallableWrappers] Error on bad IJavaObject #179
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=56819
Nothing prevents "anybody" from custom implementing
IJavaObject
:The problem is that the above doesn't actually work: the
IJavaObject.Handle
value contains the JNI Object Reference to passinto JNI. The above code will thus result in always passing
null
into Java code, which could result in a
NullPointerException
, butwill always result in not doing what was intended.
While it is theoretically possible to manually implement
IJavaObject
, it's not something I would want to contemplate, noris it for the faint of heart, so long ago we decided to emit a warning
if we encounter a type which:
Android.Runtime.IJavaObject
, andJava.Lang.Object
orJava.Lang.Throwable
.As such, the above
MyBadClass
would elicit the warning:This is all well and good, but (effectively) nobody reads warnings,
especially since our toolchain emits so many warnings that enabling
/warnaserror
is for the truly foolhardy, so lots of warnings is,unfortunately, normal.
Which brings us to Bug #56819: Could we make this an error?
Answer: Of course we can. That said, I have no idea how much existing
code this will break if we turned it into an error. That might be
good and useful, but if there's no way to disable the error,
existing apps which (appear to) work will no longer build.
That's not desirable.
Turn
JavaTypeScanner
from astatic
class into a normal class, andadd a new
JavaTypeScanner.ErrorOnCustomJavaObject
property which,when true, will cause
JavaTypeScanner
to emit an error instead ofa warning when it encounters types such as
MyBadClass
. (I'm turningit into a normal class instead of adding a new
JavaTypeScanner.GetJavaTypes()
overload in case we need to addadditional such "control" parameters in the future. Method overloads
will otherwise get unwieldy.)
JavaTypeScanner.ErrorOnCustomJavaObject
is all well and good, buthow do we actually emit a warning vs. an error? The previous
Action<string, object[]> log
delegate doesn't allow distinguishingbetween warnings and errors (and possible diagnostic messages), so how
would we eventually hook this up into xamarin-android's
<GenerateJavaStubs/>
task?Attempt to better handle that question by replacing
Action<string, object[]>
delegates withAction<TraceLevel, string>
delegates. This allows messages to explicitly specify
TraceLevel.Error, TraceLevel.Warning, or TraceLevel.Info (or others!)
so that messages can be sanely processed by downstream users.
Additionally, add a new
Diagnostic.CreateConsoleLogger()
methodwhich creates an
Action<TraceLevel, string>
which forwards messagesto
Console.Error
orConsole.Out
, as appropriate.