Showing posts with label Developer. Show all posts
Showing posts with label Developer. Show all posts

2013-04-16

How to copy all files in classpath to a directory using Ant?

Try something like the following target:

...

    <target name=copy-deps">
        <copy todir="." flatten="true">
            <path refid="my-classpath"/>
        </copy>
    </target>

...

To execute (assuming that my-classpath is defined, of course):

ant copy-deps

2013-02-28

How to write the end-of-file character in a Java String?

Use the \u001a unicode character.

There is a character called EOF. It's also called control-Z.
If you want to include it in a string, you have to type it as "\u001a" as in:

String iHaveAnEof = "file ends here\u001a";

Credits:

Ed Staub

http://stackoverflow.com/a/7591594/129497

2012-04-13

Java vs Groovy on overriden method call

In Java, suppose we have the following class:

public class Do {
    public void doSomething(Object arg) {
        System.out.println("Do " + arg.toString());
    }
}

We then extend it in the following class:

public class DoDifferently extends Do {
    public void doSomething(Object arg) {
        System.out.println("Do different " + arg.toString());
    }
    public void doSomething(String arg) {
        System.out.println("Do different string " + arg.toString());
    }
}

Now let's use these classes in our program:

Do o = new Do();
o.doSomething("x");

executes the Object method.

DoDifferently o2 = new DoDifferently();
o2.doSomething("x");

executes the String method because it is more specific.

What about the following code?

Do o3 = new DoDifferently();
o3.doSomething("x");

It will execute the Object method, because Java uses the reference type to determine which method to call.

What about Groovy?

The code above is also valid in Groovy.

The String method is always executed when available, because Groovy uses the most specific method for the argument type, determined dynamically at run-time.

2012-03-07

Arduino

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.


I am following a work that will use Arduino for building a small robot. I look forward to sharing some of the experiences here on the blog.

2012-03-05

REST in Java

Most of my work with Web Services has been with SOAP-based Web Services. However, there is an alternative, called REST.

There is Java library for RESTful Web Services, called JAX-RS.

JAX-RS RESTful principles are:
- assign everything an ID
- link things together
- use common set of methods
- allow multiple representations
- stateless communications

Classes and methods are annotated to generate REST service.
Here is an example:

@Path("/atm")
public class AtmService {

    @GET @PATH("/balance")
    @Produces("text/plain")
    public String balance(@QueryParam("card") String card,
                          @QueryParam("pin") String pin) {

        ...
    }

...

There also annotations for using HTTP POST (@POST) and specifying the data type that is consumes (@Consumes).

This example was taken from a Java ONE presentation.

2012-03-02

Groovy properties

Groovy generates getters and setters for the public fields of a class.
These default methods can be overridden as usual.
If a field is final, then Groovy generates only the getter method.

Groovy provides a special operator, called the field operator .@, that bypasses the generated accessor methods.

2012-03-01

Groovy DSL

One of the selling points for Groovy is writing Domain Specific Languages (DSL).
I have to admit that I am not fully convinced. There are many limitations to the syntax.
The basic idea is that Nouns in the DSL are implemented as Objects and Verbs in the DSL as Functions.

Someday I would like to read the following books on DSLs:
Martin Fowler, 2010
Debasish Ghosh, 2010

2012-02-29

Groovy

I have been using Groovy for almost 2 years now, and it has been a great experience.

I have a preference for typed languages, but there is no doubt that scripting languages have enormous utility too. Groovy is very flexible in this aspect. You can program using types - in this case Groovy is just Java with some nice syntax sugar - or you can program without types - in this case Groovy is a scripting language with an incredible set of libraries: anything that runs on the Java Virtual Machine.

I read that the Groovy team is considering adding more features to allow better performance in the type-based programs. This is expected for the 2.0 release.

Even Java is becoming easier to use with the Project Coin language changes.
By the way, Groovy 1.9 will include JDK 7 support including project coin language changes and invokeDynamic to improve performance.

2011-11-03

Tackling a test in an infinite loop

I had a bug that was causing a unit test to go in an infinite loop. Gradle would not print any output from the test because the standard output stream is captured to a test report.

To debug I used the following two techniques:

1 - I accessed the actual standard output, bypassing Gradle's redirection for this specific situation (this is usually not a good idea):

    PrintStream actualStdout = new PrintStream(
        new FileOutputStream(FileDescriptor.out))
    actualStdout.println("test message")

2 - I placed a pause inside the loop that was not stopping to be able to read the messages and make sense of them in human timescale (1000 milliseconds = 1 second).

    Thread.sleep(1000)
 

Gradle flatDir repository

Q:
"
For a Gradle flatDir repository, is it looking for a local filesystem that is built up like Maven (pom's etc)..
or is it just looking for jar files in a folder called lib?
"

repositories {
flatDir name: 'localRepository', dirs: 'lib'
}


A:
"
As you assume it just looks for jars/wars/whatever in the 'lib' folder. It resolves versions by looking at the version number in the filename. for instance:
myveryownjar-1.2.0.jar
spring-2.5.6.jar
"

"
Pretty much. For each dependency that you declare in the build script, Gradle will look for a corresponding file in the 'lib' directory. For example, for a dependency on 'junit:junit:4.7', Gradle would look for
lib/junit-4.7.jar or lib/junit.jar. It assumes there are no transitive dependencies.
"

Adapted from http://gradle.1045684.n5.nabble.com/Flat-Dir-repositories-td1431519.html

Groovy and Gradle

The next generation of Java and Ant (+Maven): Groovy and Gradle.



There are still some improvements required but these tools represent major steps forward in programmer productivity.

2011-10-28

Java list tricks

Most of the times the simplest way to traverse a list in Java is to use an Iterator.

If your List is a LinkedList, then you can also use a descendingIterator.

For more list-specific methods, there is ListIterator, that can move forward and backward among other things.

2010-04-18

Java tools I had never used

...until recently.

javap - The Java Class File Disassembler - Converts class files back to java source code.

JConsole - monitoring tool that uses the extensive instrumentation of the Java Virtual Machine to provide information about the performance and resource consumption of applications running

2009-05-07

Tomcats

Sometimes, one "cat" is not enough... ;-)


I've just finished the support for creation multiple replicas of Apache Tomcat web servers.

It will be part of the next stage in the Distributed Systems course. More details will emerge soon, after the project requirements document is published.