epubjs
Version:
Render ePub documents in the browser, across many devices
33 lines (32 loc) • 3.47 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Serialization Versus Code Generation</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Serialization Versus Code Generation"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-22-SECT-8"/>Serialization Versus Code Generation</h1></div></div></div><p><a id="I_indexterm22_id822757" class="indexterm"/>If you’ve been keeping an eye on the NetBeans source window
while we’ve been working, you may have noticed the code that is being
generated when you modify properties of beans. By default, NetBeans
generates method calls to set values on beans in the <a id="I_indexterm22_id822770" class="indexterm"/><code class="literal">initComponents()</code> method.
For example, if you set the value of one of your <code class="literal">NumericField</code> beans to <code class="literal">42</code> in the Properties pane, this value gets
hardcoded into the application as an initial value of the bean by a call
to <a id="I_indexterm22_id822793" class="indexterm"/><code class="literal">setValue()</code> on the bean
object at initialization time. But if you click on the Code button in the
Properties pane, you’ll see that we have another option. This area holds
properties that govern how NetBeans generates the application code. By
changing the Code Generation property from Generate Code to Serialize, you
change NetBeans’ behavior. Instead of generating method calls in the
source code, it saves your fully configured bean as a serialized object
and then generates the appropriate code to load the freeze-dried bean into
the application from a file.</p><p>Try changing the code generation property for a <code class="literal">Juggler</code> bean to Serialize. Switching to the
source code view and looking at the <code class="literal">initComponents()</code> method, you’ll see a line for
that bean that uses the static <code class="literal">Beans.instantiate()</code> method to load a serialized
copy of the bean.</p><p>NetBeans treats the serialized bean file as part of the source code
and will regenerate it whenever it saves the source file. In order to run
this example, we must first perform a manual build. Select Build <span class="unicode">→</span> Build Main Project, then you can run the file as
before with the Run File context menu. The reason for the explicit build
is to prompt NetBeans to copy the serialized bean file from the source
folder of your source file over to the compiled classes directory (it
should be smart enough to do this itself). You should see the serialized
bean file, named something like:
<span class="emphasis"><em>LearnJava1_juggler1.ser</em></span> alongside your source file
(and deployed to the <span class="emphasis"><em>classes</em></span> directory). You can run
the example and confirm that it behaves exactly like the code-generated
version. (This is pretty neat if you think about it.)</p><p>We’ll discuss working with serialized beans in more detail later in
this chapter and ask you to refer to this stored bean file.</p></div></body></html>