Class: Jerbil::AptTask

Description

A task using Java‘s Annotation Processing Tool (apt) to compile (optional) and process annotations found in the class files.

This task can be used to gather a list of all EJB3 entities, or testng annotated classes during the compile step.

Example

 desc "compile all java files and find annotations"
 Jerbil::AptTask.new(:compile) do |t|
   t.java_files = Jerbil::JavaFileList.new("src", "classes")
   t.find_annotation 'javax.persistence.Entity' do |entities|
      File.open(ANNOTATED_CLASSES, 'w') { |f| f << entities.to_yaml }
   end
 end

Attributes

NameRead/write?Description
annotations RW A hash containing annotation types and handlers (code blocks).
nocompile RW Only process annotations, don‘t compile anything.

Public Class methods


new (name)

    # File lib/jerbil/apt_task.rb, line 30
30:     def initialize(name)
31:       @annotations = {}
32:       @nocompile = false
33:       @found_annotations = {}
34:       @found_annotations_handler = {}
35:       super
36:     end

Public Instance methods


annotated_classes_to_yaml (annotation, filename)

A convenience method to serialize a list of found annotations to file filename (yaml format).

    # File lib/jerbil/apt_task.rb, line 83
83:     def annotated_classes_to_yaml(annotation, filename)
84:       find_annotation annotation do |classes|
85:         File.open(filename, 'w') { |f| f << classes.to_a.uniq.to_yaml }
86:       end
87:     end

find_annotation (annotation, options={}, &handler)

Registers an annotation handler for all annotations with name annotation. Handlers (code blocks) will be invoked after successful compilation, in post_compile. See AptTask for an example.

    # File lib/jerbil/apt_task.rb, line 70
70:     def find_annotation(annotation, options={}, &handler)
71:       defopts = { :scope => :class }
72:       defopts.merge! options
73:       annotations[annotation] = Proc.new { |spec|
74:            t = spec.getDeclaringType || spec
75:            @found_annotations[annotation] ||= []
76:            @found_annotations[annotation] << t.toString
77:       }
78:       @found_annotations_handler[annotation] = handler
79:     end

process ()

    # File lib/jerbil/apt_task.rb, line 57
57:     def process  
58:       add_each(@env.getSpecifiedTypeDeclarations).each do |specType|
59:         begin
60:           check_type(specType)
61:         rescue
62:           puts $!
63:         end
64:       end   
65:     end

Protected Instance methods


check_type (type)

     # File lib/jerbil/apt_task.rb, line 104
104:     def check_type(type)
105:       enhance_type(type)
106:       annotations.each do | annotation, handler |
107:         if type.has_annotation?(annotation)
108:           handler.call(type)
109:         elsif type.is_classdecl?  
110:           methods = type.getMethods
111:           add_each(methods).each do |m|
112:             check_type(m)
113:           end      
114:         end
115:       end
116:     end

compile (parameters, printwriter)

     # File lib/jerbil/apt_task.rb, line 98
 98:     def compile(parameters, printwriter)
 99:       apt = Rjb::import('com.sun.tools.apt.Main')
100:       parameters << "-nocompile" if nocompile
101:       apt.process(get_processor_factory, printwriter, parameters )
102:     end

gather_filenames ()

    # File lib/jerbil/apt_task.rb, line 94
94:     def gather_filenames
95:       java_files
96:     end

needs_compiling? ()

    # File lib/jerbil/apt_task.rb, line 90
90:     def needs_compiling?
91:       true
92:     end

post_compile ()

Calls all registered handlers.

     # File lib/jerbil/apt_task.rb, line 119
119:     def post_compile
120:       super unless nocompile
121:       @found_annotations.each do |name, types|
122:         handler = @found_annotations_handler[name]        
123:         handler.call(types) unless handler.nil?
124:       end
125:     end