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.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.3.tar.gz (19.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.3-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: java_coder-0.0.3.tar.gz
  • Upload date:
  • Size: 19.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.3.tar.gz
Algorithm Hash digest
SHA256 50ed48ae132f6147cdf1d4e43f515427ff58d9aec159eb1629bcf79d1b5b5c7a
MD5 22086bb81ee885941138c4ca5120e46e
BLAKE2b-256 95582fbd2ba97838c98ce6bf863e03b4f313b832b96f7bcd2dd7a09167f0947e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: java_coder-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 22.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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f0d905c926da8e3556b561f81ad3c12087ec2ab1fcd173dba6a0eb0c5b7d9fba
MD5 fb51fce2b8753ecbb8301708bda8feb4
BLAKE2b-256 45287a41e90e0b03847c376456c519f4982d37364dad847e0cc5fb0fadddce97

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