Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In java, i thought about using annotation processing. The apt tool scans the source file before actually parsing the source file to the javac command.</p> <p>During compilation of the source files, the output will be printed out:</p> <pre><code>@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface MyInterface { int offset() default 0; int last() default 100; } </code></pre> <p>The processor factory:</p> <pre><code>public class MyInterfaceAnnotationProcessorFactory implements AnnotationProcessorFactory { public Collection&lt;String&gt; supportedOptions() { System.err.println("Called supportedOptions............................."); return Collections.EMPTY_LIST; } public Collection&lt;String&gt; supportedAnnotationTypes() { System.err.println("Called supportedAnnotationTypes..........................."); return Collections.singletonList("practiceproject.MyInterface"); } public AnnotationProcessor getProcessorFor(Set&lt;AnnotationTypeDeclaration&gt; set, AnnotationProcessorEnvironment ape) { System.err.println("Called getProcessorFor................"); if (set.isEmpty()) { return AnnotationProcessors.NO_OP; } return new MyInterfaceAnnotationProcessor(ape); } } </code></pre> <p>The actual annotation processor:</p> <pre><code>public class MyInterfaceAnnotationProcessor implements AnnotationProcessor { private AnnotationProcessorEnvironment ape; private AnnotationTypeDeclaration atd; public MyInterfaceAnnotationProcessor(AnnotationProcessorEnvironment ape) { this.ape = ape; atd = (AnnotationTypeDeclaration) ape.getTypeDeclaration("practiceproject.MyInterface"); } public void process() { Collection&lt;Declaration&gt; decls = ape.getDeclarationsAnnotatedWith(atd); for (Declaration dec : decls) { processDeclaration(dec); } } private void processDeclaration(Declaration d) { Collection&lt;AnnotationMirror&gt; ams = d.getAnnotationMirrors(); for (AnnotationMirror am : ams) { if (am.getAnnotationType().getDeclaration().equals(atd)) { Map&lt;AnnotationTypeElementDeclaration, AnnotationValue&gt; values = am.getElementValues(); int offset = 0; int last = 100; for (Map.Entry&lt;AnnotationTypeElementDeclaration, AnnotationValue&gt; entry : values.entrySet()) { AnnotationTypeElementDeclaration ated = entry.getKey(); AnnotationValue v = entry.getValue(); String name = ated.getSimpleName(); if (name.equals("offset")) { offset = ((Integer) v.getValue()).intValue(); } else if (name.equals("last")) { last = ((Integer) v.getValue()).intValue(); } } //find the sum System.err.println("Sum: " + ((last + 1 - offset) / 2) * (2 * offset + (last - offset))); } } } } </code></pre> <p>Then we create a source file. simple class that uses MyInterface annotation:</p> <pre><code> @MyInterface(offset = 1, last = 1000) public class Main { @MyInterface void doNothing() { System.out.println("Doing nothing"); } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Main m = new Main(); m.doNothing(); MyInterface my = (MyInterface) m.getClass().getAnnotation(MyInterface.class); System.out.println("offset: " + my.offset()); System.out.println("Last: " + my.last()); } } </code></pre> <p>The annotation processor is compiled into a jar file, then the apt tool is used to compile the source file as:</p> <pre><code>apt -cp "D:\Variance project\PracticeProject\dist\practiceproject.jar" -factory practiceproject.annotprocess.MyInterfaceAnnotationProcessorFactory "D:\Variance project\PracticeProject2\src\practiceproject2\Main.java" </code></pre> <p>The output of the project:</p> <pre><code>Called supportedAnnotationTypes........................... Called getProcessorFor................ Sum: 5000 Sum: 500500 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload