1 11 12 package org.eclipse.jdt.apt.core.internal.env; 13 14 import com.sun.mirror.apt.Messager; 15 import com.sun.mirror.util.SourcePosition; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.jdt.apt.core.internal.util.SourcePositionImpl; 19 import org.eclipse.jdt.apt.core.util.EclipseMessager; 20 import org.eclipse.jdt.core.dom.ASTNode; 21 import org.eclipse.jdt.core.dom.CompilationUnit; 22 23 24 public class MessagerImpl implements Messager, EclipseMessager 25 { 26 public static enum Severity { 27 ERROR, 28 WARNING, 29 INFO 30 } 31 private final AbstractCompilationEnv _env; 32 33 MessagerImpl(AbstractCompilationEnv env){ 34 _env = env; 35 } 36 37 public void printError(SourcePosition pos, String msg, String [] arguments) 38 { 39 if( pos == null ) 40 printError(msg); 41 else if( pos instanceof SourcePositionImpl ) 42 print((SourcePositionImpl)pos, Severity.ERROR, msg, arguments); 43 else 44 print(pos, Severity.ERROR, msg, arguments); 45 } 46 47 public void printError(ASTNode node, String msg) 48 { 49 if( node == null ) 50 throw new IllegalArgumentException ("'node' cannot be null"); final int start = node.getStartPosition(); 52 int line = _env.getAstCompilationUnit().getLineNumber(start); 55 if( line < 1 ) 56 line = 1; 57 _env.addMessage(_env.getFile(), start, node.getLength() + start, Severity.ERROR, msg, line, null ); 58 } 59 60 public void printError(String msg) 61 { 62 print(Severity.ERROR, msg, null); 63 } 64 65 public void printNotice(SourcePosition pos, String msg, String [] arguments) 66 { 67 if( pos instanceof SourcePositionImpl ) 68 print((SourcePositionImpl)pos, Severity.INFO, msg, arguments); 69 else if (pos == null ) 70 printNotice(msg); 71 else 72 print(pos, Severity.INFO, msg, arguments); 73 } 74 75 public void printNotice(ASTNode node, String msg) 76 { 77 if( node == null ) 78 throw new IllegalArgumentException ("'node' cannot be null"); final int start = node.getStartPosition(); 80 int line = _env.getAstCompilationUnit().getLineNumber(start); 83 if( line < 1 ) 84 line = 1; 85 _env.addMessage(_env.getFile(), start, node.getLength() + start, Severity.INFO, msg, line, null ); 86 } 87 88 public void printNotice(String msg) 89 { 90 print(Severity.INFO, msg, null); 91 } 92 93 public void printWarning(SourcePosition pos, String msg, String [] arguments) 94 { 95 if( pos instanceof SourcePositionImpl ) 96 print((SourcePositionImpl)pos, Severity.WARNING, msg, arguments); 97 else if (pos == null ) 98 printWarning(msg); 99 else 100 print(pos, Severity.WARNING, msg, arguments); 101 } 102 103 public void printWarning(ASTNode node, String msg) 104 { 105 if( node == null ) 106 throw new IllegalArgumentException ("'node' cannot be null"); final int start = node.getStartPosition(); 108 int line = _env.getAstCompilationUnit().getLineNumber(start); 111 if( line < 1 ) 112 line = 1; 113 _env.addMessage(_env.getFile(), start, node.getLength() + start, Severity.WARNING, msg, line, null); 114 } 115 116 public void printWarning(String msg) 117 { 118 print(Severity.WARNING, msg, null); 119 } 120 121 public void printError(SourcePosition pos, String msg) { 122 printError(pos, msg, null); 123 } 124 125 public void printWarning(SourcePosition pos, String msg) { 126 printWarning(pos, msg, null); 127 } 128 129 public void printNotice(SourcePosition pos, String msg) { 130 printNotice(pos, msg, null); 131 } 132 133 public void printFixableError(SourcePosition pos, String msg, String pluginId, String errorId) { 134 if (pluginId == null) { 135 throw new IllegalArgumentException ("pluginId cannot be null"); } 137 if (errorId == null) { 138 throw new IllegalArgumentException ("errorId cannot be null"); } 140 printError(pos, msg, new String [] {pluginId, errorId}); 141 } 142 143 public void printFixableWarning(SourcePosition pos, String msg, String pluginId, String errorId) { 144 if (pluginId == null) { 145 throw new IllegalArgumentException ("pluginId cannot be null"); } 147 if (errorId == null) { 148 throw new IllegalArgumentException ("errorId cannot be null"); } 150 printWarning(pos, msg, new String [] {pluginId, errorId}); 151 } 152 153 public void printFixableNotice(SourcePosition pos, String msg, String pluginId, String errorId) { 154 if (pluginId == null) { 155 throw new IllegalArgumentException ("pluginId cannot be null"); } 157 if (errorId == null) { 158 throw new IllegalArgumentException ("errorId cannot be null"); } 160 printNotice(pos, msg, new String [] {pluginId, errorId}); 161 } 162 163 public void printFixableError(String msg, String pluginId, String errorId) { 164 if (pluginId == null) { 165 throw new IllegalArgumentException ("pluginId cannot be null"); } 167 if (errorId == null) { 168 throw new IllegalArgumentException ("errorId cannot be null"); } 170 print(Severity.ERROR, msg, new String [] {pluginId, errorId}); 171 } 172 173 public void printFixableWarning(String msg, String pluginId, String errorId) { 174 if (pluginId == null) { 175 throw new IllegalArgumentException ("pluginId cannot be null"); } 177 if (errorId == null) { 178 throw new IllegalArgumentException ("errorId cannot be null"); } 180 print(Severity.WARNING, msg, new String [] {pluginId, errorId}); 181 } 182 183 public void printFixableNotice(String msg, String pluginId, String errorId) { 184 if (pluginId == null) { 185 throw new IllegalArgumentException ("pluginId cannot be null"); } 187 if (errorId == null) { 188 throw new IllegalArgumentException ("errorId cannot be null"); } 190 print(Severity.INFO, msg, new String [] {pluginId, errorId}); 191 } 192 193 private void print(SourcePositionImpl pos, 194 Severity severity, 195 String msg, 196 String [] arguments) 197 { 198 final IFile resource = pos.getResource(); 199 if( resource == null ){ 200 throw new IllegalStateException ("missing resource"); } 202 else{ 203 _env.addMessage(resource, pos.getStartingOffset(), pos.getEndingOffset(), 204 severity, msg, pos.line(), arguments); 205 } 206 } 207 208 private void print(SourcePosition pos, 209 Severity severity, 210 String msg, 211 String [] arguments) 212 { 213 final java.io.File file = pos.file(); 214 IFile resource = null; 215 if( file != null ){ 216 final String projAbsPath = _env.getProject().getLocation().toOSString(); 217 final String fileAbsPath = file.getAbsolutePath(); 218 final String fileRelPath = fileAbsPath.substring(projAbsPath.length()); 219 resource = _env.getProject().getFile(fileRelPath); 220 if( !resource.exists() ) 221 resource = null; 222 } 223 224 int offset = -1; 225 if( resource != null ){ 226 final CompilationUnit unit = _env.getASTFrom(resource); 227 if( unit != null ) 228 offset = unit.getPosition( pos.line(), pos.column() ); 229 } 230 _env.addMessage(resource, offset, -1, severity, msg, pos.line(), arguments ); 231 } 232 233 private void print(Severity severity, String msg, String [] arguments) 234 { 235 _env.addMessage(null, -1, -1, severity, msg, 1, arguments ); 236 237 } 238 239 } 240 | Popular Tags |