Skip to main content

A java class file operate tools

Project description

java-coder

java-coder is a python tool to operate Java source code. It provides a series of ways to parse, modified and rebuild Java source code based on javalang, targeting Java 8.

the following gives a brief examples to use java-coder.

Getting Started

java-coder will load java source code from the java file and parse the code and show them.

from javacoder.core import operator

op = operator.ClassOperator(class_path="your_class_path")
op.preview_class()

java-coder also provides builder tools to create a new class file easily.

from javacoder.core.builder import ClassBuilder

builder = ClassBuilder("DemoClass")
builder.field("test1", annotation='JSONField', name="111")
builder.field("hello")
builder.extend("ExtendClass")
builder.implement("Implement1")
builder.implement("Implement2")
builder.add_fields_getter_and_setter()
builder.document("this is a test document")
builder.annotation("AnnotationDemo2")
builder.annotation("AnnotationDemo")
builder.imports("com.path")
builder.build().preview_class()

java-coder also provides some useful plugins to generate some infos automatically. It also supports you to apply your own plugin.

from javacoder.core.builder import InterfaceBuilder

project_path = 'your project path'
builder = InterfaceBuilder("DemoClass")
builder.author()
builder.project(project_path)
builder.extend("Implement1", "JPA_DO")
builder.extend("Header")
builder.document("this is a test interface")
builder.annotation("AnnotationDemo2")
builder.annotation("AnnotationDemo")
builder.method("hello")
builder.get_method_cursor().add(r'public void test(){ do { System.out.println("hello world");}while(true);}')
builder.imports("com.path")
builder.imports("com.path2", wildcard=True)
builder.build().preview_class()

Plugins

plugin is an important part in java-coder, it will act after some actions. There has some native plugins in java-coder.

  • AuthorPlugin

AuthorPlugin can auto add basic author infos in your source file.
AuthorPlugin will use your git config by default if you don't config the info.

  • ProjectPlugin

ProjectPlugin can scan the whole project and record the files.
ProjectPlugin can load pom.xml and parse it, recording the dependencies.
ProjectPlugin can scan the dependencies of the project and extract .class file from the jar file in your local mvn library.

Builder Extends

If you are operating multi classes and want to use the same config, you can extend builder to simplify builder initiation as follows.

from javacoder.core.builder import InterfaceBuilder

project_path = 'your project path'
builder1 = InterfaceBuilder("DemoClass1")
builder1.author()
builder1.project(project_path)
builder1.build()

builder2 = InterfaceBuilder("DemoClass2")
builder2.extend_builder(builder1)
builder2.build()

or

from javacoder.core.builder import InterfaceBuilder, create_builder

project_path = 'your project path'
builder1 = InterfaceBuilder("DemoClass1")
builder1.author()
builder1.project(project_path)
builder1.build()

builder2 = create_builder("DemoClass2", InterfaceBuilder, builder1)
builder2.build()

tips: builder can only extend built builder by once.

Project Module

project module will help you gen a basic java project easily. sample as following

from javacoder.project.project import Project

project_demo = Project('/your/project/path')
project_demo.set_current_package('com.package.demo')

# create a interface 
interface_builder = project_demo.interface_builder('DemoInterface')
interface_builder.document('this is a demo interface')
# create a class
class_builder = project_demo.class_builder('DemoClass')
class_builder.document('this is a demo class')

# save project
project_demo.save()

Useful Tools

DDL Generator

java-coder gives some useful tools to make every java coder work easily in daily work.

from javacoder.core import operator
from javacoder.utils import generator

op = operator.ClassOperator(class_path="your_class_path")
ddl_gen = generator.convert_class_fields_to_db_ddl_builder("table_name")
print(ddl_gen(class_fields=[i.declarators[0].name for i in op.get_fields()]))

this will help you generate ddl according to class fields.

FOR JPA ENTITY:

java-coder provides way to generate ddl quickly as follows:

from javacoder.core.operator import ClassOperator
from javacoder.utils.generator import convert_jpa_entity_to_ddl

op = ClassOperator(class_path='your_class_path')
print(convert_jpa_entity_to_ddl(op))

java-coder will recognize the entity and generate ddl as defined in your java source code.

furthermore, your can add ProjectPlugin to make java-coder enable to scan your whole project and generate the entire ddl if the entity extends from parent entity.

Just like this:

from javacoder.core.operator import ClassOperator
from javacoder.core.plugins import ProjectPlugin
from javacoder.utils.generator import convert_jpa_entity_to_ddl

op = ClassOperator(class_path='your_class_path')
op.add_plugin(ProjectPlugin(project_path='your_project_path'))
print(convert_jpa_entity_to_ddl(op))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

java_coder-0.0.5.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

java_coder-0.0.5-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file java_coder-0.0.5.tar.gz.

File metadata

  • Download URL: java_coder-0.0.5.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for java_coder-0.0.5.tar.gz
Algorithm Hash digest
SHA256 cfc5c4f942ee6ae0c977676c7df8df6bf41ef7ced6af389b6d189f3a383ae7b5
MD5 3c49ebc99c7493cfed5fbd168da4d5bb
BLAKE2b-256 9513d2c5341b87aae3add0f24de97880f7eda579373e9b6a4e9823488f8cd0a5

See more details on using hashes here.

File details

Details for the file java_coder-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: java_coder-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for java_coder-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cd1e3da720b73f1a6c65971b17cac4f6c2de9bde0b3c1e2c3f77fe984dd9a245
MD5 9474c284dc985b45febcaae285b8edc8
BLAKE2b-256 2657bf4164a184fa08c040f456f04b2c9b4fdd2e96b812efa2b30651227111a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page