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