1 28 29 package com.caucho.ejb.gen; 30 31 import com.caucho.bytecode.JClass; 32 import com.caucho.bytecode.JMethod; 33 import com.caucho.java.JavaWriter; 34 import com.caucho.java.gen.BaseMethod; 35 import com.caucho.java.gen.MethodCallChain; 36 import com.caucho.util.L10N; 37 38 import java.io.IOException ; 39 import java.util.Collection ; 40 import java.util.Enumeration ; 41 import java.util.Iterator ; 42 43 46 public class EntityFindCollectionMethod extends BaseMethod { 47 private static L10N L = new L10N(EntityFindCollectionMethod.class); 48 49 private JMethod _apiMethod; 50 private String _contextClassName; 51 private String _prefix; 52 53 public EntityFindCollectionMethod(JMethod apiMethod, 54 JMethod implMethod, 55 String contextClassName, 56 String prefix) 57 { 58 super(apiMethod, 59 implMethod != null ? new MethodCallChain(implMethod) : null); 60 61 _apiMethod = apiMethod; 62 _contextClassName = contextClassName; 63 _prefix = prefix; 64 } 65 66 69 public JClass []getParameterTypes() 70 { 71 return _apiMethod.getParameterTypes(); 72 } 73 74 77 public JClass getReturnType() 78 { 79 return _apiMethod.getReturnType(); 80 } 81 82 87 public void generateCall(JavaWriter out, String []args) 88 throws IOException 89 { 90 out.println(getReturnType().getName() + " keys;"); 91 92 getCall().generateCall(out, "keys", "bean", args); 93 94 out.println(); 95 96 out.println("java.util.ArrayList values = new java.util.ArrayList();"); 97 98 JClass retType = getReturnType(); 99 if (retType.isAssignableTo(Collection .class)) { 100 out.println("java.util.Iterator iter = keys.iterator();"); 101 out.println("while (iter.hasNext()) {"); 102 out.pushDepth(); 103 out.println("Object key = iter.next();"); 104 } else if (retType.isAssignableTo(Iterator .class)) { 105 out.println("while (keys.hasNext()) {"); 106 out.pushDepth(); 107 out.println("Object key = keys.next();"); 108 } else if (retType.isAssignableTo(Enumeration .class)) { 109 out.println("while (keys.hasMoreElements()) {"); 110 out.pushDepth(); 111 out.println("Object key = keys.nextElement();"); 112 } 113 114 out.print("values.add(_server.getContext(key, false)"); 115 116 if ("RemoteHome".equals(_prefix)) 117 out.print(".getEJBObject());"); 118 else if ("LocalHome".equals(_prefix)) 119 out.print(".getEJBLocalObject());"); 120 else 121 throw new IOException (L.l("trying to create unknown type {0}", _prefix)); 122 123 out.popDepth(); 124 out.println("}"); 125 126 if (retType.isAssignableTo(Collection .class)) { 127 out.println("return values;"); 128 } else if (retType.isAssignableTo(Iterator .class)) { 129 out.println("return values.iterator();"); 130 } else if (retType.isAssignableTo(Enumeration .class)) { 131 out.println("return java.util.Collections.enumeration(values);"); 132 } 133 } 134 } 135 | Popular Tags |