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.4.tar.gz (21.0 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.4-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: java_coder-0.0.4.tar.gz
  • Upload date:
  • Size: 21.0 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.4.tar.gz
Algorithm Hash digest
SHA256 bdd304d77d0b71e2c5ac58215a80b8b832598af7dbc34235882b00ef6eadf165
MD5 7673b61dc9ee3752cd4b96b4dd0b80da
BLAKE2b-256 d23fe6948ed7b77515ed9fe763b351709677a91165fe547502eb9796453ee65c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: java_coder-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 24.5 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 578641ddd9fcfe4f01c3bcc2c052f5eeb28be867eb7b902851e86c4de1b41f2a
MD5 d53f71c9cbbd67ccf90cc8b6d6cb68fc
BLAKE2b-256 6c2afc279ab3920d65253c4b7b1d6fd801e85271cf75d6c6d13c874f2304f7de

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