Skip to content

Commit

Permalink
Modify the way listeners are implemented (masesgroup#525)
Browse files Browse the repository at this point in the history
* Changed the way listeners are implemented

* Copyright is managed externally to JNetReflector

* Configuration update

* Implemented the way that interfaces are implemented while classes are extended

* Update copyright file

* Added ability to run on demand

* Added compilation of Java files

* Add exceptions on adapters
  • Loading branch information
masesdevelopers authored Sep 17, 2024
1 parent 6400fc4 commit 4935b77
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ on:
branches: '**'
paths-ignore:
- 'docs/**'

workflow_dispatch:

concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
cancel-in-progress: true
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/generateclasses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ jobs:
run: Remove-Item .\src\net\JNet\Generated\* -Recurse -Force -Exclude README.md

- name: Clear Java generated files
run: |
Remove-Item .\src\jvm\jnetcore\src\main\java\org\mases\jnet\generated\* -Recurse -Force -Exclude README.md
Remove-Item .\src\jvm\jnet\src\main\java\org\mases\jnet\generated\* -Recurse -Force -Exclude README.md
run: Remove-Item .\src\jvm\jnet\src\main\java\org\mases\jnet\generated\* -Recurse -Force -Exclude README.md

- run: dotnet build --no-incremental --configuration Release src\net\JNetReflector\JNetReflector.csproj

Expand All @@ -72,7 +70,15 @@ jobs:
env:
JCOBRIDGE_LicensePath: ${{ secrets.JCOBRIDGE_ONLINE }}

- name: Try compilation
- name: Try Java compilation
continue-on-error: true
run: mvn "-Djcobridgepath=../../../binReflector/net8.0/JCOBridge.jar" --file ./src/jvm/jnet/pom.xml --no-transfer-progress package
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

- name: Try C# compilation
continue-on-error: true
run: dotnet build --no-incremental --configuration Release /p:Platform="Any CPU" /p:NoWarn="0108%3B1030%3B0618" src\net\JNet.sln

Expand Down
1 change: 1 addition & 0 deletions src/configuration.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"CopyrightFile": "copyright.txt",
"RelativeDestinationCSharpClassPath": "net\\JNet\\Generated",
"RelativeDestinationJavaListenerPath": "jvm\\jnet\\src\\main\\java",
"JavaListenerBasePackage": "org.mases.jnet.generated",
Expand Down
17 changes: 17 additions & 0 deletions src/copyright.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 MASES s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Refer to LICENSE for more information.
*/
1 change: 1 addition & 0 deletions src/documentation/articles/usageReflector.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jnetreflector -OriginRootPath C:\\myJars -OriginJavadocUrl \"https://thehost/jav
_jnr_ accepts the following command-line switch:

* **ConfigurationFile**: The path where is stored a JSON file containing the tool configuration properties; the JSON items shall be written using the same command-line property names
* **CopyrightFile**: The path where the tool will locate copyright info
* **JavaPLocationPath**: The path where the tool will locate javap, default is to use system available
* **OriginRootPath**: The origin path where Jars to be analyzed, and dependencies, are stored
* **OriginJavadocUrl**: The base URL of the Javadoc to be associated to the classes
Expand Down
1 change: 1 addition & 0 deletions src/net/JNetReflector/InternalConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CLIParam : MASES.JNet.CLIParam
{
// ReflectorArgs
public const string ConfigurationFile = "ConfigurationFile";
public const string CopyrightFile = "CopyrightFile";
public const string JavaPLocationPath = "JavaPLocationPath";
public const string OriginRootPath = "OriginRootPath";
public const string OriginJavadocUrl = "OriginJavadocUrl";
Expand Down
43 changes: 28 additions & 15 deletions src/net/JNetReflector/InternalMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ static void AnalyzeSubItems(string package, IDictionary<string, IDictionary<stri
{
if (!_allPackages.ContainsKey(path))
{
var itemPackage = allPackageClasses.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
var itemPackage = allPackageClasses.Replace(AllPackageClasses.COPYRIGHT, JNetReflectorCore.CopyrightFileContent)
.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
.Replace(AllPackageClasses.JAR, jarOrModuleName)
.Replace(AllPackageClasses.NAMESPACE, package)
.Replace(AllPackageClasses.CLASSES, sb.ToString());
Expand All @@ -350,7 +351,8 @@ static void AnalyzeSubItems(string package, IDictionary<string, IDictionary<stri
}
else
{
var itemPackage = allPackageClasses.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
var itemPackage = allPackageClasses.Replace(AllPackageClasses.COPYRIGHT, JNetReflectorCore.CopyrightFileContent)
.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
.Replace(AllPackageClasses.JAR, jarOrModuleName)
.Replace(AllPackageClasses.NAMESPACE, package)
.Replace(AllPackageClasses.CLASSES, sb.ToString());
Expand Down Expand Up @@ -501,7 +503,8 @@ static string AnalyzeClasses(string jarOrModuleName, IEnumerable<Class> classDef
{
var singleClassFileTemplate = Template.GetTemplate(Template.SingleClassFileTemplate);

var fileContent = singleClassFileTemplate.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
var fileContent = singleClassFileTemplate.Replace(AllPackageClasses.COPYRIGHT, JNetReflectorCore.CopyrightFileContent)
.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
.Replace(AllPackageClasses.JAR, jarOrModuleName)
.Replace(AllPackageClasses.NAMESPACE, jClass.Namespace(JNetReflectorCore.UseCamel))
.Replace(AllPackageClasses.CLASSES, singleFileBlockStr);
Expand Down Expand Up @@ -832,32 +835,42 @@ static void PrepareSingleClass(this Class jClass, IReadOnlyDictionary<string, st

if (bPrepareJavaListener)
{
bool isInterfaceJavaListener = jClass.IsInterface();
var clsName = jClass.JVMListenerClassName();
var fullInterfaces = jClass.Name.Replace(SpecialNames.NestedClassSeparator, SpecialNames.NamespaceSeparator);

var javaClassMethodBlock = jClass.AnalyzeJavaMethods(fullInterfaces, isGeneric).AddTabLevel(1);
string singleJavaListenerTemplate = Template.GetTemplate(Template.SingleListenerJavaFileTemplate);

if (!jClass.IsInterface()) // it is a class, we have to implement the interfaces
{
StringBuilder sbInterfaces = new StringBuilder();
foreach (var item in jClass.Interfaces)
{
sbInterfaces.AppendFormat("{0}, ", item.Name.Replace(SpecialNames.NestedClassSeparator, SpecialNames.NamespaceSeparator));
}
string constructorExceptions = string.Empty;

fullInterfaces = sbInterfaces.ToString();
if (!string.IsNullOrWhiteSpace(fullInterfaces))
if (!isInterfaceJavaListener) // it is a class, we have to implement the interfaces
{
fullInterfaces = $"extends {fullInterfaces} ";
foreach (var ctor in jClass.Constructors)
{
fullInterfaces = fullInterfaces.Substring(0, fullInterfaces.LastIndexOf(", "));
if (ctor.ParameterCount == 0)
{
foreach (var exception in ctor.ExceptionTypes)
{
constructorExceptions += $", {exception.TypeName}";
}
}
}
}
else
{
fullInterfaces = $", {fullInterfaces}";
}

var singleJavaListenerStr = singleJavaListenerTemplate.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
var singleJavaListenerStr = singleJavaListenerTemplate.Replace(AllPackageClasses.COPYRIGHT, JNetReflectorCore.CopyrightFileContent)
.Replace(AllPackageClasses.VERSION, SpecialNames.VersionPlaceHolder())
.Replace(AllPackageClasses.PACKAGE, javaClassListenerPackage)
.Replace(AllPackageClasses.ClassStub.SIMPLECLASS, clsName)
.Replace(AllPackageClasses.ClassStub.JAVACLASS, fullInterfaces)
.Replace(AllPackageClasses.ClassStub.EXTEND_JAVACLASS, isInterfaceJavaListener ? string.Empty : fullInterfaces)
.Replace(AllPackageClasses.ClassStub.JAVACLASS, isInterfaceJavaListener ? fullInterfaces : string.Empty)
.Replace(AllPackageClasses.ClassStub.CLASS, jClass.JVMFullClassName())
.Replace(AllPackageClasses.ClassStub.EXTEND_EXCEPTIONS, constructorExceptions)
.Replace(AllPackageClasses.ClassStub.LISTENER_METHODS, javaClassMethodBlock);


Expand Down
25 changes: 25 additions & 0 deletions src/net/JNetReflector/JNetReflectorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime;
using System.Text.Json;

namespace MASES.JNetReflector
Expand Down Expand Up @@ -52,6 +53,8 @@ public VersionUrl(int version, string url)
public bool NoModule { get; set; }
}

public string CopyrightFile { get; set; }

public string JavaPLocationPath { get; set; }

public string OriginRootPath { get; set; }
Expand Down Expand Up @@ -151,6 +154,12 @@ public override IEnumerable<IArgumentMetadata> CommandLineArguments
Help = "The path where is stored a JSON file containing the tool configuration properties",
},
new ArgumentMetadata<string>()
{
Name = CLIParam.CopyrightFile,
Type = ArgumentType.Double,
Help = "The path where the tool will locate copyright info",
},
new ArgumentMetadata<string>()
{
Name = CLIParam.JavaPLocationPath,
Type = ArgumentType.Double,
Expand Down Expand Up @@ -418,6 +427,10 @@ public JNetReflectorCore()

static ConfigurationType _ConfigurationFromFile;

static string _CopyrightFile;
public static string CopyrightFile => _CopyrightFile ?? _ConfigurationFromFile.CopyrightFile;
public static string CopyrightFileContent = string.Empty;

static string _JavaPLocationPath;
public static string JavaPLocationPath => _JavaPLocationPath ?? _ConfigurationFromFile.JavaPLocationPath;

Expand Down Expand Up @@ -547,6 +560,8 @@ protected override string[] ProcessCommandLine()
{
var result = base.ProcessCommandLine();

_CopyrightFile = ParsedArgs.Get<string>(CLIParam.CopyrightFile);

_JavaPLocationPath = ParsedArgs.Get<string>(CLIParam.JavaPLocationPath);

if (ParsedArgs.Exist(CLIParam.ConfigurationFile))
Expand Down Expand Up @@ -745,6 +760,16 @@ protected override string[] ProcessCommandLine()
_TraceLevel = ParsedArgs.Get<int>(CLIParam.TraceLevel);
_TraceTo = ParsedArgs.Get<string>(CLIParam.TraceTo);

if (!string.IsNullOrWhiteSpace(CopyrightFile))
{
if (Path.IsPathRooted(CopyrightFile)) CopyrightFileContent = File.ReadAllText(CopyrightFile);
else
{
var path = Path.Combine(DestinationRootPath, CopyrightFile);
CopyrightFileContent = File.ReadAllText(path);
}
}

return result;
}

Expand Down
18 changes: 1 addition & 17 deletions src/net/JNetReflector/Templates/AllPackageClasses.template
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
/*
* Copyright 2024 MASES s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Refer to LICENSE for more information.
*/
ALLPACKAGE_COPYRIGHT_PLACEHOLDER

/*
* This file is generated by ALLPACKAGE_VERSION_PLACEHOLDER
Expand Down
18 changes: 1 addition & 17 deletions src/net/JNetReflector/Templates/SingleClassFile.template
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
/*
* Copyright 2024 MASES s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Refer to LICENSE for more information.
*/
ALLPACKAGE_COPYRIGHT_PLACEHOLDER

/*
* This file is generated by ALLPACKAGE_VERSION_PLACEHOLDER
Expand Down
66 changes: 46 additions & 20 deletions src/net/JNetReflector/Templates/SingleListenerJavaFile.template
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
/*
* Copyright 2024 MASES s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Refer to LICENSE for more information.
*/
ALLPACKAGE_COPYRIGHT_PLACEHOLDER

/*
* This file is generated by ALLPACKAGE_VERSION_PLACEHOLDER
*/

package ALLPACKAGE_PACKAGE_PLACEHOLDER;

public final class ALLPACKAGE_CLASSES_STUB_SIMPLECLASS_PLACEHOLDER extends org.mases.jcobridge.JCListener implements ALLPACKAGE_CLASSES_STUB_JAVACLASS_PLACEHOLDER {
public ALLPACKAGE_CLASSES_STUB_SIMPLECLASS_PLACEHOLDER(String key) throws org.mases.jcobridge.JCNativeException {
super(key);
public final class ALLPACKAGE_CLASSES_STUB_SIMPLECLASS_PLACEHOLDER ALLPACKAGE_CLASSES_STUB_EXTEND_JAVACLASS_PLACEHOLDERimplements org.mases.jcobridge.IJCListenerALLPACKAGE_CLASSES_STUB_JAVACLASS_PLACEHOLDER {
final org.mases.jcobridge.JCListener _internalListener;

public ALLPACKAGE_CLASSES_STUB_SIMPLECLASS_PLACEHOLDER(String key) throws org.mases.jcobridge.JCNativeExceptionALLPACKAGE_CLASSES_STUB_EXTEND_EXCEPTIONS_PLACEHOLDER {
_internalListener = new org.mases.jcobridge.JCListener(key);
}

public synchronized void release() {
_internalListener.release();
}

public synchronized void raiseEvent(String eventName) {
_internalListener.raiseEvent(eventName);
}

public synchronized void raiseEvent(String eventName, Object e) {
_internalListener.raiseEvent(eventName, e);
}

public synchronized void raiseEvent(String eventName, Object e, Object... objects) {
_internalListener.raiseEvent(eventName, e, objects);
}

public Object getEventData() {
return _internalListener.getEventData();
}

public boolean hasExtraData() {
return _internalListener.hasExtraData();
}

public int extraDataLength() {
return _internalListener.extraDataLength();
}

public Object[] extraData() {
return _internalListener.extraData();
}

public Object getReturnData() {
return _internalListener.getReturnData();
}

public void setReturnData(Object retData) {
_internalListener.setReturnData(retData);
}

// ALLPACKAGE_CLASSES_STUB_LISTENER_METHODS_PLACEHOLDER
Expand Down
3 changes: 3 additions & 0 deletions src/net/JNetReflector/Templates/Templates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public static string GetTemplate(string templateName)

public class AllPackageClasses
{
public const string COPYRIGHT = "ALLPACKAGE_COPYRIGHT_PLACEHOLDER";
public const string VERSION = "ALLPACKAGE_VERSION_PLACEHOLDER";
public const string JAR = "ALLPACKAGE_JAR_PLACEHOLDER";
public const string NAMESPACE = "ALLPACKAGE_NAMESPACE_PLACEHOLDER";
Expand Down Expand Up @@ -130,6 +131,8 @@ public class ClassStub
public const string BASECLASS = "ALLPACKAGE_CLASSES_STUB_BASECLASS_PLACEHOLDER";
public const string BASEINTERFACE = "ALLPACKAGE_CLASSES_STUB_BASEINTERFACE_PLACEHOLDER";
public const string WHERECLAUSES = "ALLPACKAGE_CLASSES_STUB_WHERECLAUSES_PLACEHOLDER";
public const string EXTEND_JAVACLASS = "ALLPACKAGE_CLASSES_STUB_EXTEND_JAVACLASS_PLACEHOLDER";
public const string EXTEND_EXCEPTIONS = "ALLPACKAGE_CLASSES_STUB_EXTEND_EXCEPTIONS_PLACEHOLDER";
public const string JAVACLASS = "ALLPACKAGE_CLASSES_STUB_JAVACLASS_PLACEHOLDER";
public const string JAVACLASS_DIRECT = "ALLPACKAGE_CLASSES_STUB_JAVACLASS_DIRECT_PLACEHOLDER";
public const string CONSTRUCTORS = "// ALLPACKAGE_CLASSES_STUB_CONSTRUCTORS_PLACEHOLDER";
Expand Down

0 comments on commit 4935b77

Please sign in to comment.