Question

So I have had an issue for a while now and thought it was worth the time to ask the more experienced regex guys if there was a way to fix this issue with a quick search and replace.

So i use a tool which generates java code(not written in java or I would manually fix the cause directly), however, it has an issue calling variables before an object is created.

This always occurs only once per object, but not for every object, the object name is unknown, and the error is always the line directly before the constructor is called. This is the format the error is always in:

this.unknownObjectName.mirror = true;
this.unknownObjectName = new Model(unknown, parameter, values);

I know there should be a trick to fix this, as a simple string replace simply will not work since 'unknownObjectName' is unknown.

Would this even be possible with regex, if so, please enlighten me :)

This is how the code SHOULD read:

this.unknownObjectName = new Model(unknown, parameter, values);
this.unknownObjectName.mirror = true;

For complex models, this error may happen hundreds of times, so this will indeed save a lot of time. That and I would rather walk on hot coals then do mindless busy work like fixing all these manually :)

Edit: I through together a java app that does the job.

public static void main(String args[]){
    File file = new File(args[0]);
    File file2 = new File(file.getParentFile(), "fixed-" + file.getName());
    try {
        if(file2.exists()) {
            file2 = new File(file.getParentFile(), "fixed-" + System.currentTimeMillis() + "-" + file.getName());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
        String line, savedline = null, lastInitVar = "";
        while((line = br.readLine()) != null){
            if(line.contains("= new ")){
                String varname = line.substring(0, line.indexOf("=")).trim();
                lastInitVar = varname;
            }else if(line.contains(".mirror")){
                String varname = line.substring(0, line.indexOf(".mirror")).trim();
                if(!lastInitVar.equals(varname)){
                    savedline = line;
                    continue;
                }
            }else if(savedline != null && savedline.contains(lastInitVar)){
                bw.write(savedline + "\n");
                savedline  = null;
            }
            bw.write(line + "\n");
        }
        bw.flush();
        bw.close();
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Was it helpful?

Solution

Over thinking it

Write a program to read line by line and when you see a object access before a constructor don't write it out, write out the next line and then write out the buffered line, rinse repeat.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski

Regular Expressions are for matching patterns not state based logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top