Eclipse JDT static field modifiers - eclipse

I want to get the information about a field modifier. To be precise, I want to find out is the field a static one. For example, I want to examine the following code:
ASTParser parser = ASTParser.newParser(AST.JLS3);
How can I infer that the JLS3 is static field? I used getModifiers with Modifier.isStatic when analyzing methods and it worked fine. However, now I am not able to get the information that JLS3 in above code snippet is a static field. Is there something I am missing?
EDIT:
This is the code that I am using:
private boolean visit(SimpleName name){
boolean isStatic = Modifier.isStatic(name.resolveTypeBinding().getModifiers());
...
return true;
}
isStatic is false in the case of JLS3.

That's obviously wrong, use name.resolveBinding() instead of name.resolveTypeBinding() --- so you should get an object of type IVariableBinding.
name.resolveTypeBinding() returns the binding for the type of the field, but not the binding of field itself, which is not what you want here.

Related

A way to read a String as dart code inside flutter?

I want to build a method to dynamically save attributes on a specific object
given the attribute name and the value to save I call the "save()" function to update the global targetObj
var targetObj = targetClass();
save(String attribute, String value){
targetObj.attribute = value;
print(targetObj.attribute);
}
But I'm getting the following error:
Class 'targetClass' has no instance setter 'attribute='.
Receiver: Instance of 'targetClass'
Tried calling: attribute="Foo"
The only thing that I can think of is that "attribute" due to being type String results in an error.
That lead me to think if there is a way to read a String as code, something like eval for php.
As #Randal mentioned, you cannot create class..method at runtime. Still, you can try something like this.
A certain class
class Foo {
dynamic bar1;
dynamic bar2;
// ...
}
Your save method
save(Foo fooObject, String attribute, dynamic value) {
if ("bar1" == attribute) fooObject.bar1 = value;
else if ("bar2" == attribute) fooObject.bar2 == value;
// ...
}
Dart (and thus flutter) does not have a way to compile and execute code at runtime (other than dart:mirrors, which is deprecated). You can build additional code that derives from other code using the various builder mechanisms, although it can be rather complicated to implement (and use!).

Annotate getter from the property

In kotlin, we can define
var text: String
which will have a getter and a setter. It's easy to remove the setter by changing it to val, but what if we have a property that should only have the setter and no getter? Is there any way to create an annotation that we can apply to text that will deprecate the getter so others cannot use it?
The end goal is to be able to use the property syntax rather than call setText
Current workaround I am aware of that achieves a similar result:
var text: String
#Deprecated(level = DeprecationLevel.ERROR, message = "Non readable property")
get() = nonReadable()
set() ...
I would like to know if it's possible to define something similar to
#SetterOnly
var text: String
set() ...
I can tell you that there is no way to complete this feature in kotlin now. since the bug KT-3110 is not marked as Fixed.
I have test the following java code in kotlin at below, it only can access setter via setText:
public class Property {
public void setText(String text) {/**/}
private String getText() { return "foo"; }
}
which means you can't remove the getter/make the getter's visibility down. so it is impossible to get rid of the getter like as kotlin-allopen plugin. so one possible solution you can do is write your own kapt plugin to throws a compile-time error, for example:
#SetOnly var text:String = "foo";
If you don't want this property to be used externally, why dont you declare it as
private var text :String =""

method based on variable type

I just have the following scenario
i want to return string from method but the method should be based on variable type which is (Type CType)
i need to make the render class like this
public string render(TextBox ctype){
return "its text box";
}
public string render(DropDown ctype){
return "its drop down";
}
you know TextBox is a Type thats why i can declare the Type variable like this
var CType = typeof(TextBox)
and i need to call the render method like this
render(Ctype);
so if the Ctype is type of TextBox it should call the render(TextBox ctype)
and so on
How can i make it ?
you should use a template function
public customRender<T>(T ctype)
{
if(ctype is TextBox){
//render textbox
}
else if(ctype is DropDown){
//render dropdown
}
}
hope it will help
First of all, even if you don't see an if or a switch, there will still be one somewhere hidden inside some functions. Distinguishing types at runtime that are not known at compile-time simply will not be possible without any such kind of branching of the control flow.
You can use one of the collection classes to build a map at runtime that maps Type instances to Func<T, TResult> methods. For example, you can use the Dictionary type to create such a map:
var rendererFuncs = new Dictionary<Type, Func<object, string>>();
You could then add some entries to that dictionary like this:
rendererFuncs[typeof(TextBox)] = ctype => "its text box";
rendererFuncs[typeof(DropDown)] = ctype => "its drop down";
Later on, you can call the appropriate function like this:
string renderedValue = rendererFuncs[Ctype.GetType()](Ctype);
Or, if you want to be on the safe side (in case there are Ctype values that have no appropriate renderer):
string renderedValue;
Func<object, string> renderer;
if (rendererFuncs.TryGetValue(Ctype.GetType(), out renderer)) {
renderedValue = renderer(Ctype);
} else {
renderedValue = "(no renderer found)";
}
Note that this will only work for as long as Ctype is of the exact type used as a key in the dictionary; if you want any subtypes to be correctly recognized as well, drop the dictionary and build your own map that traverses the inheritance hierarchy of the type being searched (by using the Type.BaseType property).

How to fetch value from custom multifield component?

I have created a multifield custom widget having two fields with names ./urlLink and ./urlText.
Now i m trying to fetch the values from widget into the component's jsp with following code
String property = properties.get("./urlLink",String[].class);
for(String value: property ) {
out.print(value);
}
out.print(property);
But i am not able to get its value instead i m getting error.
If you're getting a property and it contains a string value, you need to use the method getString() - that way when you have the property, you can set the string to the value by doing something like this:
Property property = properties.get("./urlLink",String.class);
String value = property.getString();
Just a side note, if your return is supposed to be a string array, your type that you're putting the values in should be a string array.
String[] value
Check out the documentation on day.com for Properties and getting the values inside them.
Looks like a typo: you don't prefix a property name with .\ when accessing it.
My guess is you got a NullPointerException, right? That's because there's no ./urlLink property in the value map (properties). You should check against that anyway (so that it's not thrown on a fresh page with no content).
If that doesn't help -- double check that you have the properties in the content (call your page with .xml or .infinite.json extensions, and then double check if you can read them as plain strings (you should be able to -- CRX does some magic, smart type conversions).
It's good to register custom xtype as :
// registering the custom widget with the name dualfield
CQ.Ext.reg("dualfield", CQ.Ext.form.DualField);
Then u can easily fetch the value as :
String[] data = properties.get("multi",String[].class);
Here multi is the name of widget having multifield as xtype

JDT ASTParser to get the value of a string field

Is there a way to use jdt ASTParser to get the value of a String field declared in a java file. Actually what I need is to resolve any possible dependencies from other classes e.g.
public String str = "somethig"+SomeTherClass.SOMETHING_ELSE.
I figured it out ...it was quite simple actually ..here's the code:
ICompilationUnit cu = ....
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(cu);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
ASTNode node = parser.createAST(null);
node.accept(new YourVisitor());
Then in your implementation of the ASTVisitor you need to call resolveConstantExpressionValue() on the node being visited.