This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

cond-expand feature testing for presence of a Java class


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


Attachment: IfFeature.patch
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]