@@ -53,6 +53,7 @@ public class Compiler implements Opcodes{
5353static final Symbol THROW = Symbol .create ("throw" );
5454static final Symbol MONITOR_ENTER = Symbol .create ("monitor-enter" );
5555static final Symbol MONITOR_EXIT = Symbol .create ("monitor-exit" );
56+ static final Symbol IMPORT = Symbol .create ("clojure.core" , "import*" );
5657//static final Symbol INSTANCE = Symbol.create("instance?");
5758
5859//static final Symbol THISFN = Symbol.create("thisfn");
@@ -91,6 +92,7 @@ public class Compiler implements Opcodes{
9192 FN , null ,
9293 QUOTE , new ConstantExpr .Parser (),
9394 THE_VAR , new TheVarExpr .Parser (),
95+ IMPORT , new ImportExpr .Parser (),
9496 DOT , new HostExpr .Parser (),
9597 ASSIGN , new AssignExpr .Parser (),
9698// TRY_FINALLY, new TryFinallyExpr.Parser(),
@@ -120,6 +122,7 @@ public class Compiler implements Opcodes{
120122private static final Type IFN_TYPE = Type .getType (IFn .class );
121123private static final Type RT_TYPE = Type .getType (RT .class );
122124final static Type CLASS_TYPE = Type .getType (Class .class );
125+ final static Type NS_TYPE = Type .getType (Namespace .class );
123126final static Type REFLECTOR_TYPE = Type .getType (Reflector .class );
124127final static Type THROWABLE_TYPE = Type .getType (Throwable .class );
125128final static Type BOOLEAN_OBJECT_TYPE = Type .getType (Boolean .class );
@@ -520,6 +523,48 @@ public Class getJavaClass() throws ClassNotFoundException{
520523 }
521524}
522525
526+ public static class ImportExpr implements Expr {
527+ public final String c ;
528+ final static Method forNameMethod = Method .getMethod ("Class forName(String)" );
529+ final static Method importClassMethod = Method .getMethod ("Class importClass(Class)" );
530+ final static Method derefMethod = Method .getMethod ("Object deref()" );
531+
532+ public ImportExpr (String c ){
533+ this .c = c ;
534+ }
535+
536+ public Object eval () throws Exception {
537+ Namespace ns = (Namespace ) RT .CURRENT_NS .deref ();
538+ ns .importClass (RT .classForName (c ));
539+ return null ;
540+ }
541+
542+ public void emit (C context , FnExpr fn , GeneratorAdapter gen ){
543+ gen .getStatic (RT_TYPE ,"CURRENT_NS" ,VAR_TYPE );
544+ gen .invokeVirtual (VAR_TYPE , derefMethod );
545+ gen .checkCast (NS_TYPE );
546+ gen .push (c );
547+ gen .invokeStatic (CLASS_TYPE , forNameMethod );
548+ gen .invokeVirtual (NS_TYPE , importClassMethod );
549+ if (context == C .STATEMENT )
550+ gen .pop ();
551+ }
552+
553+ public boolean hasJavaClass (){
554+ return false ;
555+ }
556+
557+ public Class getJavaClass () throws ClassNotFoundException {
558+ throw new IllegalArgumentException ("ImportExpr has no Java class" );
559+ }
560+
561+ static class Parser implements IParser {
562+ public Expr parse (C context , Object form ) throws Exception {
563+ return new ImportExpr ((String ) RT .second (form ));
564+ }
565+ }
566+ }
567+
523568public static abstract class LiteralExpr implements Expr {
524569 abstract Object val ();
525570
@@ -952,8 +997,8 @@ static class StaticFieldExpr extends FieldExpr implements AssignableExpr{
952997 public final String fieldName ;
953998 public final Class c ;
954999 public final java .lang .reflect .Field field ;
955- final static Method getStaticFieldMethod = Method .getMethod ("Object getStaticField(String,String)" );
956- final static Method setStaticFieldMethod = Method .getMethod ("Object setStaticField(String,String,Object)" );
1000+ // final static Method getStaticFieldMethod = Method.getMethod("Object getStaticField(String,String)");
1001+ // final static Method setStaticFieldMethod = Method.getMethod("Object setStaticField(String,String,Object)");
9571002 final int line ;
9581003
9591004 public StaticFieldExpr (int line , Class c , String fieldName ) throws Exception {
@@ -1238,8 +1283,9 @@ static class StaticMethodExpr extends MethodExpr{
12381283 public final String source ;
12391284 public final int line ;
12401285 public final java .lang .reflect .Method method ;
1286+ final static Method forNameMethod = Method .getMethod ("Class forName(String)" );
12411287 final static Method invokeStaticMethodMethod =
1242- Method .getMethod ("Object invokeStaticMethod(String ,String,Object[])" );
1288+ Method .getMethod ("Object invokeStaticMethod(Class ,String,Object[])" );
12431289
12441290
12451291 public StaticMethodExpr (String source , int line , Class c , String methodName , IPersistentVector args )
@@ -1338,6 +1384,7 @@ public void emit(C context, FnExpr fn, GeneratorAdapter gen){
13381384 else
13391385 {
13401386 gen .push (c .getName ());
1387+ gen .invokeStatic (CLASS_TYPE , forNameMethod );
13411388 gen .push (methodName );
13421389 emitArgsAsArray (args , fn , gen );
13431390 if (context == C .RETURN )
@@ -2083,7 +2130,8 @@ public static class NewExpr implements Expr{
20832130 public final Class c ;
20842131 final static Method invokeConstructorMethod =
20852132 Method .getMethod ("Object invokeConstructor(Class,Object[])" );
2086- final static Method forNameMethod = Method .getMethod ("Class classForName(String)" );
2133+ // final static Method forNameMethod = Method.getMethod("Class classForName(String)");
2134+ final static Method forNameMethod = Method .getMethod ("Class forName(String)" );
20872135
20882136
20892137 public NewExpr (Class c , IPersistentVector args , int line ) throws Exception {
@@ -2149,7 +2197,7 @@ public void emit(C context, FnExpr fn, GeneratorAdapter gen){
21492197 else
21502198 {
21512199 gen .push (c .getName ());
2152- gen .invokeStatic (RT_TYPE , forNameMethod );
2200+ gen .invokeStatic (CLASS_TYPE , forNameMethod );
21532201 MethodExpr .emitArgsAsArray (args , fn , gen );
21542202 if (context == C .RETURN )
21552203 {
0 commit comments