Translate Java methods to fields

2

So, recently, I found myself reformatting my code for Vitsy to using anonymous class instances for commands rather than the normal method approach. For those who don't know, anonymous class instance syntax looks something like this:

...
private abstract static class SomeClass {
    public abstract void someMethod();
}

public final SomeClass someField = new Command() {
    public void someMethod() {
        // Some implementation
    }
};
...

So what's your job, I hear you asking? Well, it's quite simple.

The Challenge

Given a class name, its inner method, and string of zero to many conventional methods, turn them into anonymous class instances, retaining the program instructions therein. For formatting purposes, I have labelled the input and output below for what must be considered (anything in <> needs to be retained in the output).

Conventional method syntax

<visibility modifier> void <method name>(){<implementation>}

Resulting instance declaration

<visibility modifier> final <class name> <method name>=new <class name>(){public void <inner method name>(){<implementation>}};

The Rules

  • You may not use any builtins for this challenge (if there are builtins for this, the lang creators have done a bad and they should feel bad).
  • You must retain the method name, the visibility modifier (public, private, (nothing), protected) and the implementation.
  • Do not worry about proper tabs/formatting. As long as it is Java compile-able, you're fine.
  • You may assume that all code inside of <implementation> is valid.
  • You may assume that there is nothing but methods in the string given.
  • Do not translate any methods that are not void.
  • Do not translate any methods with a parameter.
  • Input may be in any convenient format for your language.

Examples

>>> toAnonymousClass("ClassName", "innerMethod", "public void method1(){}")
public final ClassName method1=new ClassName(){public void innerMethod(){}};

>>> toAnonymousClass("ClassName2", "secondInnerMethod", "private void method2(){return;}protected int method3(){return 4;/*guaranteed to be random*/}")
private final ClassName2 method2=new ClassName2(){public void secondInnerMethod(){return;}};protected int method3(){return 4;/*guaranteed to be random*/}

Note here that the void method is translated with the visibility modifier (private) retained, but the int method is not translated as it has a different return type (int).

>>> toAnonymousClass("Clazz", "inMethod", "void method4(int param){}")
void method4(int param){}

method4 is not translated, as it accepts a parameter (int param).

>>> toAnonymousClass("Moar", "again", "void method5(){}")
final Moar method5=new Moar(){public void again(){}};

Another example of visibility modifier retainment - if it has no modifier, have no modifier on the output.

Finally, the big test case:

>>> toAnonymousClass("Hi", "rand", "private void method2(){return;}protected int method3(){return 4;/*guaranteed to be random*/}public void method1(){}void method4(int param){}void method5(){}")
private final Hi method2=new Hi(){public void rand(){return;}};protected int method3(){return 4;/*guaranteed to be random*/}public final Hi method1=new Hi(){public void rand(){}};void method4(int param){}final Hi method5=new Hi(){public void rand(){}};

Addison Crump

Posted 2016-03-14T19:38:20.797

Reputation: 10 763

Can the methods in input and anonymous class instances in output be separated by newlines? – daavko – 2016-03-14T21:53:52.710

@daavko Yes - "Do not worry about proper tabs/formatting. As long as it is Java compile-able, you're fine." – Addison Crump – 2016-03-14T21:55:14.410

One more test case to consider. Regex-based approaches would probably work for the other examples but fail in the general case: void foo(){String s = ""'}\}";}int bar(){return 42;}" – Ray – 2016-03-15T03:10:03.680

Answers

2

Retina, 95 bytes

Byte count assumes ISO 8859-1 encoding.

+`^((.+)¶(.+)¶(.+¶)*)(\w+ )?(void )(\w+)(..{.*})
$1$5final $2 $7=new $2(){public $6$3$8};
\w+¶

The code contains a trailing newline.

Takes input separated by newlines. Class name on the first line, inner method name on the second line, methods on following lines.
For example:

Hi
rand
private void method2(){return;}
protected int method3(){return 4;/*guaranteed to be random*/}
public void method1(){}
void method4(int param){}
void method5(){}

Try it online!

daavko

Posted 2016-03-14T19:38:20.797

Reputation: 824