1 12 13 package org.eclipse.jdt.internal.compiler.apt.dispatch; 14 15 import java.util.Iterator ; 16 import java.util.Set ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 20 import javax.annotation.processing.Processor; 21 import javax.lang.model.SourceVersion; 22 import javax.lang.model.element.TypeElement; 23 24 37 public class ProcessorInfo { 38 final Processor _processor; 39 final Set <String > _supportedOptions; 40 final SourceVersion _supportedSourceVersion; 41 42 private final Pattern _supportedAnnotationTypesPattern; 43 private final boolean _supportsStar; 44 private boolean _hasBeenCalled; 45 46 52 public ProcessorInfo(Processor p) 53 { 54 _processor = p; 55 _hasBeenCalled = false; 56 _supportedSourceVersion = p.getSupportedSourceVersion(); 57 _supportedOptions = p.getSupportedOptions(); 58 Set <String > supportedAnnotationTypes = p.getSupportedAnnotationTypes(); 59 60 boolean supportsStar = false; 61 if (null != supportedAnnotationTypes && !supportedAnnotationTypes.isEmpty()) { 62 StringBuilder regex = new StringBuilder (); 63 Iterator <String > iName = supportedAnnotationTypes.iterator(); 64 while (true) { 65 String name = iName.next(); 66 supportsStar |= "*".equals(name); String escapedName1 = name.replace(".", "\\."); String escapedName2 = escapedName1.replace("*", ".*"); regex.append(escapedName2); 70 if (!iName.hasNext()) { 71 break; 72 } 73 regex.append('|'); 74 } 75 _supportedAnnotationTypesPattern = Pattern.compile(regex.toString()); 76 } 77 else { 78 _supportedAnnotationTypesPattern = null; 79 } 80 _supportsStar = supportsStar; 81 } 82 83 96 public boolean computeSupportedAnnotations(Set <TypeElement> annotations, Set <TypeElement> result) 97 { 98 if (null != annotations && !annotations.isEmpty() && null != _supportedAnnotationTypesPattern) { 99 for (TypeElement annotation : annotations) { 100 Matcher matcher = _supportedAnnotationTypesPattern.matcher(annotation.getQualifiedName().toString()); 101 if (matcher.matches()) { 102 result.add(annotation); 103 } 104 } 105 } 106 boolean call = _hasBeenCalled || _supportsStar || !result.isEmpty(); 107 _hasBeenCalled |= call; 108 return call; 109 } 110 111 114 public boolean supportsStar() 115 { 116 return _supportsStar; 117 } 118 119 125 public void reset() 126 { 127 _hasBeenCalled = false; 128 } 129 130 @Override 131 public int hashCode() { 132 return _processor.getClass().hashCode(); 133 } 134 135 @Override 136 public boolean equals(Object obj) { 137 if (this == obj) 138 return true; 139 if (obj == null) 140 return false; 141 if (getClass() != obj.getClass()) 142 return false; 143 final ProcessorInfo other = (ProcessorInfo) obj; 144 if (!_processor.getClass().equals(other._processor.getClass())) 145 return false; 146 return true; 147 } 148 149 @Override 150 public String toString() 151 { 152 return _processor.getClass().getName(); 153 } 154 155 159 public String getSupportedAnnotationTypesAsString() 160 { 161 StringBuilder sb = new StringBuilder (); 162 sb.append('['); 163 Iterator <String > iAnnots = _processor.getSupportedAnnotationTypes().iterator(); 164 boolean hasNext = iAnnots.hasNext(); 165 while (hasNext) { 166 sb.append(iAnnots.next()); 167 hasNext = iAnnots.hasNext(); 168 if (hasNext) { 169 sb.append(','); 170 } 171 } 172 sb.append(']'); 173 return sb.toString(); 174 } 175 } 176 177 | Popular Tags |