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.

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.2.tar.gz (18.6 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.2-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: java_coder-0.0.2.tar.gz
  • Upload date:
  • Size: 18.6 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.2.tar.gz
Algorithm Hash digest
SHA256 f8a20c288668e9c9fcfd6f67adf21b8befccc41515dd8d0052bc0e350e300feb
MD5 1b14dc80390e9631af804dd5780b9796
BLAKE2b-256 1f0af7c092e22461f3c69f19e154233524a07e39fb10cf405e136efea2afcbed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: java_coder-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 21.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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0609997ba8213154bd4135b16939c4ef968fa22fc904823feaebe592861a1d0f
MD5 29457c1f5b6ba8032375eae6259df4c0
BLAKE2b-256 9915fa60cfe2fd1604d6b483231e3056f0d21ace8e05f6e4132ee19c4c51c936

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