Tuesday, February 7, 2012

Annotation with annotation element annotating itself

So... this works...
@Awesomeness(level = 100, item = @Awesomeness.Thing("Ice cream!"))
@interface Awesomeness {

   int level() default 42;

   Thing item() default @Thing("Hamburger!");

   @interface Thing {
      String value() default "Bacon!";
   }
 
}
Now if I can only do something useful with this...

And yes, the compiler does detect cycles. Awesome!!

Friday, July 29, 2011

Compile-time constants in Java annotation element values

In hindsight, this should've been obvious:
public class CompileTimeConstantsTest {
 
    @interface Awesome { String value(); }
 
    private static final String X = "X";
    private final int a = 1;
    public final int b = 1;

    @Awesome(a < b ? X : X + X) // compiles just fine
    int whatever;
 
}

Friday, June 24, 2011

Add menu accelerator for FindBugs plugin

One way to run FindBugs is to:
  1. Right click on the Java source file you’re editing
  2. Move cursor and hover over "Find Bugs"
  3. Wait until child menu appears
  4. Move cursor to select and then click on "Find Bugs"
You can greatly accelerate this process by modifying eclipse/plugins/edu.umd.cs.findbugs.plugin.eclipse_1.3.9.20090821/plugin.xml:
REPLACE:
   label="Find Bugs"

WITH:
   label="Find Bu&amp;gs"
Then restart Eclipse with -clean command line argument. Now, to run FindBugs, you can just:
  1. Press Menu key
  2. Press G (this didn’t use to have an accelerator, but now does)
  3. Press F (this already has an accelerator)
Similarly, you can clear all FindBugs markers by pressing the key sequence Menu G C.

Monday, February 14, 2011

Pascal's triangle using regex

The pattern can probably be simplified, but this works.
String s;
System.out.println(s = "-;");
for (int n = 10; n --> 0 ;) {
   System.out.println(s = s.replaceAll("^(?=(-;))|;(?=(-*;))", "$1$2"));
}

Wednesday, February 9, 2011

Per-site user stylesheet rules for Firefox

I'll summarize this later:

userContent.css and @-moz-document
https://developer.mozilla.org/en/CSS/@-moz-document

Where's my profile directory?
about:support

You'll probably want to use !important.
http://www.w3.org/TR/CSS2/cascade.html#important-rules

Thursday, February 3, 2011

Generating Collatz sequence using regex

See: Collatz conjecture on Wikipedia

final int N = 11;
String s = new String(new char[N]);

while (s.length() > 1) {
    System.out.print(s.length() + ", ");
    s = s.replaceAll("(.+)\\1$|((.)+)", "$1$2$2$2$3");
}
System.out.println(s.length());

// prints 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

Solving the postage stamp problem using regex

See: Postage stamp problem on Wikipedia
int n = 0;

while (
   (new String(new char[++n]))
      .matches("(.{1}|.{4}|.{9}|.{31}|.{51}){0,5}")
);

System.out.println(n); // 127
(see also on ideone.com)

That is, when the only available stamp denominations are {1, 4, 9, 31, 51}, and when envelopes can only accommodate up to 5 stamps, the smallest unrepresentable total stamp value is 127.

It's easy to confirm correctness, because (not so) coincidentally, this set of stamp denominations is the unique solution to the (5, 5) global PSP. See Al Zimmermann's Programming Contest - Son of Darts.