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
025public class TestAnonymous {
026
027        int testValueOuter = 34;
028        
029        SimpleClass ref;
030        
031        /**An composite instance is able to access with the type {@link SimpleClass}, 
032         * but it is an enhanced instance of {@link ImplIfc}, which based in {@link SimpleClass}.   
033         * It is suitable to use 
034         * <ul>
035         * <li>if the overridden method bodies should access data of the outer class {@link TestAnonymous},
036         * <li>if the access to this methods are sufficient over the interface or base class.
037         * </ul> 
038         */
039        final SimpleClass ref2 = new ImplIfc(23)
040        {
041                @Override int addValue(int value) {
042                  x1 += testValueOuter * value;  ////access to an value of the outer class.
043            return x1;
044                }
045        };
046        
047        /**A static anonymous instantiated reference. 
048         * TODO: The static variant doesn't work yet (Version 0.92) because the instantiation isn't clarified.
049         */
050        /*static*/ SimpleClass staticAnonymous = new SimpleClass(23)
051        {
052                float disturbing;
053                float x1;   //covers the x1 of the base class.
054                @Override int addValue(int value) {
055                  disturbing = (float)Math.random() - 0.5F;
056                        x1 += value + disturbing;  //access to an value of the outer class.
057            super.x1 = (int)x1;
058                        return super.x1;
059                }
060        };
061        
062        void setRef(SimpleClass ref){
063                this.ref = ref;
064        }
065        
066        void test(){
067                int testvalue1 = 212;
068                //An anonymous class is instanciated here, but dynamically.
069                SimpleClass refTempSimpleClass = new SimpleClass(12){
070                        @Override
071      int addValue(int value)   //with this changed method addValue().
072                  { testValueOuter = value; 
073                    x1 += value;            
074                    return x1;
075                  }
076                };
077                refTempSimpleClass.addValue(43);
078                //test of a anonymous class definition inside the expression for a parameter of a method.
079                ///
080                setRef(
081                        new SimpleClass(34){        //The class SimpleClass is overridden:
082                                @Override
083        int addValue(int value)   //with this changed method addValue().
084                          { x1 -= value;            //other operation.
085                            x1 += testValueOuter;   ////access to an value of the outer class.
086                            //x1 += testvalue1;       //access to values of the method-body or statement-block is not possible.
087                            return x1;
088                          }
089                        }
090                );
091                for(int x=0; x<100; x++){
092                        ref.addValue(x);  //calls the method above....
093                        // */
094                        ref2.addValue(x);
095                        
096                        staticAnonymous.addValue(x);
097                }
098                System.out.println("TestAnonymous " + staticAnonymous.getValue());
099        }
100        
101        
102        final class TestInnerNonstatic
103        {
104                int xInnerNonstatic;
105                
106                
107                TestInnerNonstatic(){
108                  xInnerNonstatic = 234;
109                }
110                
111                final void testInnerNonstatic()
112                {
113                        testValueOuter = xInnerNonstatic;  //
114                }
115        }
116        
117        
118        final static class TestInnerStatic
119        {
120                int xInnerStatic;
121                
122                final void testInnerNonstatic(int input)
123                {
124                        xInnerStatic = input;
125                }
126        }
127        
128        
129}