1 11 12 package org.eclipse.jdt.apt.core.internal.util; 13 14 import com.sun.mirror.util.SourcePosition; 15 import java.io.File ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.jdt.apt.core.internal.declaration.AnnotationMirrorImpl; 21 import org.eclipse.jdt.apt.core.internal.declaration.AnnotationValueImpl; 22 import org.eclipse.jdt.apt.core.internal.declaration.EclipseDeclarationImpl; 23 import org.eclipse.jdt.apt.core.internal.declaration.EclipseMirrorObject; 24 25 public class SourcePositionImpl implements SourcePosition 26 { 27 private final int _startingOffset; 28 private final int _length; 29 private final int _line; 30 private final int _column; 31 32 private final EclipseMirrorObject _decl; 33 34 public SourcePositionImpl(final int startingOffset, 35 final int length, 36 final int line, 37 final int column, 38 final EclipseDeclarationImpl decl) 39 { 40 _startingOffset = startingOffset; 41 _length = length; 42 _line = line < 1 ? 1 : line; 43 _column = column < 0 ? 0 : column; 44 _decl = decl; 45 assert decl != null : "missing declaration [decl] == null."; } 47 48 public SourcePositionImpl(final int startingOffset, 49 final int length, 50 final int line, 51 final int column, 52 final AnnotationValueImpl decl ) 53 { 54 _startingOffset = startingOffset; 55 _length = length; 56 _line = line < 1 ? 1 : line; 57 _column = column < 0 ? 0 : column; 58 _decl = decl; 59 assert decl != null : "missing declaration [decl] == null."; } 61 62 public SourcePositionImpl(final int startingOffset, 63 final int length, 64 final int line, 65 final int column, 66 final AnnotationMirrorImpl decl ) 67 { 68 _startingOffset = startingOffset; 69 _length = length; 70 _line = line < 1 ? 1 : line; 71 _column = column < 0 ? 0 : column; 72 _decl = decl; 73 assert decl != null : "missing declaration [decl] == null."; } 75 76 public int line(){ return _line; } 77 public int column(){ return _column; } 78 public File file(){ 79 IResource resource = getResource(); 80 if( resource == null ) return null; 81 final IPath absPath = resource.getRawLocation(); 82 if(absPath == null) return null; 83 return new File ( absPath.toOSString() ); 84 } 85 86 public int getStartingOffset(){ return _startingOffset; } 88 public int getEndingOffset(){ return _startingOffset + _length; } 89 public int getLength(){ return _length; } 90 public IFile getResource(){ 91 if( _decl instanceof EclipseDeclarationImpl ) 92 return ((EclipseDeclarationImpl)_decl).getResource(); 93 else if( _decl instanceof AnnotationMirrorImpl ) 94 return ((AnnotationMirrorImpl)_decl).getResource(); 95 else if( _decl instanceof AnnotationValueImpl ) 96 return ((AnnotationValueImpl)_decl).getResource(); 97 98 throw new IllegalStateException (); 99 } 100 101 public String toString() 102 { 103 StringBuilder buffer = new StringBuilder (); 104 buffer.append("offset = "); buffer.append(_startingOffset); 106 buffer.append(" line = "); buffer.append( _line ); 108 buffer.append(" column = "); buffer.append( _column ); 110 buffer.append(" length = "); buffer.append( _length ); 112 113 return buffer.toString(); 114 } 115 } 116 | Popular Tags |