2005-09-02

 

The link to the outer class



2002-12-27 The Java Specialists' Newsletter [Issue 062] - The link to the outer class
Author: Dr. Heinz M. KabutzJDK version:
Category: Language
If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page. You can subscribe either via email or RSS.
Welcome to the 62nd edition of The Java(tm) Specialists' Newsletter sent to 5450 Java Specialists in 91 countries.
Here I am, sitting in my shorts on a beautiful starry night on our balcony, listening to the Guinea Fowls in our trees, trying to churn out yet another newsletter to appeal to your cerebral impulses. Guinea Fowls make a very strange sound, which is extremely annoying to some, and music to others. Fortunately for me (and the Guinea Fowls), I like their squawking. From the sunburnt-southern-hemispherer to all eggnog-drinking-northern-hemispherers: Relax, the days are getting longer again :-)
Don't you just love those Out-Of-Office messages? Boy, am I going to get a lot of those with this newsletter...
2003 - Europe Design Patterns Tour - please look at my previous newsletter.
The link to the outer class
Consider the following classes: public abstract class Insect {
public Insect() {
System.out.println("Inside Insect() Constructor");
printDetails();
}
public void printDetails() {
System.out.println("Just an insect");
}
}
public class Beetle extends Insect {
private final int legs;
public Beetle(int legs) {
System.out.println("Inside Beetle() Constructor");
this.legs = legs;
}
public void printDetails() {
System.out.println("The beetle has " + legs + " legs");
if (legs < 6) {
System.out.println("Ouch");
}
}
}
public class BeetleTest {
public static void main(String[] args) {
Beetle sad_bug = new Beetle(5); // lost one leg in an
// argument with his wife
Beetle happy_bug = new Beetle(6); // the wife bug ;-)
}
}
Stop for a moment and think of what the effect would be of running BeetleTest. Don't read further until you have decided what would happen.
I hope you didn't peep :-) Here is the output: Inside Insect() Constructor
The beetle has 0 legs
Ouch
Inside Beetle() Constructor
Inside Insect() Constructor
The beetle has 0 legs
Ouch
Inside Beetle() Constructor
Yes, even though legs was final, we were able to access it before it was initialised. What is more, we are able to call the subclass' methods from the constructor of the superclass, before the subclass had been initialised! This should come as no surprise to you, since by being subscribed to The Java(tm) Specialists' Newsletter you would be classed as a Java Specialist. Yes? No ... ?
But, the plot thickens. Look at the following class: public class NestedBug {
private Integer wings = new Integer(2);
public NestedBug() {
new ComplexBug();
}
private class ComplexBug extends Insect {
public void printDetails() {
System.out.println(wings);
}
}
public static void main(String[] arguments) {
new NestedBug();
}
}
When we run this code, we get a NullPointerException: Inside Insect() Constructor
java.lang.NullPointerException
at NestedBug.access$0(NestedBug.java:2)
at NestedBug$ComplexBug.printDetails(NestedBug.java:8)
at Insect.(Insect.java:4)
at NestedBug$ComplexBug.(NestedBug.java:6)
at NestedBug.(NestedBug.java:4)
at NestedBug.main(NestedBug.java:12)
Exception in thread "main"
A friend of mine once had this problem, so my first thought was that somehow, because wings was null, we ended up with a NullPointerException when printing it. That explanation did not make sense, because calling toString() on a null pointer is supposed to just return null. My friend changed his code to the following: public class NestedBug2 {
private Integer wings = new Integer(2);
public NestedBug2() {
new ComplexBug();
}
private class ComplexBug extends Insect {
public void printDetails() {
if (wings != null) { // line 8
System.out.println(wings);
}
}
}
public static void main(String[] arguments) {
new NestedBug2();
}
}
Sadly, this does not make the NullPointerException go away: Inside Insect() Constructor
java.lang.NullPointerException
at NestedBug2.access$0(NestedBug2.java:2)
at NestedBug2$ComplexBug.printDetails(NestedBug2.java:8)
at Insect.(Insect.java:4)
at NestedBug2$ComplexBug.(NestedBug2.java:6)
at NestedBug2.(NestedBug2.java:4)
at NestedBug2.main(NestedBug2.java:14)
Exception in thread "main"
But wait! The line with the NullPointerException is the line that simply checks whether wings is null!? Can the mere act of checking whether wings is null itself cause a NullPointerException? In this case it appears that it can! Or is the actual mistake on line 2? What is this access$0 method?
To understand this strange behaviour, we have to understand what the constructor of the inner class consists of. The easiest way of doing this is to use JAD and decompile the class file with the -noinner option: jad -noinner NestedBug2$ComplexBug.class
class NestedBug2$ComplexBug extends Insect {
NestedBug2$ComplexBug(NestedBug2 nestedbug2) {
this$0 = nestedbug2;
}
public void printDetails() {
if (NestedBug2.access$0(this$0) != null)
System.out.println(NestedBug2.access$0(this$0));
}
private final NestedBug2 this$0; /* synthetic field */
}
We also need to look at NestedBug2.class: jad -noinner NestedBug2.class
public class NestedBug2 {
public NestedBug2() {
wings = new Integer(2);
new NestedBug2$ComplexBug(this);
}
public static void main(String arguments[]) {
new NestedBug2();
}
static Integer access$0(NestedBug2 nestedbug2) {
return nestedbug2.wings;
}
private Integer wings;
}
Again, I must urge you to spend a few minutes thinking about this. Step through what would happen in your head or scribble on a piece of paper, but try to understand. When printDetails() is called by Insect>, the handle to outer class (this$0) has not yet been set in the ComplexBug class, so that class passes null to the access$0 method, which of course causes a NullPointerException when it tries to access nestedbug2.wings as we would expect.
I bet that many developers have run into this problem, but being under typical time pressure would have just coded around it until they got it working, without taking the time to think about what is happening.
This experience leads us to some questions:
Is this$0 a new type of keyword you did not know about?
What happens when your inner class already contains a variable this$0?
Is calling a method from a constructor a good idea in the first place?
When, if ever, are inner classes a good idea?
I will leave you with these questions to answer yourself over the festive season. For those of you who are actually working, I hope this newsletter has cheered up your workday and that you will be motivated to seek new nuggets of information inside the fascinating Java language.
All the best for the new year. May 2003 bring you new opportunities to learn and grow, not just in the material world :-)
Heinz
Copyright 2000-2005 Maximum Solutions, South AfricaReprint Rights. Copyright subsists in all the material included in this email, but you may freely share the entire email with anyone you feel may be interested, and you may reprint excerpts both online and offline provided that you acknowledge the source as follows: This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions for more information.

 

