// DecorationsPrintStack.java Copyright (c) 2004 Kari Laitinen // http://www.naturalprogramming.com // 2005-08-01 File DecorationsPrintMemory.java was created. // 2005-09-15 This file was created. // 2005-09-15 Last modification. /* This program is a modified version of Decorations.java. It shows how a so-called native method can be called from a Java program. The native method named print_stack() is a method written with the C programming language, and it is in file DecorationsPrintStack.c. The compiled C program is put into a library file named DecorationsPrintStack.dll and that library is loaded when the DecorationsPrintStack.class is loaded. At the end of this program there is a special construct that loads the .dll file. The methods of the original Decorations.java have been modified so that some extra variables have been added. You can see the values of the extra variables on the stack. You can print the stack contents to a file named DecorationsPrintStack_output.txt when you execute this program with the command java DecorationsPrintStack >DecorationsPrintStack_output.txt */ class DecorationsPrintStack { static boolean stack_has_been_printed = false ; static native void print_stack() ; // The C method is declared this way. static void multiprint_character( char character_from_caller, int number_of_times_to_repeat ) { int repetition_counter = 0 ; int extra_variable = 0x778899AA ; // The stack is printed only once, when multiprint_character() is // called for the first time. if ( stack_has_been_printed == false ) { print_stack() ; stack_has_been_printed = true ; } while ( repetition_counter < number_of_times_to_repeat ) { System.out.print( character_from_caller ) ; repetition_counter ++ ; } } static void print_text_in_decorated_box( String text_from_caller ) { int text_length = text_from_caller.length() ; int extra_variable = 0x33445566 ; System.out.print( "\n " ) ; multiprint_character( '=', text_length + 8 ) ; System.out.print( "\n " ) ; multiprint_character( '*', text_length + 8 ) ; System.out.print( "\n **" ) ; multiprint_character( ' ', text_length + 4 ) ; System.out.print( "**\n ** " + text_from_caller + " **\n **" ) ; multiprint_character( ' ', text_length + 4 ) ; System.out.print( "**\n " ) ; multiprint_character( '*', text_length + 8 ) ; System.out.print( "\n " ) ; multiprint_character( '=', text_length + 8 ) ; } public static void main( String[] not_in_use ) { String first_text = "Hello, world." ; print_text_in_decorated_box( first_text ) ; print_text_in_decorated_box( "I am a computer program written in Java." ) ; } // The construct below will load the DecorationsPringStack.dll // file from the current directory. static { System.loadLibrary( "DecorationsPrintStack" ) ; } }