experiments/JavaCompiler.st
branchdevelopment
changeset 2512 f2cbd771fc7b
parent 2486 6b94908daf75
child 2513 6669c7947f72
--- a/experiments/JavaCompiler.st	Sun Apr 14 15:59:08 2013 +0200
+++ b/experiments/JavaCompiler.st	Mon Apr 15 20:54:01 2013 +0200
@@ -29,7 +29,7 @@
 Object subclass:#JavaCompiler
 	instanceVariableNames:'analyzer className imports packageName sourceCode sourceDir
 		requestor classloader'
-	classVariableNames:'CurrentCompilerClass JavaFileOutDirectory'
+	classVariableNames:''
 	poolDictionaries:''
 	category:'Languages-Java-Support-Compiling'
 !
@@ -41,27 +41,6 @@
 	privateIn:JavaCompiler
 !
 
-JavaCompiler subclass:#ECJ
-	instanceVariableNames:''
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:JavaCompiler
-!
-
-JavaCompiler subclass:#Javac
-	instanceVariableNames:''
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:JavaCompiler
-!
-
-JavaCompiler subclass:#JavacExternal
-	instanceVariableNames:''
-	classVariableNames:''
-	poolDictionaries:''
-	privateIn:JavaCompiler
-!
-
 !JavaCompiler class methodsFor:'documentation'!
 
 copyright
@@ -96,10 +75,10 @@
 
 documentation
 "
-    An inteface to Java compiler to compile Java classed from
+    An facade to Java compiler to compile Java classed from
     source (given as string).
 
-    Real compilation is implemented in one of my subclasses.
+    Internally, it uses ECJ. See stx.libjava.compiler.ecj.CompilerAdapter.
 
     [author:]
         Jan Vrany <jan.vrany@fit.cvut.cz>
@@ -119,25 +98,13 @@
     "Created: #dotJavaPathname / 13-12-2012 / 00:02:03 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
 ! !
 
-!JavaCompiler class methodsFor:'initialization'!
-
-initialize
-    "Invoked at system start or when the class is dynamically loaded"
-
-    "/ please change as required (and remove this comment)
-
-    CurrentCompilerClass := ECJ.
-    JavaFileOutDirectory := Filename newTemporaryDirectory.
-
-    "Modified: / 29-03-2013 / 23:00:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !JavaCompiler class methodsFor:'instance creation'!
 
 new
-    ^(CurrentCompilerClass ? self) basicNew initialize
+    ^self basicNew initialize
 
     "Created: / 15-12-2012 / 16:48:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 15-04-2013 / 20:43:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 newAnalyzer
@@ -146,14 +113,6 @@
     "Created: / 15-12-2012 / 16:58:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
-!JavaCompiler class methodsFor:'accessing'!
-
-fileOutDirectory
-    ^JavaFileOutDirectory
-
-    "Created: / 15-12-2012 / 23:03:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
 !JavaCompiler class methodsFor:'compiler interface'!
 
 compile: source
@@ -228,8 +187,8 @@
     ^ requestor
 !
 
-requestor:something
-    requestor := something.
+requestor:anObject
+    requestor := anObject.
 ! !
 
 !JavaCompiler methodsFor:'compiler interface'!
@@ -258,9 +217,38 @@
      Upon error, throws an exception"
 
 
-    ^ self subclassResponsibility
+    | javac classfiles |
+
+    analyzer := self class newAnalyzer.
+    analyzer analyze: source.
+    classloader isNil ifTrue:[
+        classloader := JavaClassReader classLoaderQuerySignal query.
+        classloader isNil ifTrue:[
+            classloader := JavaVM systemClassLoader.
+        ]
+    ].
+
+    javac := (Java classForName:'stx.libjava.tools.compiler.ecj.CompilerAdapter') new: 
+               classloader.
+
+    javac compile: analyzer fullName source: source.
 
-    "Modified (comment): / 02-01-2013 / 16:32:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "javac getResult hasErrors"
+    javac getClassFiles size == 0 ifTrue:[
+        ^self error:'Compilation failed'
+    ].
+
+    classfiles := javac getClassFiles.
+    ^classfiles collect:[:each|
+        (JavaClassReader readStream: each getBytes readStream)
+            classLoader: classloader;
+            setSource: source;
+            setClassfileBytes: each getBytes;
+            yourself].
+
+    "Created: / 15-12-2012 / 23:04:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-12-2012 / 15:36:16 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
+    "Modified: / 15-04-2013 / 17:55:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaCompiler::ClassSourceAnalyzer class methodsFor:'accessing'!
@@ -425,255 +413,6 @@
     "Modified: / 15-12-2012 / 17:04:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