native2ascii - Native-to-ASCII Converter


native2ascii - Native-to-ASCII Converter

Converts a file with native-encoded characters (characters which are non-Latin 1 and non-Unicode) to one with Unicode-encoded characters.
SYNOPSIS
native2ascii [options] [inputfile [outputfile]]
DESCRIPTION
The Java compiler and other Java tools can only process files which contain Latin-1 and/or Unicode-encoded (\udddd notation) characters. native2ascii converts files which contain other character encodings into files containing Latin-1 and/or Unicode-encoded charaters.
If outputfile is omitted, standard output is used for output. If, in addition, inputfile is omitted, standard input is used for input.
OPTIONS
-reverse
Perform the reverse operation: convert a file with Latin-1 and/or Unicode encoded characters to one with native-encoded characters.
-encoding encoding_name
Specify the encoding name which is used by the conversion procedure. The default encoding is taken from System property file.encoding. The encoding_name string must be taken from the first column of the table of supported encodings in the Supported Encodings document.
-Joption
Pass option to the Java virtual machine, where option is one of the options described on the reference page for the java application launcher. For example, -J-Xms48m sets the startup memory to 48 megabytes.


2005-09-01

 

JMeter behaviour in Single Computer



JMeter behaviour in Single Computer
Number of Threads : 5

Ramp-Up Period (in seconds): 50

Loop Count : 3

Users :1,2,3,4

The requests will be sent out in following order:

1,1,1

[10 sec break = 50/5]

2,2,2

[10 sec break= 50/5]

3,3,3

[10 sec break= 50/5]

4,4,4

[10 sec break= 50/5]

1,1,1


 

JMeter Command Mode



http://www.bonoy.com/jmeter/usermanual/get-started.html#override

For non-interactive testing, you may choose to run JMeter without the GUI.

To do so, use the following command options

-n This specifies JMeter is to run in non-gui mode

-t [name of JMX file that contains the Test Plan].

-l [name of JTL file to log sample results to].

-r Run all remote servers specified in jmeter.properties (or remote servers specified on command line by overriding properties)

The script also lets you specify the optional firewall/proxy server information:

-H [proxy server hostname or ip address]

-P [proxy server port]

Example: jmeter -n -t my_test.jmx -l log.jtl -H my.proxy.server -P 8000


2005-08-31

 

[测试] 关于JMeter 压力测试工具




05-04-23 05:21作者︰ CyberCorlin
比LoadRunner ,Rational Robot 简单多了:) 主页http://jakarta.apache.org/jmeter/测试您的 DB2 数据库: 用 JMeter 测量性能http://www-128.ibm.com/developerworks/cn/db2/library/techarticles/0303bhogal/0303bhogal.html使用JMeter进行性能测试http://www-128.ibm.com/developerworks/cn/java/l-jmeter/?ca=dwcn-newsletter-java利用 Apache JMeter 测试 WebSphere 性能http://www-128.ibm.com/developerworks/cn/opensource/os-jmeter/?ca=dwcn-newsletter-opensourceUsing JMeter from OnJava.Comhttp://www.onjava.com/pub/a/onjava/2003/01/15/jmeter.htmlJMeter Ant Task (自动测试?)http://www.programmerplanet.org/ant-jmeter/Load Testing with Apache JMeter http://www.devx.com/webdev/Article/17950 HPjmeterhttp://www.hp.com/products1/unix/java/hpjmeter/超越性能测试网站(推荐)http://www.perftestplus.com/http://www.perftestplus.com/pubs.htm 文章http://www.perftestplus.com/approach.htm 优化路径http://www.perftestplus.com/articles/uenm1.pdf User Experience, not Metrics Series (推荐,这就是性能优化的真谛)这张图画的很棒哦。。。。别忘了看哦。http://www.perftestplus.com/Index_Files/newmeth.gif
Load Testing your Applications with Apache JMeterhttp://javaboutique.internet.com/tutorials/JMeter/DevX的文章写的都比较细,可惜界面太乱了:(

2005-08-30

 

Cannot display Chinese in Oracle Report Builder (PDF Generate)


  1. Copy ming_uni.ttf to C:\WINDOWS\Fonts

  2. Edit C:\DevSuiteHome\tools\common90\uifont.ali
    Add following three lines
    [ PDF:Subset ]
    "細明體" = "ming_uni.ttf"
    "新細明體" = "ming_uni.ttf"

  3. Add C:\WINDOWS\Fonts; in
    HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME0\REPORTS_PATH


*note: ming_uni.ttf can be changed to arialuni.ttf


This page is powered by Blogger. Isn't yours?