1 12 13 package org.eclipse.jdt.apt.core.internal.util; 14 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.InputStreamReader ; 18 import java.util.HashSet ; 19 import java.util.Set ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.jdt.apt.core.internal.AptPlugin; 23 import org.eclipse.jdt.apt.core.internal.env.BuildEnv; 24 import org.eclipse.jdt.core.IBuffer; 25 import org.eclipse.jdt.core.ICompilationUnit; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.core.JavaModelException; 28 import org.eclipse.jdt.core.ToolFactory; 29 import org.eclipse.jdt.core.compiler.IScanner; 30 import org.eclipse.jdt.core.compiler.ITerminalSymbols; 31 import org.eclipse.jdt.core.compiler.InvalidInputException; 32 33 public class ScannerUtil { 34 35 38 public static boolean hasAnnotationInstance( IFile f ) { 39 40 InputStreamReader reader = null; 41 InputStream input = null; 42 try { 43 AnnotationScanner scanner; 44 if (SINGLE_BYTE_ENCODINGS.contains(f.getCharset())) { 47 input = BuildEnv.getInputStream(f); 48 scanner = new InputStreamAnnotationScanner(input); 49 } 50 else { 51 reader = BuildEnv.getFileReader( f ); 52 scanner = new ReaderAnnotationScanner(reader); 53 } 54 return scanner.containsAnnotations(); 55 } 56 catch( Exception ioe ) { 57 return false; 58 } 59 finally { 60 if (reader != null) { try {reader.close();} catch (IOException ioe) {} } 61 if (input != null) { try {input.close();} catch (IOException ioe) {} } 62 } 63 } 64 65 66 public static boolean hasAnnotationInstance( ICompilationUnit cu ) { 67 try { 68 IBuffer b = cu.getBuffer(); 69 if ( b == null ) 70 return false; 71 char[] source = b.getCharacters(); 72 return hasAnnotationInstance( source ); 73 } 74 catch( JavaModelException jme ) { 75 return false; 76 } 77 } 78 79 80 public static boolean hasAnnotationInstance( char[] source ) { 81 try { 82 if ( source == null ) 83 return false; 84 IScanner scanner = ToolFactory.createScanner( 85 false, false, false, JavaCore.VERSION_1_5 ); 86 scanner.setSource( source ); 87 int token = scanner.getNextToken(); 88 while ( token != ITerminalSymbols.TokenNameEOF ) { 89 token = scanner.getNextToken(); 90 if ( token == ITerminalSymbols.TokenNameAT ) 91 { 92 token = scanner.getNextToken(); 98 if ( token != ITerminalSymbols.TokenNameinterface ) 99 return true; 100 } 101 } 102 return false; 103 } 104 catch( InvalidInputException iie ) 105 { 106 return false; 108 } 109 catch( Exception e ) 110 { 111 AptPlugin.log(e, "Failure scanning source: \n" + new String (source)); return false; 114 } 115 } 116 117 private static final String [] SINGLE_BYTE_ENCODING_ARRAY = { 118 "ASCII", "Cp1250", "Cp1251", "Cp1252", "Cp1253", "Cp1254", "Cp1257", "ISO8859_1", "ISO8859_2", "ISO8859_4", "ISO8859_5", "ISO8859_7", "ISO8859_9", "ISO8859_13", "ISO8859_15", "UTF8" }; 135 136 private static final Set <String > SINGLE_BYTE_ENCODINGS = 137 new HashSet <String >(SINGLE_BYTE_ENCODING_ARRAY.length); 138 139 static { 140 for (String encoding : SINGLE_BYTE_ENCODING_ARRAY) { 141 SINGLE_BYTE_ENCODINGS.add(encoding); 142 } 143 } 144 145 } 146 | Popular Tags |