Skip to content
JonathanxD edited this page Apr 19, 2017 · 7 revisions

Outdated

Static imports used:

import static com.github.jonathanxd.codeapi.CodeAPI.*;
import static com.github.jonathanxd.codeapi.literal.Literals.*;
import static com.github.jonathanxd.codeapi.PredefinedTypes.*;
import static java.lang.reflect.Modifier.*;

Basics

Simple class

//PUBLIC - Public class (Modifier).
//com.MyClass - Qualified Class Name.
TypeDeclaration decl = aClass(PUBLIC, "com.MyClass");
Fields
// Create source code of class
CodeSource source = sourceOfParts(
  // PRIVATE | FINAL - private final field (modifier)
  // STRING - Field value type
  // myField - Field name
  // Literal - JVM Literal
  //   Literals.STRING - JVM String literal.
  //     Hello - Literal value
  field(PRIVATE | FINAL, STRING, "myField", Literals.STRING("Hello"))
);
TypeDeclaration decl = aClass(PUBLIC, "com.MyClass", aClass -> source);
Methods
// Create source of code class
CodeSource source = sourceOfParts(
        // Create a public method with name "test" and return type void
        method(PUBLIC, "test", VOID, method ->
                // Source of the mthod
                sourceOfParts(
                        // Print "Hello World" using predefined method invocation
                        Predefined.invokePrintlnStr(STRING("Hello world"))
                ))
);

TypeDeclaration decl = aClass(PUBLIC, "com.MyClass", aClass -> source);
Method Parameters
// Create a public method with name "test", return type void and a "name" parameter of String type.
method(PUBLIC, "test", VOID, parameters(parameter(String.class, "name")), method ->
        sourceOfParts(
                // ...
        )
)

Declaring variables

method(PUBLIC, "test", VOID, parameters(parameter(String.class, "name")), method ->
        sourceOfParts(
                // Declare a variable named "variable" of type String
                field(String.class, "variable")
                // A variable with default value
                // field(String.class, "variable", defaultValue)
        )
)

Accessing Variables and Fields

// Access a local variable named "variable" of type String
accessLocalVariable(String.class, "variable")
// Access field "out" of type PrintStream in class System
accessStaticField(System.class, PrintStream.class, "out")
// Access a instance field:
// accessField(Example.cass, exampleInstance, "example")

Invoke method

Invocation types:

Simplified

  • Invoke Virtual: Invoke a method of a class
  • Invoke Special: Invoke a initialization method.
  • Invoke Interface: Invoke a method of an interface.
  • Invoke Static: Invoke a static method.
  • Invoke Dynamic: Invoke a method dynamically Advanced.
// Invoke "println" method of PrintStream class.
invokeVirtual(
    // Localization of the method
    PrintStream.class,
    // Gets the PrintStream instance from System class
    accessStaticField(System.class, PrintStream.class, "out"),
    // Method name
    "println", 
    // Method return type (VOID) and parameters types (STRING)
    typeSpec(VOID, STRING),
    // Argument to pass to method ("Hello" String)
    argument(STRING("Hello"))
)
Clone this wiki locally