1 12 package org.eclipse.jdt.apt.core.internal.util; 13 14 import java.io.IOException ; 15 16 import static org.eclipse.jdt.apt.core.internal.util.AnnotationScanner.State.*; 17 18 21 public abstract class AnnotationScanner { 22 23 enum State { 24 NORMAL, 25 SEEN_SLASH, 26 IN_COMMENT, 27 IN_COMMENT_SEEN_STAR, 28 IN_SINGLE_LINE_COMMENT, 29 IN_SINGLE_QUOTE, 30 IN_DOUBLE_QUOTE 31 } 32 33 public AnnotationScanner() {} 34 35 public boolean containsAnnotations() throws IOException { 36 State state = NORMAL; 37 38 boolean seenBackslash = false; 41 42 int c = getNext(); 43 while (c != -1) { 44 45 if (seenBackslash) { 46 seenBackslash = false; 48 } 49 else if (c == '\\') { 50 seenBackslash = true; 52 } 53 else { 54 switch (state) { 56 57 case NORMAL : 58 if (c == '@') 59 return true; 60 if (c == '/') { 61 state = SEEN_SLASH; 62 } 63 else if (c == '\'') { 64 state = IN_SINGLE_QUOTE; 65 } 66 else if (c == '\"') { 67 state = IN_DOUBLE_QUOTE; 68 } 69 break; 70 71 case SEEN_SLASH : 72 if (c == '*') { 73 state = IN_COMMENT; 74 } 75 else if (c == '/') { 76 state = IN_SINGLE_LINE_COMMENT; 77 } 78 else { 79 state = NORMAL; 80 } 81 break; 82 83 case IN_COMMENT : 84 if (c == '*') { 85 state = IN_COMMENT_SEEN_STAR; 86 } 87 break; 88 89 case IN_COMMENT_SEEN_STAR : 90 if (c == '/') { 91 state = NORMAL; 92 } 93 else { 94 state = IN_COMMENT; 95 } 96 break; 97 98 case IN_SINGLE_LINE_COMMENT : 99 if (c == '\n' || c == '\r') { 100 state = NORMAL; 101 } 102 break; 103 104 case IN_SINGLE_QUOTE : 105 if (c == '\'') { 106 state = NORMAL; 107 } 108 break; 109 110 case IN_DOUBLE_QUOTE : 111 if (c == '\"') { 112 state = NORMAL; 113 } 114 break; 115 116 default : 117 throw new IllegalStateException ("Unhandled state: " + state); } 119 } 120 c = getNext(); 121 } 122 return false; 123 } 124 125 128 protected abstract int getNext() throws IOException ; 129 } 130 | Popular Tags |