-!JavaCompiler::ECJ methodsFor:'compiling'!
-
-compile:source
-    "Compiles a java class in given source code (passed as String).
-     Return an array of JavaClass which are not yet registered in Java 
-     class registry nor initialiized.
-
-     To make it accessible for Java code, caller must register returned
-     classes himself.
-
-     Upon error, throws an exception"
-
-
-    | javac classfiles |
-
-    analyzer := self class newAnalyzer.
-    analyzer analyze: source.
-    classloader isNil ifTrue:[
-        classloader := JavaClassReader classLoaderQuerySignal query.
-        classloader isNil ifTrue:[
-            classloader := JavaVM systemClassLoader.
-        ]
-    ].
-
-    javac := (Java classForName:'stx.libjava.tools.compiler.ecj.CompilerAdapter') new: 
-               classloader.
-
-    javac compile: analyzer fullName source: source.
-
-    javac getResult hasErrors ifTrue:[
-        ^self error:'Compilation failed'
-    ].
-
-    classfiles := javac getClassFiles.
-    ^classfiles collect:[:each|
-        (JavaClassReader readStream: each getBytes readStream)
-            classLoader: classloader;
-            setSource: source;
-            setClassfileBytes: each getBytes;
-            yourself].
-
-    "Created: / 15-12-2012 / 23:04:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 16-12-2012 / 15:36:16 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
-    "Modified: / 09-04-2013 / 22:41:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaCompiler::Javac class methodsFor:'documentation'!
-
-documentation
-"
-    A Java compiler that uses Java 6 Tool API (i.e., loads javac into
-    running libjava)
-
-    [author:]
-        Jan Vrany <jan.vrany@fit.cvut.cz>
-
-    [instance variables:]
-
-    [class variables:]
-
-    [see also:]
-        http://today.java.net/pub/a/today/2008/04/10/source-code-analysis-using-java-6-compiler-apis.html
-
-"
-! !
-
-!JavaCompiler::Javac methodsFor:'compiling'!
-
-compile:source
-    "Compiles a java class in given source code (passed as String).
-     Return an array of JavaClass which are not yet registered in Java 
-     class registry nor initialiized.
-
-     To make it accessible for Java code, caller must register returned
-     classes himself.
-
-     Upon error, throws an exception"
-
-
-    | javac classfiles |
-
-    analyzer := self class newAnalyzer.
-    analyzer analyze: source.
-    javac := (Java classForName:'stx.libjava.tools.compiler.JavaCompilerAdapter') new.
-    (javac compile: analyzer name source: source) ifFalse:[
-        self error:'Compilation failed for whatever reason!!'.
-    ].
-
-    classfiles := javac getClassFiles toArray.
-    ^classfiles collect:[:each|JavaClassReader readStream: each getBytes readStream].
-
-    "Created: / 15-12-2012 / 23:04:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 16-12-2012 / 15:36:16 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
-    "Modified: / 02-01-2013 / 16:33:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-! !
-
-!JavaCompiler::JavacExternal class methodsFor:'documentation'!
-
-documentation
-"                                        
-    A Java compiler that calls external javac command. 
-    Unfinished.
-
-    [author:]
-        Jan Vrany <jan.vrany@fit.cvut.cz>
-
-    [instance variables:]
-
-    [class variables:]
-
-    [see also:]
-
-"
-! !
-
-!JavaCompiler::JavacExternal methodsFor:'compiling'!
-
-compile: source
-    "Compiles a java class in given source code (passed as String).
-     Return an array of JavaClass which are not yet registered in Java 
-     class registry nor initialiized.
-
-     To make it accessible for Java code, caller must register returned
-     classes himself.
-
-     Upon error, throws an exception"
-
-
-    | 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>"
-    "Modified (comment): / 02-01-2013 / 16:32:28 / Jan Vrany <jan.vrany@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::JavacExternal methodsFor:'private'!
-
-analyzerFor: sourceCode
-   ^ self newAnalyzer analyze: sourceCode.
-
-    "Created: / 08-12-2012 / 21:57:00 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
-    "Modified: / 15-12-2012 / 16:58:46 / Jan Vrany <jan.vrany@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>"
-!
-
-fileOut: source
-    "Files out given source into a file under JavaFileOutDirectory.
-     into proper directory (as javac requires). The directory is created
-     if it does not exists.
-
-     Returns a full filename of the filed-out source"
-
-    | analyzer filename |
-    analyzer := self class newAnalyzer.
-    analyzer analyze: source.
-    filename := JavaFileOutDirectory / 
-                    ((analyzer package ? '') copyReplaceAll:$. with: Filename separator) /
-                    (analyzer name , '.java').
-    filename directory exists ifFalse:[
-        filename directory recursiveMakeDirectory
-    ].
-    filename writingFileDo:[:f|f nextPutAll: source].
-    ^filename.
-
-    "Created: / 15-12-2012 / 23:04:46 / Jan Vrany <jan.vrany@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_CVS
@@ -689,5 +428,3 @@
     ^ '§Id::                                                                                                                        §'
 ! !
 
-
-JavaCompiler initialize!