Java¶
Code in Java is a class with static Object main(G g). It is compiled once by the compiler of the JDK, and runs on the image of the JDK of Eclipse Temurin (eclipse-temurin:25-jdk unless the settings name another version), with only the packages you add from Maven Central.
In short
- Write a class with
static Object main(G g). - It is compiled once, and runs on
eclipse-temurin:25-jdkunless the settings name another version. - Refuse a request with
throw new AmErrors(message, 400).
| hello.java | |
|---|---|
| Runtime | JDK 20 or later, a version of eclipse-temurin:<version>-jdk : 25 (default), 21 |
main |
a static method main(G g) of any class : it returns Object, any other type, or is void |
| Class | class Api in a new item, not public : its file is named after the item (orders.java), and Java wants a public class in a file of its name. Every piece of code is compiled on its own, so each can have its own Api, in a sandbox and in git |
| Answer | what main returns, written as JSON; null lets the API go on |
Calls of g.sys |
typed : what a call takes is a class made key after key (new GetAll("mysql_8", "inventory", "customers").limit(10)), what it answers has its type (List<JsonObject>, JsonObject, long, UpdateManyResult) |
| Values | plain JSON : an object is an am.JsonObject (a Map<String, Object> which reads its keys by their types : getString, getLong, getObject…), then List<Object>, String, Long or Double, Boolean, null |
| Imports | am.G, am.GetAll, am.JsonObject, am.AmErrors… : import what the code names, as in any Java file |
| Packages | Maven Central : com.google.code.gson:gson 2.11.0 |
| Printing | System.out.println goes to the console of the account |
The request and the answer¶
g.req and g.res have a getter and a setter for each key they are known to have : g.req.getBody(), g.req.getQuery(), g.req.getParams() and g.req.getHeaders() give the objects of the request, g.res.setStatusCode(201) sets the status of the answer. Each object is a JsonObject, which reads a key by its type without a cast : g.req.getBody().getString("name") is null when the key is not there, getString("name", "world") gives a value for that case.
A body which is a list ([{…}, {…}], several rows to save) is read with g.req.getList("body") ; g.req.getBody() is an empty JsonObject then.
JsonObject.of("key", value, …)makes an object from keys and values in turn (a value can benull) ;Map.of(…)and any otherMapare objects too. AJsonObjectreads a key as what you expect :getString,getLong,getInt,getDouble,getBoolean(0,falseor the value you give when something else is there),getObject(an empty one when none is there),getList,getObjects,getStrings, andat("address", "phones", 0, "number")for a path.- A whole number of the request is a
Long, any other number aDouble: read numbers withgetLongandgetDouble, or as aNumber, not with a cast toInteger. - A
Map, aList, an array (abyte[]is bytes : see below), a record (its components), an object of a class of yours (its fields), anOptional, an enum (its name) : all are answers.NaNand infinities arenull; ajava.util.Dateand the types ofjava.timeare texts (2026-01-02T03:04:05.678Z). - What you change of
g.reqin a pre hook is what the API sees.
Bytes as the answer¶
A file, a picture, a PDF : return its bytes (a byte[]), or give them to g.res.setOutput, and name their kind of content. The answer is those bytes as they are, whatever the kind of content, with the headers of the code. (A text under a kind of bytes, image/gif, is read as base64.)
Calling API Maker : g.sys¶
Each method is a call of the server, with its types. What it takes is a class of am made with what it is about, then key after key (GetAll, GetById, Count…) ; what it answers is typed too : rows are a List<JsonObject>, a row a JsonObject (null when there is none), a count a long, what an API describes a class with a getter for each key (UpdateManyResult). The thread of the request waits for the answer; the other requests go on.
- A key the class has no method for is set as it is :
new GetAll("mysql_8", "inventory", "customers").with("cursor", "abc"). A class is made of a map too :new GetAll(g.req.getBody()). - The generated APIs are under
g.sys.db.gen(getAllGen…). The whole answer of a call (isSuccess(),getStatusCode(),getData(),getTotalCount(),getErrors()) :g.sys.db.full.getAll(…), anApiResponse<List<JsonObject>>.
Calls at the same time are threads. Virtual threads (JDK 21 and later) cost next to nothing :
maincan saythrows Exception. A call which fails in a thread comes out ofget(): its errors are the errors of the answer.- On JDK 20 use
Executors.newCachedThreadPool(): virtual threads are part of the JDK from 21 on.
Rows as they arrive¶
| Sum a large table | |
|---|---|
The function of the rows is an am.Rows (row -> …), and each row a JsonObject. A lambda of Java does not assign a local variable : keep the sum in an array of one, or in a DoubleAdder. What the function throws ends the call.
System APIs and cache¶
Errors¶
| Refuse a request | |
|---|---|
AmErrorsis aRuntimeException: nothing has to be declared.refused.errorsis the list of the errors (each aResponseError:getMessage(),getCode(),getField()…),refused.codethe code of the first one. Several errors at once :throw new AmErrors(List.of(Map.of("code", 400, "message", "…"), …)).- Any other exception answers
500with its type and message, the message of what it happened because of, and the lines of your code :at Api.helper(orders.java:7).
Logs, printing and shared values¶
Hooks¶
| Pre hook : only my rows | |
|---|---|
| Post hook : hide a column | |
|---|---|
Utility classes¶
A utility class written in Java is a class of its name. The utility classes of Java of an account are compiled together, and the other Java code of the account names what they declare, without an import.
public class Tax {
public static final double RATE = 0.2;
public static double add(double amount) {
return Round.cents(amount * (1 + RATE)); // Round : another utility class
}
}
import am.G;
import java.util.Map;
class Api {
static Object main(G g) {
return Map.of("total", Tax.add(g.req.getBody().getDouble("amount")));
}
}
- The class has the name of the utility class (
public class Taxof the classTax) : letters of a to z, digits and_, no digit first, no word of Java (record,class…) and notam. One class of a name per account, whatever its folder : the classes are in no package, so write nopackageline. - The classes are loaded once per runner and shared by all Java code of the account there : what a class keeps in a static field is kept there. A class which is saved again is compiled anew with the others, and the code which uses them is compiled again on its next run.
- A class of the code comes before a utility class of the same name : every piece of code keeps its own
Api. - What does not compile in a class is said with its file and its line :
src/utility_classes/utils---Tax/Tax.java:3: error: cannot find symbol…. - In the Git repository the class is
src/utility_classes/<folder>---<name>/<name>.java: the project of Gradle compiles the classes together, then every piece of code with them.
Test cases¶
A test case written in Java has a method per test : every static method whose name starts with test and which takes G g, in any class. What it throws is its failure.
import am.G;
import am.JsonObject;
import am.Mock;
import am.RunCustomApi;
import java.util.List;
import java.util.Map;
class ShippingTests {
static void testTheCostOfAParcel(G g) {
g.sys.test.mock(new Mock("db.getAll").when(Map.of("collection", "rates")).returns(List.of(Map.of("per_kg", 2.5))));
JsonObject output = JsonObject.from(g.sys.test.runCustomApi(new RunCustomApi("shippingCost").body(Map.of("weight", 4))));
if (output.getDouble("cost") != 10) throw new IllegalStateException("the cost is " + output.get("cost"));
}
}
g.sys.test:runCustomApi(new RunCustomApi(…)),runCustomApiFull(…)for the whole answer (getData(),getStatusCode(),getHeaders(),getLogs()),mock(new Mock(…)),mocks(List<Mock>),clearMocks()orclearMocks(method),calls()orcalls(method).- The tests run in the order they are written. The custom API under test can be written in any language.
- Throw to fail : the
assertof Java is checked only by a JVM started with-ea, and the runner is not. - The lines of a test run are counted on JDK 24 and later. On an older JDK the tests run as well, and the coverage has no lines of Java.
Packages¶
Add com.google.code.gson:gson in Sandbox Settings, then :
| A package of Maven Central | |
|---|---|
- A package is named by its group and its artifact :
group:artifact. Versions are the ones of Maven :2.11.0,33.4.0-jre, a range[1.0,2.0); empty is the latest release. - Maven resolves the packages, with what they need, when the image is built; while code runs nothing is downloaded. Code which imports what is not there is told so :
'com.google.gson' is not there : is its package in the packages of Java in the settings of the sandbox?. - Kotlin has packages of its own, from Maven Central too : a package added to Java is not there for Kotlin code.
How it runs¶
- Each piece of code is compiled once, with its lines : stack traces say them. Saving starts that compilation in the background, so the first request does not wait for it, and code which does not compile is reported in the console of the account (
main.java:3: error: ';' expected). - Every request runs on a thread of its own; requests run at the same time, on every core.
- Static fields of your code live as long as the runner, per piece of code : protect them (
AtomicLong,synchronized), or useg.shared. - Each piece of code has a class loader of its own : two pieces of code never see each other's classes, only the utility classes of the account.
In the code editor¶
The editor completes g as Java writes it, the names of your instances, databases and tables, and the columns of a table on the rows a call answers. More in the code editor in every language.
- After
new GetAll(…).the editor offers the methods which set its keys, each with what it is for ; a method gives the same value, so the next key follows. Inside.find(JsonObject.of("(orMap.of(") it offers the columns of the table, inside.select("and.sort("too. - What a call answers has its type : on the rows of a
getAllthe columns are offered inside the quotes ofgetString,getLong,getDouble,getObject… and ofget(row.get("), in aforloop and in a lambda (rows.forEach(row -> …)).changed.(anupdateMany) offersgetUpdatedRowsCount,saved.offersrow,rowsandisList, the whole answer ofg.sys.db.fulloffersisSuccess,getData,getErrors… g.req.andg.res.offer a getter and a setter for each key of the request and of the answer ;g.req.getAuth().the opened tokens, writteng.req.getAuth().get("authAMUser").- A value your code says is an object has its keys and what a
JsonObjecthas :JsonObject.from(value).and((JsonObject) value)..
Reference¶
g. |
Java |
|---|---|
req |
am.Request : getBody(), getQuery(), getParams(), getHeaders(), getAuth(), getReqInfo() (each a JsonObject), getEventData(), isApiRequestFromUser(), and a setter for each ; a JsonObject itself |
res |
am.Response : setStatusCode, setContentType, getHeaders(), getShared(), getOutput(), setOutput, getErrors(), getWarnings() ; a JsonObject itself |
sys.db |
getAll(GetAll), getById(GetById), query(Query), saveSingleOrMultiple(Save), masterSave(Save), arrayOperations(ArrayOperations), updateById(UpdateById), updateMany(UpdateMany), replaceById(UpdateById), removeById(RemoveById), removeByQuery(RemoveByQuery), aggregate(Aggregate), count(Count), distinct(Distinct), distinctQuery(Distinct), getAllByStream, queryByStream ; sys.db.gen.getAllGen… |
sys.db.full, sys.db.gen.full, sys.system.full, sys.cache.full |
the same calls, each with its whole answer : ApiResponse<T> (isSuccess(), getStatusCode(), getData(), getTotalCount(), getErrors(), getWarnings()) |
sys.system |
encrypt, encryptWith, decrypt, decryptWith, hash, getSecret, getSecrets, callExternalApi, callExternalApis, getToken, getTokens, executeQuery, getTableMeta, getTableMetas, createIndexes, dropIndexes, dropIndexesOfMany, getIndexes, getIndexesOfMany, emitEvent, emitEventWS, isValidDataForTable(s), isValidDataForCustomAPI(s), multiTenantInstance(s)Updated, isValidConnectionString(s), getLock, releaseLock |
sys.cache |
getKey, getKeys, setKey, setKeys, removeKey, removeKeys, resetCacheDB, resetCacheDBs, resetCacheCustomApis, resetCacheCustomApisOfTenant, resetCacheSystemApis, resetCacheSystemApisOfTenant |
sys.call("db.getAll", …) |
any API by its name in JavaScript, with its arguments as they are |
JsonObject |
of, from, getString, getLong, getInt, getDouble, getBoolean, getObject, objectAt, getList, getObjects, getStrings, has, with, at |
logger |
log, debug, info, warn, error |
shared |
a ConcurrentHashMap<String, Object> of the runner |
Json |
stringify, pretty, parse, and the paths of any value : get, text, number, integer, truth, object, array |
| errors | throw new AmErrors(message, code) or new AmErrors(list); .errors (a List<ResponseError>), .code |
Debugging¶
- The debug runner of Java speaks JDWP with the server : the Debug tab of the API testing page shows where the code stopped.
- Every piece of code of Java is a class
Api: the debugger tells the classes of each file apart. - The debugger evaluates variables, fields, items of arrays and operators : it calls no method.
- Learn more Debugging.