feat: use @javatypes defiend java type

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-06-22 10:44:17 +08:00
parent 5f0c3bbdd4
commit ef98c49e1d
18 changed files with 60 additions and 56 deletions

View File

@ -1,9 +1,12 @@
import { ConnectionFactoryAdapter } from "./connection" import { ConnectionFactoryAdapter } from "./connection"
import { RabbitTemplateAdapter } from "./template" import { RabbitTemplateAdapter } from "./template"
import { JSClass } from "@ccms/container"
export const RabbitAdmin = Java.type('org.springframework.amqp.rabbit.core.RabbitAdmin') export const RabbitAdmin = Java.type('org.springframework.amqp.rabbit.core.RabbitAdmin')
export class RabbitAdminAdapter { export class RabbitAdminAdapter {
@JSClass('org.springframework.amqp.rabbit.core.RabbitAdmin')
private RabbitAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin
private _Handler: org.springframework.amqp.rabbit.core.RabbitAdmin private _Handler: org.springframework.amqp.rabbit.core.RabbitAdmin
constructor(config: RabbitTemplateAdapter | org.springframework.amqp.rabbit.core.RabbitTemplate | ConnectionFactoryAdapter | org.springframework.amqp.rabbit.connection.ConnectionFactory) { constructor(config: RabbitTemplateAdapter | org.springframework.amqp.rabbit.core.RabbitTemplate | ConnectionFactoryAdapter | org.springframework.amqp.rabbit.connection.ConnectionFactory) {
this._Handler = new RabbitAdmin((config instanceof RabbitTemplateAdapter || config instanceof ConnectionFactoryAdapter) ? config.getHandler() : config) this._Handler = new RabbitAdmin((config instanceof RabbitTemplateAdapter || config instanceof ConnectionFactoryAdapter) ? config.getHandler() : config)

View File

@ -2,7 +2,7 @@ import { ConnectionFactoryAdapter, ConnectionFactory } from "./connection"
import { ChannelAwareMessageListenerAdapter, ChannelAwareMessageListener } from "./listener" import { ChannelAwareMessageListenerAdapter, ChannelAwareMessageListener } from "./listener"
export const SimpleMessageListenerContainer = org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer export const SimpleMessageListenerContainer = org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
export const AcknowledgeMode = org.springframework.amqp.core.AcknowledgeMode export const AcknowledgeMode: org.springframework.amqp.core.AcknowledgeMode = Java.type('org.springframework.amqp.core.AcknowledgeMode')
interface ContainerConfig { interface ContainerConfig {
connectionFactory: ConnectionFactoryAdapter | typeof ConnectionFactory connectionFactory: ConnectionFactoryAdapter | typeof ConnectionFactory
queueNames: string[] queueNames: string[]

View File

@ -1,8 +1,6 @@
import { JSClass } from "@ccms/container" import { JSClass } from "@ccms/container"
import { RabbitAdminAdapter, MessageListenerContainerAdapter, AcknowledgeMode, MessageHandler, ChannelAwareMessageListenerAdapter } from "./adapter" import { RabbitAdminAdapter, MessageListenerContainerAdapter, AcknowledgeMode, MessageHandler, ChannelAwareMessageListenerAdapter } from "./adapter"
const RabbitAdmin = Java.type('org.springframework.amqp.rabbit.core.RabbitAdmin')
export class AmqpAdmin { export class AmqpAdmin {
@JSClass('org.springframework.amqp.core.TopicExchange') @JSClass('org.springframework.amqp.core.TopicExchange')
private TopicExchange: typeof org.springframework.amqp.core.TopicExchange private TopicExchange: typeof org.springframework.amqp.core.TopicExchange
@ -10,14 +8,15 @@ export class AmqpAdmin {
private Queue: typeof org.springframework.amqp.core.Queue private Queue: typeof org.springframework.amqp.core.Queue
@JSClass('org.springframework.amqp.core.Binding') @JSClass('org.springframework.amqp.core.Binding')
private Binding: typeof org.springframework.amqp.core.Binding private Binding: typeof org.springframework.amqp.core.Binding
@JSClass('org.springframework.amqp.rabbit.core.RabbitAdmin')
private RabbitAdmin: typeof org.springframework.amqp.rabbit.core.RabbitAdmin
@JSClass('org.springframework.amqp.core.Binding.DestinationType') @JSClass('org.springframework.amqp.core.Binding.DestinationType')
private DestinationType: typeof org.springframework.amqp.core.Binding.DestinationType private DestinationType: org.springframework.amqp.core.Binding.DestinationType
private amqpAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin private amqpAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin
constructor(amqpAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin | any) { constructor(amqpAdmin: org.springframework.amqp.rabbit.core.RabbitAdmin | any) {
if (amqpAdmin instanceof RabbitAdmin) { if (amqpAdmin instanceof this.RabbitAdmin) {
this.amqpAdmin = amqpAdmin this.amqpAdmin = amqpAdmin
} else if (amqpAdmin instanceof RabbitAdminAdapter) { } else if (amqpAdmin instanceof RabbitAdminAdapter) {
this.amqpAdmin = amqpAdmin.getHandler() this.amqpAdmin = amqpAdmin.getHandler()

View File

@ -1,8 +1,8 @@
/// <reference types="@ccms/nashorn" /> /// <reference types="@ccms/nashorn" />
/// <reference types="@ccms/types/dist/typings/jdk" /> /// <reference types="@javatypes/jdk" />
/// <reference types="@ccms/types/dist/typings/spring" /> /// <reference types="@javatypes/spring-amqp" />
/// <reference types="@ccms/types/dist/typings/spring/amqp" /> /// <reference types="@javatypes/spring-rabbit" />
/// <reference types="@ccms/types/dist/typings/rabbitmq" /> /// <reference types="@javatypes/amqp-client" />
import { amqp } from '@ccms/api' import { amqp } from '@ccms/api'
import { getContainer, reduceMetadata } from '@ccms/container' import { getContainer, reduceMetadata } from '@ccms/container'

View File

@ -1,4 +1,4 @@
/// <reference types="@ccms/types/dist/typings/bukkit" /> /// <reference types="@javatypes/spigot-api" />
import { server } from '@ccms/api' import { server } from '@ccms/api'
import { Container } from '@ccms/container' import { Container } from '@ccms/container'

View File

@ -1,4 +1,4 @@
/// <reference types="@ccms/types/dist/typings/bungee" /> /// <reference types="@javatypes/bungee-api" />
import { server } from '@ccms/api' import { server } from '@ccms/api'
import { Container } from '@ccms/container' import { Container } from '@ccms/container'

View File

@ -24,7 +24,8 @@
"typescript": "^3.9.2" "typescript": "^3.9.2"
}, },
"dependencies": { "dependencies": {
"@ccms/nashorn": "^0.7.0" "@ccms/nashorn": "^0.7.0",
"@javatypes/jdk": "^1.14.0"
}, },
"gitHead": "562e2d00175c9d3a99c8b672aa07e6d92706a027" "gitHead": "562e2d00175c9d3a99c8b672aa07e6d92706a027"
} }

View File

@ -1,4 +1,4 @@
/// <reference types="@ccms/types/dist/typings/jdk" /> /// <reference types="@javatypes/jdk" />
/** /**
* *
@ -11,7 +11,7 @@ const methodCache = []
class Reflect { class Reflect {
private obj: java.lang.Object private obj: java.lang.Object
private class: java.lang.Class private class: java.lang.Class<any>
constructor(obj: any) { constructor(obj: any) {
// if (obj === undefined || obj === null) { throw Error(`reflect object can't be ${obj}!`) } // if (obj === undefined || obj === null) { throw Error(`reflect object can't be ${obj}!`) }
@ -26,8 +26,8 @@ class Reflect {
} }
} }
method(name: string, ...args: any[]) { method(name: string, ...args: java.lang.Class<any>[]) {
return declaredMethod(this.class, name, args) return declaredMethod(this.class, name, ...args)
} }
methods() { methods() {
@ -90,7 +90,7 @@ function types(values: any[], def?: any) {
if (values === null) { if (values === null) {
return [] return []
} }
let result: java.lang.Class[] = [] let result: java.lang.Class<any>[] = []
values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class)) values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class))
return result return result
} }
@ -119,7 +119,7 @@ function declaredConstructor(clazz, param) {
return accessible(constructor) return accessible(constructor)
} }
function declaredField(clazz: java.lang.Class, name: string | java.lang.String) { function declaredField(clazz: java.lang.Class<any>, name: string | java.lang.String) {
if (!clazz) { throw Error(`target class can't be ${clazz}!`) } if (!clazz) { throw Error(`target class can't be ${clazz}!`) }
let target = clazz let target = clazz
let field = null let field = null
@ -139,17 +139,17 @@ function declaredField(clazz: java.lang.Class, name: string | java.lang.String)
return field return field
} }
function declaredMethod(clazz: java.lang.Class, name: string, ...clazzs: any[]): java.lang.reflect.Method { function declaredMethod(clazz: java.lang.Class<any>, name: string, ...clazzs: java.lang.Class<any>[]): java.lang.reflect.Method {
let key = clazz.getName() + '.' + name + ':' + (clazzs || []).join(':') let key = clazz.getName() + '.' + name + ':' + (clazzs || []).join(':')
if (!methodCache[key]) { if (!methodCache[key]) {
try { try {
methodCache[key] = clazz.getMethod(name, clazzs as any) methodCache[key] = clazz.getMethod(name, ...clazzs)
} catch (ex) { } catch (ex) {
try { try {
methodCache[key] = clazz.getDeclaredMethod(name, clazzs as any) methodCache[key] = clazz.getDeclaredMethod(name, clazzs as any)
} catch (ex) { } catch (ex) {
for (const m of Java.from(declaredMethods(clazz))) { for (const m of Java.from(declaredMethods(clazz))) {
if (m.name == name) { if (m.getName() == name) {
methodCache[key] = m methodCache[key] = m
break break
} }
@ -160,8 +160,8 @@ function declaredMethod(clazz: java.lang.Class, name: string, ...clazzs: any[]):
return methodCache[key] return methodCache[key]
} }
function declaredMethods(clazz) { function declaredMethods(clazz: java.lang.Class<any>) {
return clazz.declaredMethods return clazz.getDeclaredMethods()
} }
let classMethodsCache: any[] = [] let classMethodsCache: any[] = []

View File

@ -1,5 +1,3 @@
/// <reference types="@ccms/types/dist/typings/spring" />
import { Model } from './model' import { Model } from './model'
const HikariDataSource = Java.type('com.zaxxer.hikari.HikariDataSource') const HikariDataSource = Java.type('com.zaxxer.hikari.HikariDataSource')
@ -69,7 +67,7 @@ export class DataBase {
*/ */
query<T>(sql: string, ...args: any[]): Array<T> { query<T>(sql: string, ...args: any[]): Array<T> {
let startTime = Date.now() let startTime = Date.now()
let result = Java.from(this.jdbcTemplate.queryForList(sql, args)) let result = Java.from<any>(this.jdbcTemplate.queryForList(sql, args))
console.debug(java.lang.String.format(`\n[DB] query \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args)) console.debug(java.lang.String.format(`\n[DB] query \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
return result return result
} }

View File

@ -1,5 +1,6 @@
/// <reference types="@ccms/nashorn" /> /// <reference types="@ccms/nashorn" />
/// <reference types="@ccms/types/dist/typings/jdk/index" /> /// <reference types="@javatypes/jdk" />
/// <reference types="@javatypes/spring-jdbc" />
export * from './database' export * from './database'
export * from './manager' export * from './manager'

View File

@ -13,8 +13,8 @@ declare global {
namespace Java { namespace Java {
function type<T = any>(clazz: string): T function type<T = any>(clazz: string): T
function from<T>(javaObj: T[]): T[] function from<T = any>(javaObj: T[]): T[]
function to<T>(array: T[], type?: T): T[] function to<T = any>(array: T[], type?: T): T[]
function extend(...parentTypes: any[]): any function extend(...parentTypes: any[]): any
function synchronized(func: () => void, lock: any): Function function synchronized(func: () => void, lock: any): Function
function asJSONCompatible<T = any>(obj: T): T function asJSONCompatible<T = any>(obj: T): T

View File

@ -1,4 +1,4 @@
/// <reference types="@ccms/types/dist/typings/nukkit" /> /// <reference types="@javatypes/nukkit-api" />
import { server } from '@ccms/api' import { server } from '@ccms/api'
import { Container } from '@ccms/container' import { Container } from '@ccms/container'

View File

@ -1,9 +1,8 @@
/// <reference types="@ccms/types" /> /// <reference types="@javatypes/tomcat" />
/// <reference types="@ccms/types/dist/typings/tomcat/index" /> /// <reference types="@javatypes/spring-web" />
/// <reference types="@ccms/types/dist/typings/spring/index" />
import { constants, database, plugin, web } from "@ccms/api" import { constants, database, plugin, web } from "@ccms/api"
import { inject, ContainerInstance, Container } from "@ccms/container" import { inject, ContainerInstance, Container, JSClass } from "@ccms/container"
import { JSPlugin, interfaces, cmd } from "@ccms/plugin" import { JSPlugin, interfaces, cmd } from "@ccms/plugin"
import { DataBase, DataBaseManager } from '@ccms/database' import { DataBase, DataBaseManager } from '@ccms/database'
import { Server, Context, RequestHandler } from '@ccms/web' import { Server, Context, RequestHandler } from '@ccms/web'
@ -22,6 +21,9 @@ export class MiaoSpring extends interfaces.Plugin {
@inject(web.Server) @inject(web.Server)
private webServer: Server private webServer: Server
@JSClass('org.springframework.http.HttpStatus')
private HttpStatus: org.springframework.http.HttpStatus
private ResponseEntity = org.springframework.http.ResponseEntity private ResponseEntity = org.springframework.http.ResponseEntity
private mainDatabase: DataBase private mainDatabase: DataBase
@ -56,7 +58,7 @@ export class MiaoSpring extends interfaces.Plugin {
preHandle: (ctx: Context) => { preHandle: (ctx: Context) => {
const index = foundMap.indexOf(ctx.request.getRequestURI()) const index = foundMap.indexOf(ctx.request.getRequestURI())
if (index != -1) { if (index != -1) {
return this.ResponseEntity.status(org.springframework.http.HttpStatus.FOUND).header('Location', foundMap[index + 1]).build() return this.ResponseEntity.status(this.HttpStatus.FOUND).header('Location', foundMap[index + 1]).build()
} }
} }
}) })

View File

@ -1,6 +1,3 @@
/// <reference types="@ccms/types/dist/typings/rabbitmq" />
/// <reference types="@ccms/types/dist/typings/spring/amqp" />
import { constants, plugin as pluginApi, amqp, server, web } from '@ccms/api' import { constants, plugin as pluginApi, amqp, server, web } from '@ccms/api'
import { plugin, interfaces, cmd } from '@ccms/plugin' import { plugin, interfaces, cmd } from '@ccms/plugin'
import { AmqpAdmin, ConnectionFactoryAdapter, AmqpManager } from '@ccms/amqp' import { AmqpAdmin, ConnectionFactoryAdapter, AmqpManager } from '@ccms/amqp'

View File

@ -1,4 +1,4 @@
/// <reference types="@ccms/types/dist/typings/sponge" /> /// <reference types="@javatypes/sponge-api" />
import { server } from '@ccms/api' import { server } from '@ccms/api'
import { Container } from '@ccms/container' import { Container } from '@ccms/container'

View File

@ -1,18 +1,18 @@
import { server, constants } from '@ccms/api' import { server, constants } from '@ccms/api'
import { provideSingleton } from '@ccms/container'; import { provideSingleton } from '@ccms/container'
import * as reflect from '@ccms/common/dist/reflect' import * as reflect from '@ccms/common/dist/reflect'
const Sponge = org.spongepowered.api.Sponge; const Sponge = org.spongepowered.api.Sponge
const TextSerializers = org.spongepowered.api.text.serializer.TextSerializers; const TextSerializers = org.spongepowered.api.text.serializer.TextSerializers
const File = Java.type("java.io.File"); const File = Java.type("java.io.File")
@provideSingleton(server.Server) @provideSingleton(server.Server)
export class SpongeServer extends server.ReflectServer { export class SpongeServer extends server.ReflectServer {
private pluginsFolder: string; private pluginsFolder: string
constructor() { constructor() {
super(); super()
this.pluginsFolder = new File(base.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile().getCanonicalPath() this.pluginsFolder = new File(base.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile().getCanonicalPath()
} }
@ -35,25 +35,25 @@ export class SpongeServer extends server.ReflectServer {
if (typeof sender === 'string') { if (typeof sender === 'string') {
sender = this.getPlayer(sender) sender = this.getPlayer(sender)
} }
return Sponge.getCommandManager().process(sender, command).getQueryResult().get() return Sponge.getCommandManager().process(sender, command).getQueryResult().get() != 0
} }
dispatchConsoleCommand(command: string): boolean { dispatchConsoleCommand(command: string): boolean {
return Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command).getQueryResult().get() return Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command).getQueryResult().get() != 0
} }
getPluginsFolder(): string { getPluginsFolder(): string {
return this.pluginsFolder; return this.pluginsFolder
} }
getNativePluginManager() { getNativePluginManager() {
return Sponge.getPluginManager() as any; return Sponge.getPluginManager() as any
} }
getDedicatedServer() { getDedicatedServer() {
return reflect.on(Sponge.getServer()).get() return reflect.on(Sponge.getServer()).get()
} }
getNettyPipeline() { getNettyPipeline() {
return this.pipeline; return this.pipeline
} }
getRootLogger() { getRootLogger() {
return this.rootLogger; return this.rootLogger
} }
sendJson(sender: string | any, json: string): void { sendJson(sender: string | any, json: string): void {
if (typeof sender === "string") { if (typeof sender === "string") {

View File

@ -24,6 +24,7 @@
"typescript": "^3.9.2" "typescript": "^3.9.2"
}, },
"dependencies": { "dependencies": {
"@ccms/api": "^0.7.0",
"@ccms/container": "^0.7.0" "@ccms/container": "^0.7.0"
} }
} }

View File

@ -1,7 +1,9 @@
/// <reference types="@ccms/types/dist/typings/jdk/index" /> /// <reference types="@ccms/nashorn" />
/// <reference types="@ccms/types/dist/typings/tomcat/index" /> /// <reference types="@javatypes/jdk" />
/// <reference types="@ccms/types/dist/typings/spring/index" /> /// <reference types="@javatypes/tomcat" />
/// <reference types="@ccms/types/dist/typings/spring/beans/index" /> /// <reference types="@javatypes/spring-web" />
/// <reference types="@javatypes/spring-core" />
/// <reference types="@javatypes/spring-beans" />
export * from './server' export * from './server'
export * from './decorators' export * from './decorators'