cond-expand feature testing for presence of a Java class
Jamison Hope
jrh@theptrgroup.com
Sun Apr 3 23:56:00 GMT 2011
I recently found myself wanting to conditionalize some code on the
presence of a Java class. At first, I tried to see if I could have a
module call (provide 'some-feature) only when this class was found,
but then I realized it would be much simpler to modify the feature
testing in IfFeature.java to recognize class names as a last resort.
This has proven quite handy for me, letting me do something like:
(cond-expand
(com.example.AClass ;; AClass is found in classpath, safe to use
(define-simple-class MySubclass (com.example.AClass) ...))
(else (error "AClass not found.")))
It seems unlikely for some unrelated feature's name to exactly match a
fully-qualified class name, so I think this patch is pretty safe.
$ svn diff kawa/standard/IfFeature.java
Index: kawa/standard/IfFeature.java
===================================================================
--- kawa/standard/IfFeature.java (revision 6942)
+++ kawa/standard/IfFeature.java (working copy)
@@ -86,7 +86,13 @@
Declaration decl =
Compilation.getCurrent().lookup(provide_symbol, -1);
if (decl!=null && ! decl.getFlag(Declaration.IS_UNKNOWN))
return true;
- return false;
+
+ try {
+ Class.forName(name);
+ return true;
+ } catch (ClassNotFoundException ex) {
+ return false;
+ }
}
public static final String PROVIDE_PREFIX = "%provide%";
- Jamie
--
Jamison Hope
The PTR Group
www.theptrgroup.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: IfFeature.patch
Type: application/octet-stream
Size: 598 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/kawa/attachments/20110403/bd4092c7/attachment.obj>
More information about the Kawa
mailing list