SpencerPark / IJava

A Jupyter kernel for executing Java code.
MIT License
1.07k stars 211 forks source link

how can i use lombok in ijava-kernel #126

Open xdewx opened 2 years ago

xdewx commented 2 years ago
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.Setter;

ObjectMapper mapper = new ObjectMapper();
String jsonStr = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
Map<String, Object> map = mapper.readValue(jsonStr, Map.class);
System.out.println(map);

@Getter
@Setter
@JsonNaming(PropertyNamingStrategy.LowerDotCaseStrategy.class)
//    @JsonNaming(PropertyNamingStrategy.LowerCaseStrategy.class)
//    @JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
//    @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
static class TestBean {

    String sName = "andy";
    String XName = "dede";
    String sourceName = "xx";
    String FirstName = "first";
    String a_name = "xxxcd";
}
TestBean t=new TestBean();

String x=mapper.writeValueAsString(t);
x

image

the getter and setter do not work! What shoud i do?

potoo0 commented 2 years ago

it seems jshell not load any annotation processor (such as lombok and mapstruct, etc).

https://bugs.openjdk.java.net/browse/JDK-8213600

potoo0 commented 2 years ago

We can compile source code in shell, then import those bytecode class files:

  1. edit java source code, eg TestVO:

    package vo;
    
    @lombok.Data
    public class TestVO {
        private String name;
        private Integer age;
    }
  2. download lombok.jar(https://projectlombok.org/downloads/lombok.jar) to libs;

  3. compile: cd vo; javac -cp libs/lombok.jar TestVO.java;

  4. add to classpath, eg: add vo parent folder to env's IJAVA_CLASSPATH(restart jupyter): export IJAVA_CLASSPATH="/home/jupyter/notebooks/0-Java:$IJAVA_CLASSPATH";

  5. import in jupyter cell, eg: import vo.*;

xdewx commented 2 years ago

Very useful. But it seems a little troublesome. It will be great if ijava-kernel could compile and setup by itself. Thank u for your reply.

potoo0 commented 1 year ago

We could compile at runtime by ourselves.

eg:

// 1. compiler output, use System.err if null
StringWriter out = new StringWriter();
// 2. a diagnostic listener; if null use the compiler's default method for reporting diagnostics
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
// 3. a file manager; if null use the compiler's standard file manager
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
// 4. compiler options, null means no options
List<String> options = buildCompileOptions(compileOptions);
// 5. the compilation units to compile, null means no compilation units
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(sourceFile));

CompilationTask task = compiler.getTask(out, fileManager, diagnostics, options, null, compilationUnit);
Boolean isCompileSuccess = task.call();
fileManager.close();

I built a cell magic for compile, here is source code:

  1. compile utils: RuntimeCompiler
  2. cell magic: CompilerMagics
  3. usage: image