I firmly believe the runtime system should provide enough metadata about the system itself so programs can make better decisions and complete tasks. Java has been able to introspect and reflect on classes for some time, but until now it has lacked the simple ability to map runtime code back to its position in the source code file. The pre-Java 1.4 solution was to manually parse an exception's stack trace. Now, with Java 1.4, we have a better solution. |
If we use a survey design where an answer to a question does not determine the next question, then Turbo Tax users who are single or married but not filing jointly would be quite frustrated. Additionally, when new laws and regulations cause the logic and data to change from year to year, Turbo Tax code would have to be modified. It would be more cost effective if such logic and data was independent and maintained separately from the codebase. This article shows you how to develop an application that decouples the surveying content and logic from the application. |
Java offers two types of clipboards: local and system. Local clipboards are only available inside the virtual machine that your applet or application is running. However, unlike some operating systems that limit you to only one clipboard, Java allows you to have as many local clipboards as you desire. Accessing a particular local clipboard is as easy as referring to it by name. |
In the following sections, we'll work with various JTable display options -- such as scroll bars, column widths, selection, and other attributes. We'll extend MyTable and incorporate various methods that will support the display features we want to change. Each section adds a new method to the MyTable class, so in the end, we'll have a totally reusable JTable. |
One of the memory conservation techniques Java programmers find useful is lazy instantiation. With lazy instantiation, a program refrains from creating certain resources until the resource is first needed -- freeing valuable memory space. In this tip, we examine lazy instantiation techniques in Java class loading and object creation, and the special considerations required for Singleton patterns. The material in this tip derives from the work in Chapter 9 of our book, Java in Practice: Design Styles & Idioms for Effective Java (see Resources). |
Since Java purposefully hides many aspects of memory management, discovering how much memory your objects consume takes some work. You could use the Runtime.freeMemory() method to measure heap size differences before and after several objects have been allocated. Several articles, such as Ramchander Varadarajan's "Question of the Week No. 107" (Sun Microsystems, September 2000) and Tony Sintes's "Memory Matters" (JavaWorld, December 2001), detail that idea. Unfortunately, the former article's solution fails because the implementation employs a wrong Runtime method, while the latter article's solution has its own imperfections: |
Since my trusty C++ compiler produced fast math code, I just needed to call these math routines instead of the slow StrictMath routines from my Java program. Java Native Interface (JNI) enables Java code to interface with native code written in other languages like C/C++. It enables you to write the bulk of your application in Java and still utilize highly optimized native code where necessary. And in the corporate computing world, JNI enables Java applications to connect to legacy code that might be difficult or impractical to port to Java. |
|
This Java Tip shows you how to use the Reflection API, and specifically how to use it to implement a simple system for sending dynamic messages. In our particular case, this simple system is the doesNotUnderstand message. |
With the PropDiff utility described here, not only can variants of large property files be compared with ease, but also the layering of property files can be used as an organizing principle that makes the early stages of implementation much smoother. |
As the number of tables that need to be populated increases, you'll need to write more code. But even this is only the case for a single database. If the data is ported to a different database system, you must rewrite all the existing code to account for the different data types in the new database. The issue of cross-database data typing has earned well-deserved notoriety in database programming. |
Java's support of interfaces provides a mechanism by which we can get the equivalent of callbacks. The trick is to define a simple interface that declares the method we wish to be invoked. |
For a server-based solution, the Synchronizer Token pattern (from Core J2EE Patterns) can be applied, which requires minimal contribution from the client side. The basic idea is to set a token in a session variable before returning a transactional page to the client. This page carries the token inside a hidden field. Upon submission, request processing first tests for the presence of a valid token in the request parameter by comparing it with the one registered in the session. If the token is valid, processing can continue normally, otherwise an alternate course of action is taken. After testing, the token resets to null to prevent subsequent submissions until a new token is saved in the session, which must be done at the appropriate time based on the desired application flow of control. In other words, the one-time privilege to submit data is given to one specific instance of a view. This Synchronizer Token pattern is used in the Apache Jakarta Project's Struts framework, the popular open source Model-View-Controller implementation. |
Notice that nothing strange has been done with the data shown in Listing 2. There are no strange encoding schemes, and the destination, or action, is a simple Java? servlet. A form submitted in this way simply sends the XML instance document as the contents of a POST request. |
The TilesDecorationFilter provides auto-configuration of tiles definitions. To use it, simply configure your filter to intercept any request to which you?d like to provide a standard look and feel. The filter will intercept the request, create a new templating definition that includes the standard set of tiles, configure the content page dynamically based on the original request, and render the definition. In many cases, the use of the decoration filter reduces the amount of XML configuration from one tiles definition per unique request to a single definition. This single definition then is applied to all requests. |