class A{
int x = 10;
}
This is A.java
I want to get NewA.java
class NewA{
int x = 10;
Sting text = "B";
}
I want to add a variable using javaparser.
You need to do this:
Parse the code
Find the point where you want to add your element
Add the element you want
Dump back the code
The first point is trivial, just use the JavaParser.parse method. You will get a CompilationUnit. In the example you shown you are adding a field in a class declaration, so you need first to get that class declaration. Call getTypes and look into that list for the declaration you want or just call getClassByName.
Once you have your class declaration you can call addMember on it. In your example you are adding a field so you need to instantiate a FieldDeclaration.
Once you are done you take your CompilationUnit and call toString. You will get back the modified source code.
Source: I am a JavaParser committer
Related
If I have a public method, I can call it inside its class using both $.name and self.name:
class TEST {
has Int $.a;
method b($x) {
return $!a * $x;
}
method c($y) {
return self.b($y) * 3; # or $.b($y)
}
}
my $m = TEST.new(a => 10);
say $m.c(2); # 60
But if I make b a private method, I only can call it with self!b, not $!b, otherwise I get the following error message:
Attribute $!b not declared in class TEST
What's behind this rule? What are the rules of calling a method inside its own class?
An attribute can always be referred to as $!foo in a class. If you do that, than the code will be generated to directly access the attribute itself, and any classes subclassing your class will not be able to change this behaviour.
If you use has $.foo in the declaration of a class, it means that a public accessor (and if you add is rw it can also function as a mutator).
When you use $.foo in your code otherwise, it is exactly the same as $( self.foo ). This means that it will call the method foo on self, and itemize the return value (make it a single "thing" if it wasn't yet). This will go wrong if you defined your attribute with $!foo and you did not supply a method foo yourself.
This goes even further: $.bar really means self.bar: you only need to have a method existing by the name bar, which may not be related to any attribute at all.
If you define a private method !baz, the ! just indicates the privacy of the method, which means you need to call it indeed as self!baz. There is no short syntax for it.
Personally I dislike the fact that you can say $.zippo even if zippo is not an attribute. But I'm afraid that ship has sailed. But this behaviour is now causing you confusion :-(
So what's behind the rule for not having a short syntax for calling a private method? Not sure, I guess really that $!foo was already taken to mean direct access to the attribute, and provide you with a compile time error if the attribute doesn't exist.
Hope this answers your question!
I am completely new to C++\CLI and I have the following problem: I have a class called, for example, MyClass, a few values of which I need to store in memory. I have decided to use List, because it is the most familiar to me, since I was using C# for a long time. So here goes the code:
//Header File
ref class MyClass
{
public:
MyClass(void);
private:
System::Collections::Generic::List<MyClass^> values;
};
//CPP file
MyClass::MyClass(void){
this->values=gcnew System::Collections::Generic::List<MyClass^>();
}
The compiler keeps on saying that there's an error C2582. But if there's no assignment operator, then how should I initialize the List?
Thanks in advance.
values should not be a List<...^>, but a List<...^>^. You want to assign a reference, not to create another clone of the list.
I have coded the following, and am very new to c++, and it feels clumsy. I am trying to give 'spriteBatch' (a unique_Ptr) class scope. Here's the header file:
ref class CubeRenderer : public Direct3DBase
{
public:
CubeRenderer();
~CubeRenderer();
private:
std::unique_ptr<SpriteBatch> spriteBatch;
};
Then in the cpp file Constructor, this:
std::unique_ptr<SpriteBatch> sb(new SpriteBatch(m_d3dContext.Get()));
spriteBatch = std::move(sb);
It just seems clumsy the way I had to create 'sb' and move it to 'spriteBatch'. attempting to assign directly to 'spriteBatch' failed (maybe I simply don't know the proper syntax). Is there a way to avoid needing to use 'sb' & std::move?
Thank you.
The following should work fine:
spriteBatch = std::unique_ptr<SpriteBatch>(new SpriteBatch(m_d3dContext.Get()));
Alternatively, you can avoid repeating the type name with some make_unique function.
spriteBatch = make_unique<SpriteBatch>(m_d3dContext.Get());
There's also the reset member:
spriteBatch.reset(new SpriteBatch(m_d3dContext.Get()));
But, since you mention a constructor, why not just use the member initialization list?
CubeRenderer::CubeRenderer()
: spriteBatch(new SpriteBatch(m_d3dContext.Get())) {}
Given a structure, is there a way to create a class in MATLAB? Take for instance
>> p = struct(); p.x = 0; p.y = 0;
>> p
p =
x: 0
y: 0
>> name = 'Point'
name =
Point
What I would like to do, is given a string containing the name of the class and a struct with containing the fields I would like to create a class without having to write a file explicitly writing the definition.
Right now if we use class(p) we will obtain struct. What I want to do is create an object of the type Point so that when I do class(obj) then I get Point.
Any ideas how to accomplish this besides writing a file in MATLAB with the class definition and then executing it?
Either you have specific functionality (methods) associated with the Point class as opposed to e.g. the Line class, in which case you should write out the classes by hand, anyway, or you can make a single dynamicprops class that can have dynamically created properties, and unless you really need to call a method named class, you much simplify your life by calling classname instead.
classdef myDynamicClass < dynamicprops
properties (Hidden)
myClass %# this stores the class name
end
methods
function obj = myDynamicClass(myClassName,varargin)
%# synopsis: obj = myDynamicClass(myClassName,propertyName,propertyValue,...)
%# myClassName is the name of the class that is returned by 'classname(obj)'
%# propertyName/propertyValue define the dynamic properties
obj.myClass = myClassName;
for i=1:2:length(varargin)
addprop(obj,varargin{i})
obj.(varargin{i}) = varargin{i+1};
end
end
function out = classname(obj)
out = obj.myClass;
end
end
end
I don't know of any way of creating objects dynamically, so I'd say the answer to your question is no. However, to solve your problem, I would propose something very similar to what Mikhail said:
Work with a struct with fields x, y and classname:
p.x=0;
p.y=0;
p.classname='Point';
and then write a function myclass(x) which returns x.classname. If for some reason you need to use class() you could even overload it with your own function which checks if x is one of your special structs and calls builtin('class', x) otherwise:
function out=class(varargin)
if nargin==1 && isstruct(varargin{1}) ... #check if we were given a struct
&& isfield(varargin{1}, 'classname') ... #...which contains a field classname
&& ischar(varargin{1}.classname) %# ... which is a string
out=varargin{1}.classname; %# ok, our special case :-)
else
out=builtin('class',varargin{:}); %# normal case - call builtin class()
end
One solution that I've used in the past is to write a MATLAB function that takes this information (i.e. the class name and fields) and writes an M-file containing the required classdef construct.
This works well if you're using this information to describe a prototype that you want to expand later.
i have an object which represents a database table. I want to iterate through this object and print printing each value. What can i use to do this?
i want to do this inside my mxml not actionscript
for each object attribute i want to create an imput field
Look up the documentation on Flex 3 looping. If you do, you'll find this:
for..in
The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties may appear in a seemingly random order):
var myObj:Object = {x:20, y:30};
for (var i:String in myObj)
{
trace(i + ": " + myObj[i]);
}
// output:
// x: 20
// y: 30
Instead of trying to create an input field for each object, I'd suggest you take a look at DataGrid and custom ItemEditors.
I agree that this answer isn't useful. It only works with generic objects, not user declared
objects.
However, here's some code that should/could work using the describeType as suggested above. (And I don't really think it's too complex). Be aware that only public properties/methods, etc. are exposed:
var ct:CustomObject = new CustomObject();
var xml:XML = describeType(ct);
for each(var accessor in xml..accessor) {
var name:String = accessor.#name;
var type.String = accessor.#type;
trace(ct[name]);
}
The problem with "for...in" is that it iterates only on dynamic properties. That is, if your object is defined as a Class (and not dynamically), "for..in" won't give anything.
The ActionScript documentation suggest to use describeType() for fixed properties, but it looks over-complicated for this simple taskā¦
You can write it like actionscript but include it inside the mxml file with the script tag:
<mx:Script>
<![CDATA[
public function LoopAndPrint() : void
{
//your code here
}
]]>
</mx:Script>