tools/JavaSourceRef.st
branchdevelopment
changeset 2717 16564a7101b8
parent 2716 1cca77810e04
child 2718 b3fe904a2fc7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/JavaSourceRef.st	Mon Sep 09 01:27:53 2013 +0100
@@ -0,0 +1,112 @@
+"{ Package: 'stx:libjava/tools' }"
+
+Object subclass:#JavaSourceRef
+	instanceVariableNames:'line offset length'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Languages-Java-Tools-Source'
+!
+
+!JavaSourceRef class methodsFor:'documentation'!
+
+documentation
+"
+    JavaMethodSourceRef is a reference to method's source in
+    its class's source file.
+    
+
+    [author:]
+        Jan Vrany <jan.vrany@fit.cvut.cz>
+
+    [instance variables:]
+        line <Integer>      line number of first line of the method's source.
+                            If the method have Javadoc, then line is first line
+                            of the Javadoc, otherwise it's a first line of its
+                            header, 
+        offset <Integer>    offset of methods source portion in source file in bytes.
+                            Zero-based.
+        length <Integer>    length of the source in bytes.
+                            Zero-based.
+        
+
+    [class variables:]
+
+    [see also:]
+
+"
+! !
+
+!JavaSourceRef class methodsFor:'utilities'!
+
+undent: source
+    | lines indent |
+
+    lines := source asStringCollection withTabsExpanded.
+    lines size == 1 ifTrue:[ ^ source ].
+
+    indent := lines last indexOfNonSeparator - 1.
+    (indent \\ 4) == 0 ifFalse:[ ^ source ].
+    2 to: lines size do:[:lineNr |
+        | line |
+
+        line := lines at: lineNr.
+        1 to: indent do:[:colNr |
+            (line at: colNr) ~~ (Character space) ifTrue:[ ^ source ].
+        ].
+        lines at: lineNr put: (line copyFrom: indent + 1).
+    ].
+    ^ lines asString
+
+    "Created: / 09-09-2013 / 01:05:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaSourceRef methodsFor:'accessing'!
+
+length
+    ^ length
+!
+
+length:something
+    length := something.
+!
+
+line
+    ^ line
+!
+
+line:something
+    line := something.
+!
+
+offset
+    ^ offset
+!
+
+offset:something
+    offset := something.
+! !
+
+!JavaSourceRef methodsFor:'reading'!
+
+readFrom: stringOrStream
+    | s |
+
+    s := stringOrStream readStream.
+    s position: offset.
+    ^ self class undent: (s next: length)
+
+    "Created: / 07-09-2013 / 02:31:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 09-09-2013 / 01:05:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaSourceRef class methodsFor:'documentation'!
+
+version_CVS
+    ^ '$Header: /cvs/stx/stx/libjava/tools/JavaSourceReference.st,v 1.2 2013-02-25 11:15:35 vrany Exp $'
+!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+! !
+