Posts Tagged java_programming

Java-Quiz: The Iterator Quiz

Danilo sent us an interesting Java-Quiz from the Java Specialists’ Newsletter created by Olivier Croisier. You have to insert your corrective code in place of the //FIXME comment, following these instructions:

  1. Do not modify his existing code, it’s Perfect (of course).
  2. The FIXME tag shows where you’re allowed to insert your corrective code
  3. He must be able to understand your solution when he comes back (so using Reflection is not an option).

Can you find a solution for it?

final List<String> list = new ArrayList() {{ 
final Iterator<String> iterator = list.iterator();
// FIXME : work here while I'm sunbathing

Later I’ll post my attempt to solve it.

Tags: , , ,

Effect propagation to code

Reading Michael Feathers’ ‘Working Effectively With LegacyCode’, I found quite interesting his heuristics to trace propagation of effects to code:

  1. Identify a method that will change.
  2. If the method has a return value, look at its callers.
  3. See if the method modifies any values. If it does, look at the method that use those values, and the methods that use those methods.
  4. Make sure you look for superclasses and subclasses that might be users of these instance variables and methods also.
  5. Look at parameters to the methods. See if they or any objects that their methods return are used by the code that you want to change.
  6. Look for global variables and static data that is modified in any of the methods you’ve identified.

Tags: , , , ,

JFileContentManager included in the Softpedia Mac OS software database

100% FREE award granted by Softpedia

I’m proud to announce that JFileContentManager, a software of mine, has been added to Softpedia’s database of software programs for Mac OS. It is featured with a description text, screenshots, download links and technical details on this page.

JFileContentManager has been tested in the Softpedia labs using several industry-leading security solutions and found to be completely clean of adware/spyware components. We are impressed with the quality of your product and encourage you to keep these high standards in the future.

You can see the announce by clicking on the image above. Thanks Softpedia for the award!

Tags: , ,

Generating LGPL notices with Python support

When we started developing JFileContentManager, we didn’t even think to release it as an open-source project. So, when we finished the project, I particularly thought about this idea, because I was very interested in participating on an community involving project. So, we decided to release it under the LGPL licence, because we wanted the project audience to be wider as possible. But, before turning it public, we needed to add the LGPL license term to every Java file of the project.
Opening and editing each Java class, one by one, would be a tedious task, taking a lot of time and adding up to the fact I’m very interested on learning dynamic languages (like Python, Ruby), I developed a Python script to automate this task for us. Our Python code is intended to open each Java file, adding the license term on the start of the file as a Java comment and writing it to the disk. The script is as follows:

 1:# Python script to add the LGPL notices to each java file of the FileContentManager project.
 2:import os, glob, sys
 3:License = """\
 5:*FileContentManager is a Java based file manager desktop application, 
 6:*it can show, edit and manipulate the content of the files archived inside a zip.
 8:*Copyright (C) 2008 
10:*Created by Camila Sanchez [], Rafael Naufal [] 
11:and Rodrigo [[email protected]]
13:*FileContentManager is free software; you can redistribute it and/or
14:*modify it under the terms of the GNU Lesser General Public
15:*License as published by the Free Software Foundation; either
16:*version 2.1 of the License, or (at your option) any later version.
18:*This library is distributed in the hope that it will be useful,
19:*but WITHOUT ANY WARRANTY; without even the implied warranty of
21:*Lesser General Public License for more details.
23:*You should have received a copy of the GNU Lesser General Public
24:*License along with FileContentManager; if not, write to the Free Software
25:*Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """
27:size = len(sys.argv)
28:if size == 1 or size > 2:
29:    print "Usage: $1"
30:    sys.exit(1)
31:inputPath = sys.argv[1]
32:if not os.path.exists(inputPath):
33:    print inputPath, "does not exist on disk"
34:    sys.exit(1)
35:if not os.path.isdir(inputPath):
36:    print inputPath, "isn't a dir"
37:    sys.exit(1)   
38:for path, dirs, files in os.walk(inputPath):
39:    fileWithLicense = ''
40:    for filepath in [ os.path.join(path, f)
41:            for f in files if f.endswith(".java")]:
42:        content = file(filepath).read()
43:        f = file(filepath, "w")
44:        print >>f, License + "\n" + content
45:        f.close()

License is a Python multiline string, denoted by the triple-quotes. The main action begins with the os.walk() expression that walks every file in the directory, entered by the user as a comannd line argument, like this:

python ${path}

where ${path} defines user project root directory. The list comprehension syntax produces the full path of each of the Java files. When the match is found, the file is opened and its content is assigned to the variable content. So, the License string is added to the start of the file and the file is then written to the disk. The print statements use a redirection syntax, for example: print >>f, License + "\n" + content. The ‘>>f’ sends the results to f rather than the console. So, this Python script helped our team to release the project early and at the same time automated the task of adding the LGPL notices. I think Python is very useful for these kind of tasks. And you?

Tags: , , , ,

Annoucing JFileContentManager 1.2!

A new version of JFileContentManager has been released! Below are the changes release:

  • The tree UI component was corrected to reflect the zip hierarchy content;
  • Added support to show the contents of:
    • Java files
    • Properties files
    • Shell script files
    • XHTML / HTML files
    • XML files
  • The About Dialog has been integrated with JDIC;

Check this out!

Tags: , , ,

JFileContentManager has been released!

JFileContentManager is a Java based file manager desktop application, it can show, edit and manipulate the content of the files archived inside a zip.
Showing a simple and friendly GUI, the user can see the content archived inside a zip file in a tree view way, select one file per time and see their content (either text or image). It’s also possible to reopen the last opened zip files through the history option on the file menu.

Below are the current features:

  • See the zip files’ list in a tree view way;
  • Select one by one of the files and see their content (either text or image);
  • Open the last four opened zip files through the history option on the file menu and
  • Download the bundled executable Jar.

and future goals:

  • Support other file formats, like .tar, .tgz, .tar.gz, .gzip, .rar, .7z.
  • Edit the content of a text file;
  • Save the edition etiher inside the opened zip or at another location on the disk;
  • Select one or more files and zip them together;
  • Edit the image’s file content with the help of a picture toolbar and
  • Bundle the project in Java Web Start.

The project is released under the LGPL license. Misfit, rnaufal and roddy will be very proud and happy if you take a look at this project, running the application, downloading the source code on SVN and commenting about it. Thanks and enjoy!

Tags: , , ,

TestSuite Merlin javadoc

Running through the Java TestSuite API I’ve found this funny javadoc..

Tags: , ,

Inspect your Java codebase with SemmleCode

This Eclipse plugin seems to be a nice tool to drill down into your company codebase. You can gather with it code conventions, metrics, styles, method name patterns, all of it using a simple object-oriented query language, classe .QL.

This is a short description of what SemmleCode can do gor you to manage your code conventions (from TheServerSide):

SemmleCode works by storing Eclipse projects in a relational database. You can then run queries to compute metrics, to find defects, to check style rules, and to navigate. For almost any frequent task, whether it involves a quality audit or a change impact analysis, there is a ready-made query that you can launch via the run menu of Eclipse. In particular there are queries for Robert C. Martin’s package metrics, and for checking J2EE coding conventions.

And about .QL:

.QL is a simple object-oriented query language for writing queries over the codebase. It is very intuitive, andtightly integrated in Eclipse with syntax highlighting, auto-completion, and continuous checking. .QL supports overriding of methods, and that is often very convenient for tailoring checks or metrics to a particular application. SemmleCode puts you in the driving seat when it comes to quality audits: when youthink of a new check, it usually takes only a few lines of .QL to implement it, and share it with other team members.

The plugin suggests us it can be a good tool to analyze our code conventions. I haven’t tried it out yet (I’ll share my thougths here when I do that 🙂 ), if you do, post your comments here.


Integrating Struts and Spring

My coworker Max has argued with me there is no relationship between Java web based frameworks, specially Struts and Spring. Well, I completelly disagreed with him when he raised this question and here I will post one of the possibilities of their integration. This alternative let you configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a Spring context file. Here are the steps to apply this configuration:

Add the following XML snippet to the plug-ins section near the bottom of your struts-config.xml file:

<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”/>

Now you can configure your actions to be injected by Spring. One way you can do this is use the DelegatingActionProxy class in the type attribute of your <action-mapping>.

This way of configuration allow you to manage your Actions and their dependencies in the action-context.xml file. The relationship between the Action in struts-config.xml and action-servlet.xml is established by the action-mapping’s
“path” and the bean’s “name”. If you have the following in your struts-config.xml file:

<action path=”/employees” …/>

You must define that Action’s bean with the “/users” name in action-servlet.xml:

<bean name=”/employees” …/>

Defining your Action in a context file let you to use Spring’s IoC features, as well as instantiate new Actions for each request created. To do this, add singleton=”false” to your action’s bean definition.

<bean name=”/employees” singleton=”false” autowire=”byName” class=”org.myapplication.web.EmployeeSearchAction”>

To be more concrete, here is a complete example of an Action described in struts-config.xml:

<action input=”/index.jsp” name=”mainForm” path=”/login” scope=”request” type=”org.springframework.web.struts.DelegatingActionProxy”> <forward name=”main_page” path=”/main_page.jsp”/> </action>

And here is its version in action-servlet.xml:

<bean id=”action.loginAction” singleton=”false” autowire=”byName” name=”/login” class=””/>

There are more alternatives to inject your actions in Spring. If you want to know the other ones, please check out the Spring reference manual.