experiments/JavaCompiler.st
branchdevelopment
changeset 1869 0ae14ac1c9af
child 1880 27b932afa4a7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/experiments/JavaCompiler.st	Wed Dec 12 23:26:59 2012 +0000
@@ -0,0 +1,185 @@
+"
+ Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
+                         SWING Research Group, Czech Technical University 
+                         in Prague
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the 'Software'), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+"
+"{ Package: 'stx:libjava/experiments' }"
+
+Object subclass:#JavaCompiler
+	instanceVariableNames:'analyzer className imports packageName sourceCode sourceDir'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Languages-Java-Support-Compiling'
+!
+
+!JavaCompiler class methodsFor:'documentation'!
+
+copyright
+"
+ Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
+                         SWING Research Group, Czech Technical University 
+                         in Prague
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the 'Software'), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+"
+!
+
+history
+
+    "Created: #dotJavaPathname / 13-12-2012 / 00:02:03 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+! !
+
+!JavaCompiler class methodsFor:'compiling'!
+
+compile: sourceFile 
+    ^ self new compile: sourceFile.
+
+    "Created: / 06-12-2012 / 22:14:12 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+! !
+
+!JavaCompiler methodsFor:'compiling'!
+
+compile: source 
+    | classDir  wasSuccessful  package  packageAsPath  compiledClass  result |
+    sourceCode := source.
+    sourceDir := Java cacheDirectory.
+    analyzer := self analyzerFor: sourceCode.
+        self fileOutSourceCode.
+    result := self runCompiler.
+    ^ result.
+
+    "Created: / 06-12-2012 / 23:13:47 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 07-12-2012 / 10:05:03 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 12-12-2012 / 23:58:04 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+runCompiler
+    | result  compiledClass |
+    result := OperatingSystem 
+            executeCommand: 'javac -cp "' , Java classPathAsString , ':' , sourceDir pathName
+                    , '" ' , self dotJavaPathname
+            inDirectory: sourceDir.
+    result ifFalse: [ self error: 'Compilation of Java class failed' ].
+    compiledClass := JavaClassReader 
+            readFile: sourceDir / self dotClassPathname
+            ignoring: #().
+    compiledClass classInit.
+    compiledClass setSource: sourceCode.
+    ^ compiledClass.
+
+    "Created: / 08-12-2012 / 22:02:47 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+    "Modified: / 13-12-2012 / 00:11:43 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+! !
+
+!JavaCompiler methodsFor:'private'!
+
+analyzerFor: sourceCode
+   ^ JavaSourceCodeAnalyzer analyze: sourceCode.
+
+    "Created: / 08-12-2012 / 21:57:00 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+className
+    className ifNil: [ className := analyzer className. ].
+    ^ className.
+
+    "Created: / 12-12-2012 / 23:59:08 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+classPackageAsPath
+    | name |
+    name := self packageName.
+    name isNil ifTrue: [ ^ '' ].
+    ^ (name copyReplaceAll: $. with: Filename separator) , Filename separator.
+
+    "Created: / 08-12-2012 / 22:50:40 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+    "Modified: / 13-12-2012 / 00:02:18 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+dotClassPathname
+    ^ self classPackageAsPath , (self className , '.class').
+
+    "Created: / 13-12-2012 / 00:01:49 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+dotJavaPathname
+    ^ self classPackageAsPath , (self className , '.java').
+
+    "Created: / 13-12-2012 / 00:02:03 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+fileOutSourceCode
+    | path |
+    path := sourceDir / self dotJavaPathname.
+    path directory recursiveMakeDirectory.                     
+    path exists ifTrue: [ path delete ].
+    path createAsEmptyFile.
+    path writingFileDo: [:out | out nextPutAll: sourceCode ].
+
+    "Created: / 08-12-2012 / 22:43:30 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+    "Modified: / 13-12-2012 / 00:09:03 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+fullClassName
+    | package |
+    package := self packageName.
+    package ifNil: [ ^ self className ].
+    ^ package , '.' , self className.
+
+    "Created: / 09-12-2012 / 20:30:13 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+    "Modified: / 13-12-2012 / 00:03:07 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+!
+
+packageName
+    packageName ifNil: [ packageName := analyzer packageName ].
+    ^ packageName.
+
+    "Created: / 12-12-2012 / 23:59:00 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+! !
+
+!JavaCompiler class methodsFor:'documentation'!
+
+version_SVN
+    ^ '$Id::                                                                                                                        $'
+! !