github.com/LanceLRQ/deer-common@v0.0.9-0.20210319081233-e8222ac018a8/provider/java.go (about)

     1  /* Java Compiler Provider
     2   * (C) 2019 LanceLRQ
     3   *
     4   * This code is licenced under the GPLv3.
     5   */
     6  package provider
     7  
     8  import (
     9  	"fmt"
    10  	"path"
    11  	"regexp"
    12  )
    13  
    14  type JavaCompileProvider struct {
    15  	CodeCompileProvider
    16  	javaClassName string
    17  }
    18  
    19  func NewJavaCompileProvider() *JavaCompileProvider {
    20  	java := JavaCompileProvider{
    21  		javaClassName: "",
    22  	}
    23  	java.isReady = false
    24  	java.realTime = false
    25  	java.Name = "java"
    26  	return &java
    27  }
    28  
    29  func getJavaClassName(code string) (className string, err error) {
    30  	reg := regexp.MustCompile(`public class ([A-Za-z0-9_$]+)`)
    31  	matched := reg.FindSubmatch([]byte(code))
    32  	if matched != nil {
    33  		className = string(matched[1])
    34  		err = nil
    35  	} else {
    36  		className = "Main" // default java public classname (might cause compile error)
    37  		// err = errors.Errorf("no java public class name matched")
    38  	}
    39  	return
    40  }
    41  
    42  func (prov *JavaCompileProvider) Init(code string, workDir string) error {
    43  	prov.isReady = false
    44  	prov.realTime = false
    45  	prov.codeContent = code
    46  	prov.workDir = workDir
    47  	prov.Name = "java"
    48  
    49  	javaClassName, err := getJavaClassName(prov.codeContent)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	prov.javaClassName = javaClassName
    54  
    55  	err = prov.checkWorkDir()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	err = prov.initFiles(".java", ".class")
    61  	return err
    62  }
    63  
    64  func (prov *JavaCompileProvider) initFiles(codeExt string, programExt string) error {
    65  	prov.codeFileName = fmt.Sprintf("%s%s", prov.javaClassName, codeExt)
    66  	prov.programFileName = fmt.Sprintf("%s%s", prov.javaClassName, programExt)
    67  	prov.codeFilePath = path.Join(prov.workDir, prov.codeFileName)
    68  	prov.programFilePath = path.Join(prov.workDir, prov.programFileName)
    69  	err := prov.saveCode()
    70  	return err
    71  }
    72  
    73  func (prov *JavaCompileProvider) Compile() (result bool, errmsg string) {
    74  	result, errmsg = prov.shell(fmt.Sprintf(CompileCommands.Java, prov.codeFilePath, path.Dir(prov.programFilePath)))
    75  	if result {
    76  		prov.isReady = true
    77  	}
    78  	return
    79  }
    80  
    81  func (prov *JavaCompileProvider) GetRunArgs() (args []string) {
    82  	args = []string{
    83  		"/usr/bin/java", "-client", "-Dfile.encoding=utf-8",
    84  		"-classpath", path.Dir(prov.programFilePath), prov.javaClassName,
    85  	}
    86  	return
    87  }
    88  
    89  func (prov *JavaCompileProvider) IsCompileError(remsg string) bool {
    90  	return false
    91  }