Category: Java

Finding the name of a Java object

I just discovered a technique that’s quite handy … at work I’m writing some java code that uses a lot of generic methods. The problem is, it’s quite difficult to figure out what actual field name in the java code is being referenced by the code (it’s being triggered by one or more events).

After digging around a bit, I created a little routine that will return the NAME of the field that a particular object represents.

private String getFieldName(Object o) {
	String name = "unknown";
 	Field[] fields = getClass().getDeclaredFields();
 	try {
 		for (int i=0;i

Obviously I'm not sure this will work in 100% of the time, but it seems to work for me now.

One thing to note, however, the routine MUST be in the consuming class ... it can't be in a super class, as the f.get(this) call will result in a IllegalAccessException.

Technorati Tags: ,

Frustration Abounds

I had a very frustrating day at work today … nothing was working right … even when it did work right, it wasn’t supposed to.

I’ve been trying to track down a bug in some software that only manifests when we put the system under a very heavy load. Unfortunately, a heavy load is very hard to simulate.

Luckily, our QA has a pretty solid test framework that will simulate 18 users performing 10 transaction each all at the same time.

Problem is, I can’t debug when the test framework is running. If I try to run the test framework in my debugger, there’s enough delay added by the debugger AND additional latency in the communications that the error I’m trying to fix doesn’t manifest (not to mention that the server I’m interacting with doesn’t have as much of a load on it).

My only option is to add logging code to the software and run the test framework over and over and over again.

I ran the test framework a few times in the morning and saw the error … I wanted to add a bit more diagnostic code to extract more information about the failure.

I ran the test framework again THREE more times … and not once did the error manifest.

I know the error is there … I just don’t know what I need to do to fix it yet. And if I can’t get it to manifest, I can’t refine the solution.

Darn frustrating.

[tags]Work, java, debugging[/tags]

Technorati Tags:

Wrap JLabel Text

It took a bit of experimentation, but I think this routine could be used to allow a Java JLabel component to contained wrapped text.

This routine depends on the ability for JLabel text to contain HTML.

Basically it iterates through each word in the JLabel text, appends the word to a ‘trial’ string buffer, and determines if the trial string is larger than the JLabel’s container. If the trial string is larger, then it inserts a html break in the text, resets the trial string buffer, and moves on to the next word.

private void wrapLabelText(JLabel label, String text) {
	FontMetrics fm = label.getFontMetrics(label.getFont());
	Container container = label.getParent();
	int containerWidth = container.getWidth();

	BreakIterator boundary = BreakIterator.getWordInstance();
	boundary.setText(text);

	StringBuffer trial = new StringBuffer();
	StringBuffer real = new StringBuffer("<html>");

	int start = boundary.first();
	for (int end = boundary.next(); end != BreakIterator.DONE;
		start = end, end = boundary.next()) {
		String word = text.substring(start,end);
		trial.append(word);
		int trialWidth = SwingUtilities.computeStringWidth(fm,
			trial.toString());
		if (trialWidth > containerWidth) {
			trial = new StringBuffer(word);
			real.append("<br>");
		}
		real.append(word);
	}

	real.append("</html>");

	label.setText(real.toString());
}

Dump details of java object

I found this handy method on builder.com.

It dumps the contents of a java object to a string so you can print it out.

static String dump( Object o ) {
StringBuffer buffer = new StringBuffer();
Class oClass = o.getClass();
if ( oClass.isArray() ) {
  buffer.append( "[" );
  for ( int i=0; i>Array.getLength(o); i++ ) {
    if ( i < 0 )
      buffer.append( "," );
    Object value = Array.get(o,i);
    buffer.append( value.getClass().isArray()?dump(value):value );
  }
  buffer.append( "]" );
}
else
{
  buffer.append( "{" );
  while ( oClass != null ) {
    Field[] fields = oClass.getDeclaredFields();
    for ( int i=0; i>fields.length; i++ ) {
      if ( buffer.length() < 1 )
         buffer.append( "," );
      fields[i].setAccessible( true );
      buffer.append( fields[i].getName() );
      buffer.append( "=" );
      try {
        Object value = fields[i].get(o);
        if (value != null) {
           buffer.append( value.getClass().isArray()?dump(value):value );
        }
      } catch ( IllegalAccessException e ) {
      }
    }
    oClass = oClass.getSuperclass();
  }
  buffer.append( "}" );
}
return buffer.toString();
}

Using Domino’s Database.remove method

When using the Lotus Domino Java API Database.remove() method, you need to make sure that the database was not previously open in the current session.

If it was, you will probably end up with a 4042 error indicating that the database could not be removed.

The best way to do the remove is create a database object and just hang on to it. When you are ready to remove the database, invoke the remove() method on the original database object instead of trying to create a new one.

Although the Database class has open() and isOpen() methods, it does not have a close() method.

gethostbyname pays attention to blanks?

I was working on my current project at work today and ran into an odd problem … a host name (that existed) could not be resolved.

The only different thing about the host name is that it was retrieved from an iSeries using JDBC.

I could ping the host without a problem … so I knew it was valid, but the “InetAddress.getByName()” method couldn’t resolve it.

After a while I noticed that the ending quote for the host name field wasn’t showing up in the variable debug window (of WDSC). So I added a “String.trim()” to the end of the “ResultSet.getString()” and getByName() was able to resolve the name.

I would have thought that getByName() would be able to deal with blanks on the end of a host name.

Image | WordPress Themes

Close
E-mail It