001/**************************************************************************** 002 * Copyright/Copyleft: 003 * 004 * For this source the LGPL Lesser General Public License, 005 * published by the Free Software Foundation is valid. 006 * It means: 007 * 1) You can use this source without any restriction for any desired purpose. 008 * 2) You can redistribute copies of this source to everybody. 009 * 3) Every user of this source, also the user of redistribute copies 010 * with or without payment, must accept this license for further using. 011 * 4) But the LPGL is not appropriate for a whole software product, 012 * if this source is only a part of them. It means, the user 013 * must publish this part of source, 014 * but don't need to publish the whole source of the own product. 015 * 5) You can study and modify (improve) this source 016 * for own using or for redistribution, but you have to license the 017 * modified sources likewise under this LGPL Lesser General Public License. 018 * You mustn't delete this Copyright/Copyleft inscription in this source file. 019 * 020 * @author Hartmut Schorrig: hartmut.schorrig@vishia.de, www.vishia.org 021 * @version 0.93 2011-01-05 (year-month-day) 022 *******************************************************************************/ 023package org.vishia.java2C.test; 024 025import org.vishia.bridgeC.AllocInBlock; 026import org.vishia.msgDispatch.LogMessageFile; 027 028 029/**This class contains some examples to demonstrate and test all features of Java2C. 030 * The elements all explained respecting the Java2C-features which are tested there. 031 * <br> 032 * <br> 033 * Most of the methods demonstrates a programming style, which is opportune also in C. 034 * <br> 035 * The class is represent in C wit a struct type definition: 036 * <pre> 037 * typedef struct TestAllConcepts_Test_t 038 * { 039 * union { ObjectJc object; ImplIfc_Test_s super;} base; 040 * int32 simpleInt; 041 * ...etc. 042 * } 043 * </pre> 044 * The first element of struct is a union, which pools the super class, all interfaces and the ObjectJc-base. 045 * The most inner super class has ObjectJc as its first Element, the access to that base of all super classes 046 * can accessed immediately in this unit. 047 */ 048public class TestAllConcepts //extends ImplIfc 049{ 050 /**A simple class variable. In C it's a member of the class struct: 051 * <pre> 052 * int32 simpleInt; 053 * </pre> 054 * The type <code>int32</code> is defined in os_types_def.h platform-depending, so that a 32-bit-integer 055 * is used. 056 * <br> 057 * The initialization with <code>25</code> is done in the Constructor, see {@link #TestAllConcepts()} 058 */ 059 private int simpleInt = 25; 060 061 062 /**A total constant value, it is a #define kMsgBlockHeap 9000 in C. 063 * This is the base of message ident numbers for output Garbage collection activity. */ 064 static final int kMsgBlockHeap = 9000; 065 066 static final int kIdxMsgOutputFile = 2; 067 068 /**A final constant not-int-value, it is a const variable in C. */ 069 static final char kCharConst = (char)(3); 070 071 /**A static, but not final variable. It should be intitalized outside the constructor, 072 * but in C it is simple implemented like <code>int32 nrofInstances_TestAllConcepts = 5;</code> 073 * It may be better, to write in a common <code>initializeStatics()</code>-Method, to regard more complex expressions.*/ 074 static int nrofInstances = 5; 075 076 /**In Java it is a fix build instance at construction time. In C it is an embedded instance. */ 077 final SimpleDataStruct embeddedData = new SimpleDataStruct(); 078 079 /**In Java it is a fix build instance at construction time. In C it is also an embedded instance. 080 * The expandable class isn't expand here because it is final. */ 081 final SimpleDataStruct embeddedDataNotEnpand = new SimpleDataStruct(); 082 083 /**In Java it is a fix build instance at construction time. In C it is also an embedded instance. 084 * The expandable class isn't expand here because it is final. */ 085 final ExpandedDataStruct embeddedDataExpand = new ExpandedDataStruct(); 086 087 final TestWaitNotify.WaitNotifyData testWaitNotifyData = new TestWaitNotify.WaitNotifyData(); 088 089 final TestThread testThread = new TestThread(testWaitNotifyData); 090 091 final TestWaitNotify testWaitNotify = new TestWaitNotify(testWaitNotifyData); 092 093 final StringBuffer stringBufferMain = new StringBuffer(20000); 094 095 final TestString testString = new TestString(stringBufferMain); 096 097 final TestStringFormatter testStringFormatter = new TestStringFormatter(); 098 099 /**A initialized array, it is embedded because it's final. */ 100 private final int[] intArray = new int[1000]; 101 102 /**A initialized array, but it is not embedded because it isn't final. */ 103 private int[] intArrayRef = new int[234]; 104 105 /**A simple reference to an array. @java2c=noGC. */ 106 private int[] intArrayRef2; 107 108 /**A initialized array, it is embedded because it's final. @java2c=simpleArray.*/ 109 private final int[] intArraySimple = new int[1000]; 110 111 /**A initialized array, but it is not embedded because it isn't final. @java2c=simpleArray.*/ 112 private int[] intArrayRefSimple = new int[234]; 113 114 static final int[] intArrayStaticConst = {10, 11, 12}; 115 116 /**@java2c = embeddedArrayElements. */ 117 SimpleDataStruct[] dataArrayRef; 118 119 /**@java2c = embeddedArrayElements. */ 120 final SimpleDataStruct[] dataArrayEmbedded = new SimpleDataStruct[12]; 121 122 /**@java2c = embeddedArrayElements, simpleArray. */ 123 final SimpleDataStruct[] dataArraySimpleEmbedded = new SimpleDataStruct[12]; 124 125 /**@java2c = embeddedArrayElements. A array of embedded elements. */ 126 final SimpleDataStruct[] dataRefArrayEmbedded = new SimpleDataStruct[12]; 127 128 /**A array of references, but the head is embedded */ 129 final SimpleDataStruct[] dataRefArray = new SimpleDataStruct[12]; 130 131 /**A array of embedded elements, not final. */ 132 SimpleDataStruct[] dataAssociatedEmbeddedArray; 133 134 /**@java2c = embeddedArrayElements. A array of references, not final. */ 135 SimpleDataStruct[] dataAssociatedRefArray; 136 137 /**A initialized array, it is embedded because it's final. */ 138 //TODO private static final int[] intArrayStatic = new int[1000]; 139 140 /**A initialized array, but it is not embedded because it isn't final. */ 141 //TODO private static int[] intArrayRefStatic = new int[234]; 142 143 /**A initialized array, it is embedded because it's final. @java2c=simpleArray.*/ 144 //TODO private static final int[] simpleArrayStatic = new int[1000]; 145 146 /**A initialized array, but it is not embedded because it isn't final. @java2c=simpleArray.*/ 147 //TODO private static int[] simpleArrayRefStatic = new int[234]; 148 149 /**An instance which implements a interface, using to test interface access. */ 150 private final ImplIfc implifc = new ImplIfc(100); 151 152 private final Ifc2 ifc3 = new ImplIfc(123); 153 154 /**A second instance which implements a interface, using to test interface access. */ 155 private ImplIfc implifc2; 156 157 /**A class reference to an interface, set one time final. */ 158 private final Ifc ifc = implifc; 159 160 /**A class reference to an interface, 161 * but it is set to only one instance: 162 * Therefore the call of methods at C-level uses the methods of the implemtation, it doesn't use 163 * the dynamic call. 164 * @java2c=instanceType:"org.vishia.java2C.test.ImplIfc". 165 * */ 166 private final Ifc ifcNonVirtual; 167 168 169 170 /**A class reference to an interface, but set only if used. 171 * It may be referenced to one or another instance depend on fine algorithm. */ 172 private Ifc ifc2; 173 174 private final ExtendsImpl extendsImpl= new ExtendsImpl(); 175 176 /**A initialized but not final reference. */ 177 private ImplIfc ifc4 = new ImplIfc(555); 178 179 180 private final AnyClass anyRef; 181 182 private final TestAnonymous testAnonymous = new TestAnonymous(); 183 184 /**The Constructor of this example. 185 * Before calling the constructor (ctor), the memory space is provided and initialized. 186 * The initialization of the memory space contains: 187 * <ul> 188 * <li> Initialization of the ObjectJc-base. That base data are disposed at start of memory space always. 189 * It the class doesn't have a Object-base (special case of data classes possible, see {@link SimpleDataStruct}). 190 * than this initialization isn't happen. 191 * <li>All other bytes of the whole data spaces are set to 0. Therefore all references are <code>null</code>-references, 192 * and all values are default set to 0. 193 * </ul> 194 * This memory initialization is done calling a allocation in the Jc-Runtime environment. 195 * If the memory is a static one, the user should call <code>init_ObjectJc(object, sizeof(Instance), ident);</code> 196 * manually at C-level. Static instances can be defined at C-level before starting initialization 197 * in the C-typical wise. 198 * <br> 199 * The constructor starts in C-file with: 200 * <pre> 201 * struct TestAllConcepts_Test_t* ctorO_TestAllConcepts_Test(ObjectJc* othis, ThCxt* _thCxt) 202 * { TestAllConcepts_Test_s* ythis = (TestAllConcepts_Test_s*)othis; //upcasting to the real class. 203 * STACKTRC_TENTRY("ctorO_TestAllConcepts_Test"); 204 * checkConsistence_ObjectJc(othis, sizeof(TestAllConcepts_Test_s), null, _thCxt); 205 * setReflection_ObjectJc(othis, &reflection_TestAllConcepts_Test_s, sizeof(TestAllConcepts_Test_s)); 206 * </pre> 207 * <ul> 208 * <li>The reference to the instance is given as a <code>ObjectJc*</code>-Reference. This is, 209 * because the calling environment supplied only this base reference. This ObjectJc is initialized, 210 * especially the size of the memory area is set in <code>ObjectJc.objectIdentSize</code>. 211 * <li>The return value is the same reference, but typed to the instance, to set the reference 212 * to the initialized instance without casting. 213 * <li><code>ThCxt* _thCxt</code> is the reference of the threadContext. It is necessary for exception handling. 214 * <li>The first instruction is the casting to the real type. The casting is unchecked yet, but see next: 215 * <li>The <code>STACKTRC_TENTRY(...)</code> registers the method stack level in thread context. 216 * It is for exception handling with stack-trace dump. 217 * <li>The <code>checkConsistence_ObjectJc(...)</code> tests whether the memory area 218 * referenced with <code>ythis</code> is initialized properly. Especially the size of the area is tested. 219 * This routine calls an Exception if the conditions are not true. The conditions should be met any time, 220 * elsewhere a user software error is happen. After this test the continuation of initialize is safety. 221 * Without such test the software may be crash here causing of an external error. 222 * <li><code>setReflection(...)</code> completes the ObjectJc-Data with the only one special information 223 * regarding the implementation instance. The reflection include the method table too. It is a structured data 224 * defined at C-level in this compilation unit. 225 * </ul> 226 * The next action is the call of constructor of the super class. That constructor calls first the constructor of its superclass 227 * and so on. The constructor of Object is not called, because the initialization of memory 228 * before executing the constructor initializes the ObjectJc-base. There are contained some informations 229 * of memory organization. 230 * */ 231 public TestAllConcepts() 232 { implifc2 = new ImplIfc(200); 233 ifcNonVirtual = implifc2; //use the interface reference. 234 anyRef = new AnyClass(extendsImpl); 235 } 236 237 238 public int access(int x) 239 { 240 intArray[3] = simpleInt; 241 intArrayRef[2] = kMsgBlockHeap; 242 243 embeddedData.db = 3.14F; 244 embeddedDataExpand.xb = (short)x; 245 embeddedDataNotEnpand.db = 2.7; 246 247 248 249 simpleInt = intArrayStaticConst[1]; 250 intArrayRef2 = intArray; //cast from a special embedded array type to int_Y 251 intArrayRef2 = intArrayRef; 252 //TODO intArray[2] = intArrayStatic[4]; 253 254 { /**Test get new array with embedded array elements. */ 255 int maxQueue = 5; 256 /**@java2c = embeddedArrayElements. */ 257 SimpleDataStruct[] entries = new SimpleDataStruct[maxQueue]; 258 for(int idxEntry = 0; idxEntry < entries.length; idxEntry++) 259 { entries[idxEntry] = new SimpleDataStruct(); 260 } 261 dataArrayRef = entries; 262 } 263 return simpleInt; 264 } 265 266 267 /**This method shows some calls of interface methods, see {@link org.vishia.java2C.Docu.SuperClassesAndInterfaces#callingOverrideableMethods()}. 268 * @return 269 */ 270 private int checkSomeDynamicCalls() 271 { int a; 272 Ifc ifc3 = this.implifc; 273 ifc3.processIfcMethod(5); 274 /**Because the instance implifc is embedded and their type is known hence, 275 * the method is performed non-dynamically, but direct. */ 276 a = implifc.processIfcMethod(456); 277 /**A stack instance, data in stack. @java2c=stackInstance. */ 278 ImplIfc stackInstance = new ImplIfc(45); 279 /**Because the stackInstance is embedded and their type is known hence, 280 * the method is performed non-dynamically, but direct. */ 281 a += stackInstance.processIfcMethod(678); 282 283 /**call of own overrideable method. */ 284 //TODO, fails yet: processIfcMethod(4); 285 /**call of a method with a class reference: */ 286 ifc.processIfcMethod(56); 287 return a; 288 } 289 290 291 292 293 /**Example to show concatenated calls in a simple variant. Concatenated calls are a well used 294 * construct in Java, whereby there are two suggestions to do so: 295 * <ol> 296 * <li>References are needed, there are got calling a method. 297 * <br><br> 298 * In C such references are known too, 299 * but mostly they are got immediate writing <code>myInstance.ref->refOfRef->variable</code>. 300 * It is the adequate construct. The usage of methods instead the immediately getting of the reference 301 * is the concept of ObjectOrientation and Encapsulation: The access to the class data should be done 302 * always using get-methods. The advantage is: Any access check can be implemented in the get-method. 303 * The programming can be implemented more safety. But Lastly it's possible to present a get-method 304 * with a primitive macro in C. Than no extra method call is produced, the machine code is the same 305 * like the immediately access. 306 * <br><br> 307 * Sum up: The usage of get-methods instead immediately access to references is recommended. 308 * <br><br> 309 * This variant of usage is shown in a statement line 310 * <pre class="Java"> 311 * int a = 234 + implifc.returnAnyInstance().returnRef().returnAnyInstance().addValue(24) + 27; 312 * </pre> 313 * <li>calls to the same instance written in a nicely kind, which is associated with a concatenation of data. 314 * A typically example in Java is the here shown concatenation of append(...) for a StringBuffer, 315 * shown in a second statement line. 316 * <pre class="Java"> 317 * sbufferFix.append("Value=").append(simpleInt).append(" miles"); 318 * </pre> 319 * In comparison with C++, the usage of an C++-expression <code>cout << "Value=" << simpleInt << " miles"</code> 320 * phrases the same. The shift operator should phrase a association to "shift in pipe", 321 * but at syntactical level it is an concatenation like shown above. 322 * <br><br> 323 * </ol> 324 * Both application types of concatenation calls should be translated to C in a well readable form. 325 * The translated result of both lines is: 326 * <pre> 327 AnyClass_Test_s* _tempRef1; ImplIfcTest_s* _tempRef2; AnyClass_Test_s* _tempRef3; 328 a = 234 + 329 ( _tempRef1= returnAnyInstance_ImplIfcTest(& (ythis->implifc), _thCxt) 330 , _tempRef2= returnRef_AnyClass_Test(_tempRef1, _thCxt) 331 , _tempRef3= returnAnyInstance_ImplIfcTest(_tempRef2, _thCxt) 332 , addValue_AnyClass_Test(_tempRef3, 24, _thCxt) 333 ) + 27; 334 335 ( append_s_StringBufferJc(& (ythis->sbufferFix.sb), s0_StringJc("Value="), _thCxt) 336 , append_i_StringBufferJc(& (ythis->sbufferFix.sb), ythis->simpleInt, _thCxt) 337 , append_s_StringBufferJc(& (ythis->sbufferFix.sb), s0_StringJc(" miles"), _thCxt) 338 ); 339 * </pre> 340 * In the first concatenation example: The references from each call are parked in temporary variables. 341 * That is because the result from the first call is the first parameter of the next call. 342 * If temporary variables are not used, the first call may be placed as first parameter of the second call etc. 343 * This construct may be lesser able to read, it is a nesting of calls. There is another problem thereby: 344 * If method tables are necessary for dynamic calls, the reference is needed twice. Therefore a explicitly 345 * temporary reference is proper. 346 * <br><br> 347 * In the second concatenation example: The references are the same in any call because the methods contain 348 * <pre class="Java"> 349 * return this; 350 * </pre> 351 * in its implementation. That is labeled at the methods. The translator knows that therefore and can optimize: 352 * All methods use the same reference. The concatenation in Java is translated to a simple order of routine call. 353 * <br><br> 354 * In both examples a comma-separated expression is used. It is a non-often used but possible construct in C. 355 * The first examples shows the necessity of that construct: The concatenation is a part of an comprehensive expression, 356 * and the calling of the concatenated routines should be done in the correct order. In the second example 357 * the comma-separation may be unnecessary, but it's possible and don't may be confusingly. 358 * The one-line-expression in Java is broken in several lines at position of the concatenation-representing comma, 359 * otherwise the line in C would be to long. This form is better to read. 360 * 361 * @return a value. 362 */ 363 private final int checkConcatenationSimple() 364 { 365 int a = 234 + implifc.returnAnyInstance().returnRef().returnAnyInstance().addValue(24) + 27; 366 //sbufferFix.append("Value=").append(simpleInt).append(" miles"); 367 368 { /**This line is a test whether a mix of return this and return association works. See Java- and C-code.*/ 369 a +=implifc.returnAnyInstance().returnThis(56).returnRef().testImplIfc(); 370 } 371 return a; 372 } 373 374 375 /**Example to show concatenated calls of override-able methods. There are some kinds. 376 * The first line in Java 377 * <pre class="Java"> 378 * a = 234 + implifc2.returnAnyInstanceOverrideable().returnRefOverrideable().returnAnyInstanceOverrideable().addValueOverrideable(24) + 27; 379 * </pre> 380 * is related to the first line in {@link #checkConcatenationSimple()}, but the difference is: 381 * all methods are dynamically called. It is a simple non-prepared dynamic call, 382 * therefore the code in C may be look catastrophic for real time. 383 * The method table of the references have to be got. It is a call of <code>getMtbl_ObjectJc(...)</code> 384 * with the given reference as first parameter. But the calculation time is not so expensive, 385 * it may be a few nanoseconds if the derivation is not deep, and some more nanoseconds if it is deeper, 386 * but not more. Only for hard real-time it is non-opportune. The lines in C are: 387 * <pre> 388 AnyClass_Test_s* _tempRef1; ImplIfcTest_s* _tempRef2; AnyClass_Test_s* _tempRef3; 389 a = 234 + 390 ( _tempRef1= ((Mtbl_ImplIfcTest const*)getMtbl_ObjectJc(&(REFJc(ythis->implifc2))->base.object, sign_Mtbl_ImplIfcTest) )->returnAnyInstanceOverrideable(REFJc(ythis->implifc2), _thCxt) 391 , _tempRef2= ((Mtbl_AnyClass_Test const*)getMtbl_ObjectJc(&(_tempRef1)->base.object, sign_Mtbl_AnyClass_Test) )->returnRefOverrideable(_tempRef1, _thCxt) 392 , _tempRef3= ((Mtbl_ImplIfcTest const*)getMtbl_ObjectJc(&(_tempRef2)->base.object, sign_Mtbl_ImplIfcTest) )->returnAnyInstanceOverrideable(_tempRef2, _thCxt) 393 , ((Mtbl_AnyClass_Test const*)getMtbl_ObjectJc(&(_tempRef3)->base.object, sign_Mtbl_AnyClass_Test) )->addValueOverrideable(_tempRef3, 24, _thCxt) 394 ) + 27; 395 * </pre> 396 * The second line in Java 397 * <pre class="Java"> 398 * a += this.returnThisOverrideable_Test(34).returnThisOverrideable_Test(45).access(56); 399 * </pre> 400 * shows a concatenated call of overrideable methods, which returns this and uses this as first reference. 401 * Because this is supported as method-table-reference if it is need, with one access to <code>getMtbl_ObjectJc(...)</code> 402 * in the method, and the <code>mthis</code> is used for all calls because the <code>return this</code>-property 403 * of the called method is known, this is a optimized access. In C it is: 404 * <pre> 405 Mtbl_TestAllConcepts_Test const* mtthis = (Mtbl_TestAllConcepts_Test const*)getMtbl_ObjectJc(&ythis->base.object, sign_Mtbl_TestAllConcepts_Test); 406 ... 407 a += 408 ( mtthis->returnThisOverrideable_Test(ythis, 34) 409 , mtthis->returnThisOverrideable_Test(ythis, 45) 410 , mtthis->access_i(ythis, 56, _thCxt) 411 ); 412 * </pre> 413 * whereby the first line is generated only one time in the method. 414 * <br> 415 * <br> 416 * The next line in Java shows a call of dynamic methods of referenced data. The methods returns <code>this</code>, 417 * why wet even the reference is the same for all three calls. The reference is copied into a stack variable, 418 * which is labeled with <code>@ java2c=dynamic-call</code>. Therefore in C a so named "method-table-reference" is built. 419 * This reference contains the pointer to the method table beside the pointer of data. The pointer to the method table 420 * is got calling <code>getMtbl_ObjectJc(...)</code> only on setting the reference. 421 * It's a part of the macro <code>SETMTBJc(...)</code>. So the C-code is optimized. The Java-lines are: 422 * <pre class="Java"> 423 * / **This reference is build in stack because it contains the method-table-reference too: @ java2c=dynamic-call. * / 424 * AnyClass anyRef2 = anyRef; 425 * anyRef2.returnThisOverrideable(45).returnThisOverrideable(234).addValueOverrideable(67); 426 * </pre> 427 * The translated C-code is: 428 * <pre> 429 SETMTBJc(anyRef2, REFJc(ythis->anyRef), AnyClass_Test); 430 a += 431 ( anyRef2.mtbl->returnThisOverrideable( (anyRef2.ref), 45, _thCxt) 432 , anyRef2.mtbl->returnThisOverrideable( (anyRef2.ref), 234, _thCxt) 433 , anyRef2.mtbl->addValueOverrideable( (anyRef2.ref), 67, _thCxt) 434 ); 435 * </pre> 436 * In opposite, if the special stack-local method-table-reference is not built, the C-code isn't optimize, 437 * but able to run (translated from next Java line using <code>anyRef</code> as class reference): 438 * <pre> 439 a += 440 ( ((Mtbl_AnyClass_Test const*)getMtbl_ObjectJc(&(REFJc(ythis->anyRef))->base.object, sign_Mtbl_AnyClass_Test) )->returnThisOverrideable(REFJc(ythis->anyRef), 345, _thCxt) 441 , ((Mtbl_AnyClass_Test const*)getMtbl_ObjectJc(&(REFJc(ythis->anyRef))->base.object, sign_Mtbl_AnyClass_Test) )->returnThisOverrideable(REFJc(ythis->anyRef), 3234, _thCxt) 442 , ((Mtbl_AnyClass_Test const*)getMtbl_ObjectJc(&(REFJc(ythis->anyRef))->base.object, sign_Mtbl_AnyClass_Test) )->addValueOverrideable(REFJc(ythis->anyRef), 367, _thCxt) 443 ); 444 * </pre> 445 * shows a call of a dynamic-linked method of a other instance, whereby the <code>return this</code>-property 446 * is known too, and the reference is prepared as a locally method-table-reference: 447 * <br><br> 448 * The conclusion: 449 * <ul> 450 * <li>If dynamic call is necessary, a optimization is possible. 451 * <li>Calling of methods of the own class, which return this, uses the one-time-prepared <code>mthis</code>. 452 * <li>Calling of methods of a referenced instance: The reference should be copied in a stack-local variable 453 * which is labeled with <code>@ java2c=dynamic-call</code> or it is an interface-reference. 454 * If the methods returns this, and they are labeled with <code>@ java2c=return-this</code>, 455 * the access is optimized. 456 * <li>If no such optimization is done, the method-table-pointer is got some more times, but the call is correct. 457 * </ul> 458 */ 459 private int checkConcatenationDynamicCall() 460 { 461 /**Checks how return-this-methods of the own class can concatenated: */ 462 int a; 463 a = 234 + implifc2.returnAnyInstanceOverrideable().returnRefOverrideable().returnAnyInstanceOverrideable().addValueOverrideable(24) + 27; 464 a += this.returnThisOverrideable_Test(34).returnThisOverrideable_Test(45).access(56); 465 466 /**This reference is build in stack because it contains the method-table-reference too: @java2c=dynamic-call. */ 467 AnyClass anyRef2 = anyRef; 468 a += anyRef2.returnThisOverrideable(45).returnThisOverrideable(234).addValueOverrideable(67); 469 470 /**Oposite: don't use the stacl-local reference, but the class variable, it isn't optimized in C, but able to run: */ 471 a += anyRef.returnThisOverrideable(345).returnThisOverrideable(3234).addValueOverrideable(367); 472 473 return a; 474 } 475 476 477 478 479 /**Example to test dynamic and static calls to methods, which are methods of the base class. 480 * The calling of base methods requires the appropriate types of pointers of this. 481 * @return 482 */ 483 private int checkConcatenationDynamicCallToBaseMethods() 484 { int a =5; 485 a =implifc.returnThisOverrideable(34).returnThisOverrideable(56).processIfcMethod(44); 486 487 return a; 488 } 489 490 /**Example to show concatenated calls with the special kind: The methods returns this itself. 491 * @deprecated 492 * In this case the calling instance is the same as the returned instance, which is the calling instance 493 * for the next concatenated call. 494 * In Java it is written: 495 * <pre class="Java"> 496 * a =implifc.returnThisA(34).returnThisA(56).processIfcMethod(44); 497 * </pre> 498 * Because the reference <code>implifc</code> is a embedded instance and the type of it is fix, 499 * (the same is for stack instances or @ <code>java2c=instanceType:"Type"</code>-designated references), 500 * all method-calls are execute non-dynamic. 501 * Because a <code>@ java2c=return-this</code> is designated to the called method, the Java2C-translator 502 * accept that the result instance is the same as calling instance, but because that is type-fix, 503 * the concatenated call is execute non-dynamic too. 504 * <pre> 505 * a = 506 * ( returnThisA_ImplIfcTest_F(& (ythis->implifc), 34, _thCxt) 507 * , returnThisA_ImplIfcTest_F(& (ythis->implifc), 56, _thCxt) 508 * , processIfcMethod_i_ImplIfcTest_F(&((& ((ythis->implifc).base.IfcToTest))->base.object), 44, _thCxt) 509 * ); 510 * </pre> 511 * The instance to call the method is <code>implifc</code> always, it's the first parameter of any called method. 512 * That is because the methods are labeled to <code>@ java2c=return-this</code>. The concatenation in Java is presented 513 * as a comma-separated expression, see {@link #checkConcatenationSimple()}. 514 * But in this case no temporary references are needed, it is more simple. Lastly the concatenated methods in Java 515 * are only a simplifying of writing of the method calls with the same reference. 516 * <br><br> 517 * Note: the expression <code>&((& ((ythis->implifc).base.IfcToTest))->base.object)</code> is needed 518 * because the <code>processIfcMethod_i_ImplIfcTest_F</code> is a method of the interface type. 519 */ 520 private int checkConcatCallReturnThisTypefixNonVirtual() 521 { int a; 522 ///**A stack instance, data in stack. @java2c=stackInstance. */ 523 //ImplIfc stackInstance = new ImplIfc(45); 524 a =implifc.returnThisOverrideable(34).returnThisOverrideable(56).processIfcMethod(44); 525 return a; 526 } 527 528 529 530 private int checkConcatCallReturnAnything() 531 { int a=0; 532 ///**A stack instance, data in stack. @java2c=stackInstance. */ 533 //ImplIfc stackInstance = new ImplIfc(45); 534 //a =implifc.returnAnyInstanceOverrideable(34).returnAnyInstanceOverrideable(56).processIfcMethod(44); 535 return a; 536 } 537 538 539 540 541 542 543 /**This method helps to test concatenations with return-this, but override-able. 544 * The return value is the same as the calling instance. This is recognized by Java2C, 545 * if <code>@ java2c=return-this</code> is written. 546 * <br> 547 * This is also a example for non-using of STACKTRC, it is not necessary here. 548 * @java2c=return-this, stacktrace:no. 549 * @param value any value 550 * @return this 551 */ 552 public TestAllConcepts returnThisOverrideable_Test(int value) 553 { 554 simpleInt += value; 555 return this; 556 } 557 558 559 560 /**Example for a non-dynamic call of an interface referenced method. 561 * Because the reference is marked with the instanceType, this information is used to produce 562 * a normal C-function call of the known instance method. The additional designation of the reference 563 * helps to economize calculation time, if the dynamic call isn't necessary really. 564 * <br> 565 * This method is used as an example for using STACKTRC, but without external parameter. 566 * @java2c=stacktrace:no-param. 567 * 568 */ 569 void checkNonVirtual() 570 { 571 /**This method should not use a dynamic call in C, because the reference is marked with the instanceType. */ 572 ifcNonVirtual.processIfcMethod(23); 573 } 574 575 576 577 /**Check whether Object.equals() will be overridden. 578 * @see java.lang.Object#equals(java.lang.Object) 579 */ 580 public boolean equals(Object cmp) 581 { return true; 582 } 583 584 585 /**This method doesn't override Object.equals. */ 586 public boolean equals(String cmp) 587 { return true; 588 } 589 590 591 /**A method with same name but other parameter types. 592 * @param x 593 */ 594 public void access(float x) 595 { 596 } 597 598 599 int testAccessIfc() 600 { int val = 0; 601 Ifc ifc22 = implifc; //stacklocal reference to type Ifc 602 if(ifc22 !=null){ //test usage ifc22.ref 603 for(int ii=0; ii<1000; ii++) 604 { val += ifc22.processIfcMethod(5); //use it for dynamic call, in C too. 605 } 606 } 607 return val; 608 } 609 610 611 int testAccessIfcMtbl() 612 { int val = ifc.processIfcMethod(5); 613 return val; 614 } 615 616 617 int testAccessIfcMtbl2(boolean bTest) 618 { int val; 619 if(bTest){ 620 ifc2 = implifc2; 621 } else { 622 ifc2 = implifc; 623 } 624 Ifc ifcCall = ifc2; 625 val = ifcCall.processIfcMethod(6); 626 val += ifc2.processIfcMethod(5); 627 return val; 628 } 629 630 631 /**Example to check how an dynamic call of own methods is implemented in C. 632 * The internal non-final method {@link #testAccessIfcMtbl()} is called twice: 633 * 634 */ 635 final void testInternalDynCall() 636 { boolean cond = true; 637 testAccessIfcMtbl(); 638 if(cond) 639 { testAccessIfcMtbl(); 640 } 641 } 642 643 644 /* 645 int testNewDerived() 646 { 647 /**Any instance-reference can be initialized with an derived class. * / 648 SimpleClass myClass = new SimpleClass(10) 649 { 650 @Override int addValue(int value) 651 { 652 x1 += 2*value; 653 return x1; 654 } 655 }; 656 return myClass.addValue(3); 657 } 658 */ 659 660 /**Example for a special finalize method body. finalize is called by garbage collection 661 * if the object is deleted finally. For Java2C in C all enhanced references will be deleted than. 662 * The finalize method is part of override-able methods of Object. The method will be placed 663 * in the method table. 664 * <br> 665 * The C-code of this method has the following form: 666 * <pre> 667 * TODO copy from C 668 * </pre> 669 */ 670 @Override public void finalize() 671 { 672 System.out.println("finalize"); 673 } 674 675 676 /** 677 * 678 */ 679 private void main() 680 { int ret = 0; 681 TestAllConcepts main = this; 682 TestgarbageCollector testGc = new TestgarbageCollector(); 683 testAnonymous.test(); 684 testGc.test(); 685 main.testInternalDynCall(); 686 main.checkConcatenationSimple(); 687 main.checkConcatenationDynamicCall(); 688 //main.checkConcatCallReturnThisTypefixNonVirtual(); 689 //main.checkConcatCallReturnThisVirtual(); 690 //main.checkConcatCallThisVirtual(); 691 main.access(234); 692 main.testString.testStringProcessing(); 693 main.testStringFormatter.test(); 694 /**Method from a super class which is only defined there. but called final. */ 695 extendsImpl.testImplIfc(); 696 /**Method from a super class which implements an interface method, but called final. */ 697 //TODO dynCall of baseclass method faulty. processIfcMethod(234); 698 ret += main.checkSomeDynamicCalls(); 699 ret += main.testAccessIfc(); 700 ret += main.testAccessIfcMtbl(); 701 ret += main.testAccessIfcMtbl2(true); 702 703 TestContainer testContainer = new TestContainer(); 704 testContainer.test(); 705 706 /**Start a two threads: */ 707 testWaitNotify.start(); 708 709 testThread.start(); 710 testThread.otherThreadRoutine(); 711 testWaitNotify.shouldRun = false; 712 713 try{ Thread.sleep(2000);} //wait for finishing notify-thread. 714 catch (InterruptedException e) {} 715 System.out.println("main finished."); 716 } 717 718 719 public static void main(String[] args) 720 { //MsgDispatcher msgDispatcher = new MsgDispatcher(10,100,100,8); 721 LogMessageFile msgOutputFile = new LogMessageFile("log/gc$MMMdd-hhmmssS$.log", 0, 0, null,null,null); //msgDispatcher.getSharedFreeEntries()); 722 //msgDispatcher.setOutputRoutine(kIdxMsgOutputFile, "log", true, msgOutputFile); 723 //msgDispatcher.setOutputRange(kMsgBlockHeap, kMsgBlockHeap+99, 1<<kIdxMsgOutputFile, MsgDispatcher.mSet, 4); 724 TestAllConcepts main1 = new TestAllConcepts(); 725 AllocInBlock.setRunModeAll(); 726 AllocInBlock.setLogMessageOutput(msgOutputFile, kMsgBlockHeap); 727 //main1.processIfcMethod(234); 728 main1.main(); 729 System.gc(); 730 main1.finalize(); //it isn't need anymore, but the gc hasn't freed it because it is in use still. 731 main1 = null; 732 System.gc(); 733 msgOutputFile.close(); 734 } 735 736 737}