How can I do a barebones version of a source file?

M

Miguel Farah

I need to create a barebones version of my Java source files.

What I mean when I say "barebones" is this: I have, for example, this class:

----8<--------8<--------8<--------8<--------8<--------8<--------8<----
package cl.cl.cl;

import cl.lc.*;
import java.util.*;

public class demo {

public String salute="Hello, World!";
private String somethingElse="";

public void SayHello() {
System.out.println(":::"+salute+":::");
// ... a lot of code ...
}

public int RealAge(Person p) {
// ... a lot of code ...
return theStuffIJustCalculated;
}

public Person[] Friends(Person p) {
// ... a lot of code ...
return theArray[];
}

}
----8<--------8<--------8<--------8<--------8<--------8<--------8<----

The barebones version would be:

----8<--------8<--------8<--------8<--------8<--------8<--------8<----
package cl.cl.cl;

import cl.lc.*;
import java.util.*;

public class demo {
public String salute="Hello, World!";
public void SayHello() { }
public int RealAge(Person p) { return 0; }
public Person[] Friends(Person p) { return null; }
}
----8<--------8<--------8<--------8<--------8<--------8<--------8<----

It would keep all the public/protected (not the private) attributes, methods,
etcetera, but only the declarations and no code inside the methods. The methods
that return something return a zero or a null value.

My intention is to be able to compile the entire set of source files, even if
there isn't any "meat" in them.

I wrote a Perl script, using pattern matching, but it's pretty crude and won't
handle properly stuff like more than one modifier for an attribute or a method,
or inner classes.

I'm wondering if this has been solved before. Do you know about a tool that does
what I'm trying to do?

Thanks in advance.
 
T

Thomas Schodt

Miguel said:
I need to create a barebones version of my Java source files.

What I mean when I say "barebones" is this: I have, for example, this class:
<trimmed>

More commonly known as a "stub".

I wrote a Perl script, using pattern matching, but it's pretty crude and won't
handle properly stuff like more than one modifier for an attribute or a method,
or inner classes.

I'm wondering if this has been solved before. Do you know about a tool that does
what I'm trying to do?

I'd say perl is probably the right way to go for generating stub source
from actual java source.

I wrote a small C application that makes stub source from class files,
not source I wish to publish tho.

Coding it was a valuable lessen in the structure of class files.
 
C

Cid

I need to create a barebones version of my Java source files.

javap -protected [class]

...would give you a nice start on it. That would give you abstract
style stubs for protected and public methods. You'd still have to take
another pass to provide dummy return values. Might save you some
parsing hassle though.
 
C

Chris Uppal

Miguel said:
I need to create a barebones version of my Java source files.

Personally, I'd use reflection. Compile the input file, load it into java, use
the features of java.lang.reflect.* to identify the members and generate new
source code from that.

There are, of course, other approaches. One could parse the .class file if you
are already familiar with a toolkit for doing that. You /might/ be able to use
a doclet of some sort.

You can, of course, use Perl or similar, but unless you implement a full Java
parser in Perl (which seems excessive) you are always going to be taking a
potentially-fragile shortcut.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top