Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use annotation in Java classes to use them as adapters or functions from within Navascript #615

Open
aschoneveld opened this issue Nov 14, 2021 · 3 comments
Labels
enhancement New feature or impovement of existing one.

Comments

@aschoneveld
Copy link
Member

aschoneveld commented Nov 14, 2021

Currently you need to edit an XML file in order to use java classes as adapters.

For example the following Java class:

public class Adder {
     public int operandA;
     public int operandB;
     public int result;
     public String operator = "+";
     public boolean flag;
     public ComplexField complexField;
     public ComplexField [] complexArrayField;

     public void setCalculate(boolean b) {
          if ( "+".equals(operator) ( {
             result = operandA + operandB;
          } else if ("-".equals(operator) ) {
             result = operandA - operandB;
          }
     }
}

If you want to use this class, in a convenient way from Navascript you have to add the following XML:

<map>
		<tagname>adder</tagname>
		<object>Adder</object>
		<methods>
			<method name="plus">
				<param name="left" field="operandA" type="integer" required="true" />
				<param name="right" field="operandB" type="integer" required="true" />
				<param name="value" field="calculate" type="boolean" required="automatic" value="true" />
			</method>
                </methods>
</map>

With annotations this could be circumvented. Something like this:

@NavajoAdapter(map='adder')
public class Adder {
     public int operandA;
     public int operandB;
     public int result;
     public String operator = "+";
     public boolean flag;
     public ComplexField complexField;
     public ComplexField [] complexArrayField;

     @NavajoMethod(name='plus')   // This assumes that this "setter" is called last as param with required='automatic'
     @MethodParam(name='left',field='operandA',required=true)
     @MethodParam(name='right',field='operandB',required=true)
     public void setCalculate(boolean b) {
          if ( "+".equals(operator) ( {
             result = operandA + operandB;
          } else if ("-".equals(operator) ) {
             result = operandA - operandB;
          }
     }
}

This would allow to use this 'adapter' in a NS3 script as follows:

map.adder {
   .plus(left=4,right=2);
   message "Result" {
      property "Answer" = $result;
   }
}
@aschoneveld aschoneveld added the enhancement New feature or impovement of existing one. label Nov 14, 2021
@aschoneveld
Copy link
Member Author

@roelofkemp @kharybdys @gballintijn : wat denken jullie van bovenstaande idee?

@kharybdys
Copy link
Contributor

Ik roep al een poos dat het makkelijker moet worden om Adapters en Functions toe te voegen en een belangrijk onderdeel daarvan, in mijn ogen, is het verwijderen van alle ballast mbt discovery mechanisme, dus zeker de XML (die al over meerdere plekken verspreid staat anyways), en bij voorkeur ook de verplichting tot het subclassen van ExtensionDefinition. Hoe dat precies gebeurt is minder belangrijk maar annotaties zijn wel de eerst logische oplossing lijkt mij.
En het is voor mij helemaal akkoord om het alleen binnen NS3 te laten werken (en backwards compatibility te schrappen)

@kharybdys
Copy link
Contributor

Ik zat nog te bedenken dat als we inderdaad iets met annotations gaan doen, het mij logisch lijkt dat methodes en klassevariabelen die NIET geannoteerd zijn dan dus ook niet benaderbaar zijn vanuit webservices.

@Dexels Dexels deleted a comment from aschoneveld Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or impovement of existing one.
Projects
None yet
Development

No branches or pull requests

2 participants