-- @atlcompiler atl2006 -- $Id$ -- Java all versions code generation helpers library JavaMappings; uses Java; -- Java1 or Java2 uses Strings; ------------------------------- General ----------------------------------- helper context String def : value() : String = if self = 'language' then 'java' else if self = 'separator' then '.' else '' endif endif; ------------------------------- Accessors ----------------------------------- helper context String def : setterBody() : String = 'this.' + self + ' = ' + self + ';'; helper context String def : dualSetterBody(otherEnd : String) : String = 'if (this.' + self + ' != ' + self + ') {\n' + ' this.' + self + ' = ' + self + ';\n' + ' ' + self + '.set' + otherEnd + '(this);\n' + '}'; helper context String def : addRemoveSetterBody(otherEnd : String) : String = if otherEnd <> '' then 'if (this.' + self + ' != ' + self + ') {\n' + ' if (this.' + self + ' != null) this.' + self + '.remove' + otherEnd + '(this);\n' + ' this.' + self + ' = ' + self + ';\n' + ' if (' + self + ' != null) ' + self + '.add' + otherEnd + '(this);\n' + '}' else 'this.' + self + ' = ' + self + ';' endif; helper context String def : getterBody() : String = 'return ' + self + ';'; helper context String def : multiGetterBody() : String = self.javaMultiGetterBody(); helper context String def : getAtBody(type : String) : String = self.javaGetAt(type); helper context String def : indexOfBody() : String = 'return this.' + self + '.indexOf(' + self + ');'; helper context String def : javaNotContainsCheck(body : String) : String = 'if (! this.' + self + '.contains(' + self + ')) {\n' + body + '\n}'; helper context String def : javaContainsCheck(body : String) : String = 'if (this.' + self + '.contains(' + self + ')) {\n' + body + '\n}'; helper context String def : javaOtherEndSetter(otherEnd : String) : String = self + '.set' + otherEnd + '(this);'; helper context String def : adderBody(isUnique : Boolean) : String = if isUnique then self.javaNotContainsCheck(' ' + self.javaAdder()) else self.javaAdder() endif; helper context String def : javaOtherEndAdder(otherEnd : String) : String = self + '.add' + otherEnd + '(this);'; helper context String def : javaOtherEndResetter(otherEnd : String) : String = self + '.set' + otherEnd + '(null);'; helper context String def : javaOtherEndRemover(otherEnd : String) : String = self + '.remove' + otherEnd + '(this);'; helper context String def : setterAdderBody(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.javaAdder() + '\n' + ' ' + self.javaOtherEndSetter(otherEnd)); helper context String def : insertBody(isUnique : Boolean) : String = if isUnique then self.javaNotContainsCheck(' ' + self.javaInsert()) else self.javaInsert() endif; helper context String def : setterInsertBody(otherEnd : String) : String = self.javaNotContainsCheck( ' ' + self.javaInsert() + '\n' + ' ' + self.javaOtherEndSetter(otherEnd)); helper context String def : removerBody() : String = self.javaRemover(); helper context String def : setterRemoverBody(otherEnd : String) : String = self.javaContainsCheck( ' ' + self.javaRemover() + '\n' + ' ' + self.javaOtherEndResetter(otherEnd)); helper context String def : dualRemoverBody(otherEnd : String) : String = self.javaContainsCheck( ' ' + self.javaRemover() + '\n' + ' ' + self.javaOtherEndRemover(otherEnd)); ------------------------------- DataTypes ----------------------------------- helper context String def : implTypeName() : String = if self = 'Integer' then 'java::lang::int' else if self = 'Natural' then 'java::lang::long' else if self = 'UnlimitedNatural' then 'java::lang::int' else if self = 'Real' then 'java::lang::double' else if self = 'Boolean' then 'java::lang::boolean' else if self = 'String' then 'java::lang::String' else if self = 'OclAny' then 'java::lang::Object' else if self = 'Exception' then 'java::lang::Exception' else self.javaImplTypeName() endif endif endif endif endif endif endif endif; helper context String def : implCollectionReturnTypeName() : String = 'java::util::Enumeration'; helper context String def : defaultValueBody() : String = if self = 'java::lang::int' then '0' else if self = 'java::lang::long' then '0' else if self = 'java::lang::double' then '0.0' else if self = 'java::lang::boolean' then 'false' else self.javaDefaultValueBody() endif endif endif endif; ------------------------------- Observer ----------------------------------- helper context String def : javaBoxedType() : String = if self = 'OclAny' then 'Object' else if self = 'Natural' then 'Long' else if self = 'UnlimitedNatural' then 'Integer' else if self = 'Real' then 'Double' else self endif endif endif endif; helper context String def : javaBoxing(type : String) : String = if type = 'Boolean' then 'new Boolean(' + self + ')' else if type = 'Integer' then 'new Integer(' + self + ')' else if type = 'Natural' then 'new Long(' + self + ')' else if type = 'UnlimitedNatural' then 'new Integer(' + self + ')' else if type = 'Real' then 'new Double(' + self + ')' else self endif endif endif endif endif; helper context String def : javaUnboxing(type : String) : String = if type = 'Boolean' then '(' + self + ').booleanValue()' else if type = 'Integer' then '(' + self + ').intValue()' else if type = 'Natural' then '(' + self + ').longValue()' else if type = 'UnlimitedNatural' then '(' + self + ').intValue()' else if type = 'Real' then '(' + self + ').doubleValue()' else self endif endif endif endif endif; helper context String def : javaTypedValue(type : String) : String = '(' + type.javaBoxedType() + ') ' + self; helper context String def : javaUnboxValue(type : String) : String = self.javaTypedValue(type).javaUnboxing(type); helper context String def : notifyObserversBody() : String = 'for (java.util.Enumeration os = getObservers(); os.hasMoreElements();) {\n' + ' ((' + self + ') os.nextElement()).update(name, value);\n' + '}'; helper context String def : updateBody(propertyName : String, parTypeName : String) : String = '\nif (name.equals("' + propertyName + '")) {\n' + ' ' + self + '(' + 'value'.javaUnboxValue(parTypeName) + ');\n' + '}'; helper context String def : setterNotifyBody(type : String, body : String, reflective : Boolean) : String = 'if (this.' + self + ' != ' + self + ') {\n' + body + '\n' + if reflective then self.reflectiveNotifyStanza(type) else self.notifyStanza(type) endif + '\n' + '}'; helper context String def : adderNotifyBody(type : String, body : String, isUnique : Boolean, reflective : Boolean) : String = if isUnique then self.javaNotContainsCheck(self.javaAdderNotifyBody(type, body, reflective)) else self.javaAdderNotifyBody(type, body, reflective) endif; helper context String def : javaAdderNotifyBody(type : String, body : String, reflective : Boolean) : String = body + '\n' + if reflective then self.reflectiveNotifyStanza(type) else self.notifyStanza(type) endif; helper context String def : removerNotifyBody(type : String, body : String, reflective : Boolean) : String = self.javaContainsCheck(self.javaRemoverNotifyBody(type, body, reflective)); helper context String def : javaRemoverNotifyBody(type : String, body : String, reflective : Boolean) : String = body + '\n' + if reflective then self.reflectiveNotifyStanza(type) else self.notifyStanza(type) endif; helper context String def : notifyStanza(type : String) : String = ' notifyObservers("' + self.firstToUpper + '", ' + self.javaBoxing(type) + ');\n'; helper context String def : setterSubscribeBody(body : String, reflective : Boolean) : String = if reflective then self.reflectiveUnsubscribeStanza() else self.unsubscribeStanza() endif + '\n' + body + '\n' + self.subscribeStanza(); helper context String def : adderSubscribeBody(body : String) : String = body + '\n' + self.subscribeStanza(); helper context String def : removerSubscribeBody(body : String, reflective : Boolean) : String = if reflective then self.reflectiveRemoveUnsubscribeStanza() else self.removeUnsubscribeStanza() endif + '\n' + body; helper context String def : subscribeStanza() : String = 'if (' + self + ' != null) ' + self + '.addObserver(this);'; helper context String def : unsubscribeStanza() : String = 'if (this.' + self + ' != null) this.' + self + '.removeObserver(this);'; helper context String def : removeUnsubscribeStanza() : String = 'if (' + self + ' != null) ' + self + '.removeObserver(this);'; ------------------------------- Observer ----------------------------------- helper context String def : reflectiveUpdateBody() : String = 'if (' + self + ' instanceof java.util.Hashtable) {\n' + ' java.util.Hashtable e = (java.util.Hashtable) ' + self + ';\n' + ' String mName = "on" + ((String) e.get("name")) + "Change";\n' + ' Class[] parmTypes = { (Class) e.get("class") };\n' + ' try {\n' + ' java.lang.reflect.Method m = getClass().getDeclaredMethod(mName, parmTypes);\n' + ' Object[] args = { e.get("value") };\n' + ' m.invoke(this, args);\n' + ' } catch (NoSuchMethodException nex) {\n' + ' // no handler\n' + ' } catch (Exception ex) {\n' + ' // wrong handler configuration\n' + ' ex.printStackTrace();\n' + ' }\n' + '}'; helper context String def : reflectiveNotifyStanza(type : String) : String = ' setChanged();\n' + ' java.util.Hashtable e = new java.util.Hashtable();\n' + if self = self.javaBoxing(type) then ' e.put("name", "' + self.firstToUpper + '");\n' + ' e.put("class", ' + type.javaBoxedType() + '.class);\n' + ' if (' + self + ' != null) {\n' + ' e.put("value", ' + self + ');\n' + ' }\n' else ' e.put("name", "' + self.firstToUpper + '");\n' + ' e.put("class", ' + type.javaBoxedType() + '.class);\n' + ' e.put("value", ' + self.javaBoxing(type) + ');\n' endif + ' notifyObservers(e);'; helper context String def : reflectiveUnsubscribeStanza() : String = 'if (this.' + self + ' != null) this.' + self + '.deleteObserver(this);'; helper context String def : reflectiveRemoveUnsubscribeStanza() : String = 'if (' + self + ' != null) ' + self + '.deleteObserver(this);'; ------------------------------- Singleton ----------------------------------- helper context String def : singletonBody() : String = 'if (instance == null) {\n' + ' instance = new ' + self + '();\n' + '}\n' + 'return instance;'; ------------------------------- Applet ----------------------------------- helper context Boolean def : appletInitBody() : String = if self then 'instance = this;\n' + '\t\tsetLayout(new java.awt.BorderLayout());' else 'setLayout(new java.awt.BorderLayout());' endif; helper context Boolean def : midletInitBody() : String = if self then 'instance = this;' else '' endif; helper context Boolean def : swtAppInitBody() : String = self.midletInitBody(); helper context String def : appletInfoBody() : String = 'return "' + self + '";'; helper context String def : parameterInfoBody() : String = 'String pinfo[][] = ' + self + ';\nreturn pinfo;'; helper context String def : swtAppMainBody() : String = 'try {\n' + self + ' app = new ' + self + '();\ncatch (Throwable t) {\nt.printStackTrace();\n}'; ------------------------------- AbstractFactory ----------------------------------- helper context String def : createBody() : String = 'return new ' + self + '();'; ------------------------------- AsyncMethods ----------------------------------- helper context String def : asynchronousBody() : String = 'new Thread() {\n public void run() {\n' + self + '\n }\n}.start();';