Skip to content

Instantly share code, notes, and snippets.

@pcarrier
Last active August 29, 2015 14:04
Show Gist options
  • Save pcarrier/e7618f684b898fefbd6f to your computer and use it in GitHub Desktop.
Save pcarrier/e7618f684b898fefbd6f to your computer and use it in GitHub Desktop.
package com.example;
class Hello {
// Simple example (unknown field name, namespace "com/example/Hello", id "noop")
@Logged
native void noop(int a);
// Complex example (known field names, namespace "com/example", id "fooed")
@Logged(id="fooed", namespace="com/example", fields=["from", "to"])
void foo(@LogFormat(As.HASHCODE) Bar source, @LogFormat(As.IDENTITY) Baz dest);
}
package java.lang.annotation;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface LogFormat {
As value;
public enum As {
STRING, // default, uses Object#toString()
HASHCODE, // uses Object#hashCode()
IDENTITY, // uses System.identityHashCode()
STRUCT, // uses recursive representation of public fields
IGNORED, // completely skipped
}
}
package java.lang.annotation;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Logged {
// defaults to method name, eg logMyEvent
String id() default null;
// defaults to class descriptor, eg com/example/Foo
String namespace() default null;
// name of each field; defaults to undefined
String[] fields[];
}
Supports primitive types & strings, LogFormat.As for other classes.
package java.lang.annotation;
// Pool it, t
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD})
public @interface Pooled {
Boolean sharedPrefix() default false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment