How to access the map returned by IParameterValues::getParameterValues()? - eclipse-plugin

I declared a command and a commandParameter for this command. I specified the "values" of this commandParameter as a class implemented by myself. The implementation of this class is below,
public class ParameterValues implements IParameterValues {
#Override
public Map<String, Double> getParameterValues() {
// TODO Auto-generated method stub
Map<String, Double> values = new HashMap<String, Double>(2);
values.put("testParam", 1.1239);
values.put("AnotherTest", 4.1239);
return values;
}
}
The implementation of the handler of this command is blow,
public class testHandler extends AbstractHandler implements IHandler {
private static String PARAMETER_ID = "my.parameter1";
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
String value = event.getParameter(PARAMETER_ID);
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
"Test", "Parameter ID: " + PARAMETER_ID + "\nValue: " + value);
return null;
}
}
Now, I contribute the command to a menu,
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="my.edit"
label="Edit">
<command
commandId="myCommand.test"
label="Test1">
<parameter
name="my.parameter1"
value="testParam">
</parameter>
</command>
Since I specified a "values" class for the commandParater, I expect when the menu is clicked, this code line "String value = event.getParameter(PARAMETER_ID);" in the handler class returns 1.1239 instead of "testParam".
But, I still see that code line returns "testParam".
What's the problem? How could I access the map returned by getParameterValues()?
By the way, following menu declaration still works even I don't define "ppp" in the map.
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="my.edit"
label="Edit">
<command
commandId="myCommand.test"
label="Test1">
<parameter
name="my.parameter1"
value="ppp">
</parameter>
</command>
Thanks!

From the javadoc, org.eclipse.core.commands.IParameterValues.getParameterValues() "Returns a map keyed by externalized names for parameter values. These names should be human-readable, and are generally for display to the user in a user interface of some sort. The values should be actual values that will be interpreted by the handler for the command."
This interface is used to provide translatable names for command parameters. So that "Show View (Package Explorer)" can be displayed in the Keys preference page instead of "Show View (org.eclipse.jdt.ui.PackageExplorer)"
But org.eclipse.ui.menus definitions must contain the parameter value as opposed to a translatable name.
You can define the parameter type and AbstractParameterValueConverter in the command definition if you want to process the string parameters and turn them into various objects.

Related

Finding the annotated method call as a parameter to Logger methods

Suppose I have below Person class with one getAccountNumber() method having #ShouldNotBeLogged custom annotation.
public class Person {
private String name;
private String accountNumber;
#ShouldNotBeLogged
public String getAccountNumber() {
return accountNumber;
}
}
Considering the above Person class I want to find the all occurrences where getAccountNumber() method is logged using the Logger class - error|warn|debug|info methods like in the below HelloWorld class logMessage method. Please note that there are many methods having ShouldNotBeLogged annotation in the actual code, so we cannot create search with name of the method in it.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorld {
private static final Logger logger = LogManager.getLogger(HelloWorld.class);
public void logMessage(Person person) {
logger.debug("logging Acc No. - {}", person.getAccountNumber()); // Occurence Type 1
LogManager.getLogger(HelloWorld.class).info("Account Number - {}", person.getAccountNumber()); // Occurence Type 2
}
}
I have tried using the Method call existing template and given the Text filter for $MethodCall$ with - error|warn|debug|info and it finds the Logger method with the error,warn,debug or, info names. Not able to create the filter for $Instance$ & $Parameter$ where Instance will be of Logger type (can be instantiated as a constant of a class or, directly with the Logger class method) and Parameter will be the call to #ShouldNotBeLogged annotated method.
$Instance$.$MethodCall$($Parameter$)
I'm using the IntelliJ IDEA 2022.2 (Ultimate Edition)
This is a difficult case. Hopefully this will help you.
First, create a search that will find #ShouldNotBeLogged annotated methods:
#ShouldNotBeLogged
$ReturnType$ $Method$($ParameterType$ $Parameter$);
With a count modifier [0,∞] on $Parameter$ (this template is based on the existing template "Deprecated methods"). Save this template under a name, for example "Methods that should not be logged".
$logger$.$call$($arg1$, $method$())
Modifiers:
$logger$: type=Logger
$method$: reference=Methods that should not be logged
This will find all logger calls with a call to a #ShouldNotBeLogged annotated method as the second argument.
Unfortunately, if you want to find logger calls with calls to a #ShouldNotBeLogged annotated method on a different position, you will have to construct a separate query template. For example:
$logger$.$call$($arg1$, $arg2$, $method$())

Can I use a bool type with XAML X:Static?

I am trying to make a menu item's visibility conditioned on the DEBUG macro. (It's a debug menu item and so should not be visible in a release build.) I am trying to follow the x:Static example in https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions
I have the following in my AppShell Class:
namespace myappname
{
public partial class AppShell : Xamarin.Forms.Shell
{
#if DEBUG
public static readonly bool IsDebug = true;
#else
public static readonly bool IsDebug = false;
#endif
In the AppShell XAML I have tried numerous variations of
<FlyoutItem Title="Debug" Icon="DebugCheckedTests_16x.png" IsVisible="{x:Static local:bool.IsDebug}">
But everything I have tried either gives me the error XFC0000 Cannot resolve type "bool" -- or some other error.
Am I way off base, or is bool not a valid type for x:Static? How do I code this?
From the docs you linked what you are trying to accomplish comes under "a public static field", thus you need to specify the class where the field IsDebug is defined, in the docs example the class name is AppConstants, it is not a matter of "bool" type you can use any type as long as it is the type expected by the property.
xmlns:localRoot="clr-namespace:myappname"
<FlyoutItem Title="Debug" Icon="DebugCheckedTests_16x.png"
IsVisible="{x:Static localRoot:AppShell.IsDebug}>

Binding keys using org.eclipse.ui.binding

I am trying to introduce a shortcut key(Ctrl+Shift+f) to our customized editor to format the content.
I've implememented the following changes.
Added changes to plugin xml by adding key extension with definition Id/schema/context.
Implemented Action by extending TextEditorAction class as below.
#Override
public void run() {
this.doOperation(ISourceViewer.FORMAT);
}
Implemented one Formatter class by implementing IContentFormatter.
Passed the above Formatter class to our cutsomized sourceVIewConfiguration (extends SourceViewerConfiguration) class by overriding getContentFormatter.
overrided createActions() API inside our customized editor class which extends TextEditor.
For some reason my shortcut key is not working. I put a debug point inside my action class and noticed the controller is not going there when i press on the shortcut key.
I also noticed that the newly created key is not displayed under preferences -> keys list.
Can somebody provide pointers or example to resolve the issue.
plugin.xml entries:
<key
commandId="com.language.javascripteditor.XJSFormatAction"
schemeId="myScheme"
sequence="M1+M2+z"/>
<scheme
id="myScheme"
name="myScheme">
</scheme>
Formatter class:
public class JavaScriptEditorFormatter implements IContentFormatter {
#Override
public void format(IDocument document, IRegion region) {
try {
String content =document.get(region.getOffset(), region.getLength());
String formatted = new JSBeautifier().js_beautify(content,null);
document.replace(region.getOffset(), region.getLength(), formatted);
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
#Override
public IFormattingStrategy getFormattingStrategy(String contentType) {
throw new UnsupportedOperationException();
}
}
Added a new property file for customized schema with the name plugin_customization.ini and with the content as below
org.eclipse.ui/KEY_CONFIGURATION_ID=myScheme
Command section inside plugin.xml
<command
defaultHandler="com.cisco.nm.workflowbuilder.language.javascripteditor.XJSFormatAction"
id="com.language.javascripteditor.XJSFormatAction"
name="%action.label.format.xjs">
</command>
Instead of a handler I have written an Action class. Please let me know if this approach does not work
1.Added contributor class to extension of the existing editor.
2.Created command with an id for format.eg: com.javascript.text.format
3.written Action class with format method
#Override
public void run() {
this.doOperation(ISourceViewer.FORMAT);
}
4. plugin xml entry
<key
commandId="com.javascript.text.format"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+M2+F"/>
5. Overriden the createActions() .Inside this method instantiated the Action class and setActionDefinitionId.

how to hide property page from some resource type

my application project contains a different type of resources. for some IFolder not all folders in the project I need to set property page on right click properties.
my problem is that this page is shown for all folders even the property tester returns false.
plugin.xml
<extension
point="org.eclipse.ui.propertyPages">
<page
class="com.my.ui.properties.PropertiesPage"
id="com.my.ui.properties.Properties"
name="page">
<enabledWhen>
<adapt
type="org.eclipse.core.resources.IFolder">
<test
property="com.my.ui.propertyTester.supperFolder">
</test>
</adapt></enabledWhen>
</page>
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.my.ui.properties.FolderTester"
id="com.my.ui.propertyTester"
namespace="com.my.ui.propertyTester"
properties="supperFolder"
type="org.eclipse.core.resources.IResource">
</propertyTester>
public class FolderTester extends PropertyTester {
#Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if ("supperFolder".equals(property) && receiver instanceof IFolder) {
IFolder folder = (IFolder) receiver;
if (IWResourceManager.getInstance().isIWResource(folder)) {
..
..
return true;
}
}
}
return false;
}
So what am I missing?
after hours of debugging, i finally found the mistake, I fill like I need to share it, in case someone has the same problem...
so..
in property tester extension point, tester "id" and "namespace" had the same value and that's was creating some sort of conflict.

AOP - Injecting a property with a dynamically computed value

(or "Using LocationInterceptionAspect and IInstanceScopedAspect together")
Using Postsharp I'm trying to inject a property into a target class using 'IntroduceMember' and then using the 'OnGetValue' functionality of LocationInterceptionAspect dynamically give it a value on inspection.
Originally I thought that I'd need two separate aspects, one for the field injection and one for the location interception but managed to combine the two by implementing the IInstanceScopedAspect interface and inheriting from LocationInterceptionAspect.
The problem is that if I set a breakpoint I will see the property that's been injected, but if I set another breakpoint in the OnGetValue method (that gets fired for each property on the class) I can't see it...
Here's some sample code:
[Serializable]
class DALDecoratorWrapper : LocationInterceptionAspect, IInstanceScopedAspect
{
public override void OnGetValue(LocationInterceptionArgs args)
{
if (args.LocationName == "Type")
{
args.Value = "computed value here";
}
args.ProceedGetValue();
}
[IntroduceMember(OverrideAction = MemberOverrideAction.OverrideOrFail)]
public String Type { get; set; }
I was also hoping there was a better way of doing this than overriding OnGetValue as that's called for each getter where really I want to only target the getter of the property that's been injected
Cheers