3
\$\begingroup\$

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(){}};
\$\endgroup\$
3
  • \$\begingroup\$ Can the methods in input and anonymous class instances in output be separated by newlines? \$\endgroup\$
    – daavko
    Mar 14, 2016 at 21:53
  • \$\begingroup\$ @daavko Yes - "Do not worry about proper tabs/formatting. As long as it is Java compile-able, you're fine." \$\endgroup\$ Mar 14, 2016 at 21:55
  • \$\begingroup\$ 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;}" \$\endgroup\$
    – Ray
    Mar 15, 2016 at 3:10

1 Answer 1

3
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.