eclipse - Always view object when debugging - eclipse

I have an object in a Set that has it's guts updated by reference. Is there a way I can use the object hash ID (MyObject#93efa2ed) or Eclipse ID (id=356) to always watch in in my Expressions view even when it's out-of-scope?
I am using Eclipse Neon.

ur question is not quite clear to me, but i guess u mean this:
u have an object O that is contained in a set S, that contains more objects
O is updated in ur code (ie. its internal properties change) and u want to keep an eye on these changes thru the expressions view while u step thru the code
u also want this to happen in code lines/methods that dont a have a straitforward path to O
i dont know of any way (internal eclipse goody) that lets u ref' O by its eclipse debug ID or object hash.
but it can be done fairly simple: u just need to be able to reach O by some (lengthy) way of references from the current frame that is selected in the debugger to accomplish this.
the easiest way is to add some extra code to set O to some static var of some sort.
U can even assign the static var while debugging manually, with the help of the "Display" view that lets u execute java code, that runs in the context of the current stack frame.
Steps:
create/add the class MyWatcher to ur code base.
step thru ur code to where u get ur handle on O (eg. in the sample code, on the 5th break when adding o to the set)
open the Display view and add this line MyWatcher.watches.put("a", o) (replace 'o' by the expression that references O at ur breakpoint)
Execute the line with Ctrl+U or thru the context menu
add this to ur "expressions" view: MyWatcher.watches.get("a")
now O will remain visible at all times, eg. in the example below: even when u are in foo(), b/c the map holds ar ref on O.
public class MyCode {
public static void main(final String[] args) {
bar();
foo();
}
static void bar() {
final Set<Object> set = new HashSet<>();
for (int i = 0; i < 10; i++) {
final List<Integer> o = Arrays.asList(i);
set.add(o);
}
}
static void foo() {
System.out.println("bar");
}
}
public class MyWatcher {
public static final Map<String, Object> watches = new HashMap<>();
}

Add a toString() method in you MyObject class.
You can do it through Eclipse using:
right click(in MyObject) -> Source -> Generate toString().
Now you will be able to see the contents rather than hash id of MyObject.

Related

find variable Declaration reference Abstract syntax tree eclipse cdt C code

I have a c code like this
int x;
x = 5;
I used eclipse cdt to generate the AST, and traverse on it, so this is the code of the traversed class
public class RuleChk extends AbstractRule {
public RuleChk(IASTTranslationUnit ast) {
super("RuleChk", false, ast);
shouldVisitDeclarations = true;
shouldVisitParameterDeclarations = true;
}
#Override
public int visit(IASTSimpleDeclaration simpleDecl) {
//if this node has init, e.g: x = 5, do business
if(VisitorUtil.containNode(simpleDecl, CASTExpressionStatement){
// Now I have the x = 5 node,
// I want to get the reference node of it's declaration
// I mean (int x;) node
IASTNode declNode = ?????
}
return super.visit(parameterDeclaration);
}
}
what I want to visit the node that only has assignation(Initialization) and get the reference of declaration node for that varaiable.
I'm not sure how VisitorUtil works (it's not from the CDT code), but I assume it gives you a way to access the the found node. So:
Given the IASTExpressionStatement node that was found, use IASTExpression.getExpression() to get the contained expression.
See if it's an IASTBinaryExpression, and that is getOperator() is IASTBinaryExpression.op_assign.
Use IASTBinaryExpression.getOperand1() to get the assignment expression's left subexpression. Check that it's an IASTIdExpression, and get the variable it names via IASTIdExpression.getName().
Now that you have the name, use IASTName.resolveBinding() to get the variable's binding. This is the variable's representation in the semantic program model.
To find the variable's definition, use IASTTranslationUnit.getDefinitionsInAST(IBinding) if you only want it to look in the current file, or IASTTranslationUnit.getDefinitions(IBinding) if you want it to look in included header files as well (the latter requires the project to be indexed). The IASTTranslationUnit can be obtained from any IASTNode via IASTNode.getTranslationUnit().

How to copy a bsoncxx::builder::basic::document to another?

Is there any way to safely copy a bsoncxx document to another.
In following code I am not able to do that
class DocClass
{
private:
bsoncxx::builder::basic::document m_doc;
public:
bsoncxx::builder::basic::document& copy(bsoncxx::builder::basic::document& obj)
{
obj = m_doc; //Not allowed
//Error C2280 attempting to reference a deleted function
}
};
There should not be any harm to the object even after copy.
Please help.
Thanks,
Shibin
If you want to copy a bsoncxx::document::value, you can construct a new one from its view:
bsoncxx::document::value foo = ...;
bsoncxx::document::value bar{foo.view()};
bsoncxx::builder::basic::document is only movable, not copyable. However, you can get view to the underlying document from the builder with the view() method, which might be able to help you depending on your use cases. You'll still only be able to extract from the builder once though, so you'll have to rely on constructing a second document::value if you need more than one.

How to get the Result from previous activity?

I am new to WF 4.5.
The "GenerateResult" activity will generate a string in the Result property.
I want to assign the Result to the varExternal in the following Assign activity.
How to?
The GeneratedResult activity is defined as below.
public sealed class GenerateResult<TResult> : NativeActivity<TResult>
{
protected override void Execute(NativeActivityContext context)
{
this.Result.Set(context, "Hello, world!");
}
}
Just like you'd do it when programming. You have to hold the result within a variable, then reference that variable elsewhere.
I'm assuming you want to use the result in the WriteLine activity, so you would create a variable within the workflow (look at the bottom of the designer), bind it to the Result property of your GenerateResult activity (it's in the Property grid, so right-click and hit Properties). Then you can reference that variable in the WriteLine activity.

Unity3D. How to construct components programmatically

I'm new to unity and am having a bit of trouble getting my head around the architecture.
Lets say I have a C# script component called 'component A'.
I'd like component A to have an array of 100 other components of type 'component B'.
How do I construct the 'component B's programmatically with certain values?
Note that 'component B' is derived from 'MonoBehaviour' as it needs to be able to call 'StartCoroutine'
I'm aware that in unity you do not use constructors as you normally would in OO.
Note that 'component B' is derived from 'MonoBehaviour' as it needs to
be able to call 'StartCoroutine'
I'm aware that in unity you do not use constructors as you normally
would in OO.
That's true. A possibility is to istantiate components at runtime and provide a method to initialize them ( if initialization requires arguments, otherwise all initialization can be done inside Start or Awake methods ).
How do I construct the 'component B's programmatically with certain
values?
Here's a possible way:
public class BComponent : MonoBehavior
{
int id;
public void Init(int i)
{
id = i;
}
}
}
public class AComponent : MonoBehavior
{
private BComponent[] bs;
void Start()
{
bs = new BComponent[100];
for (int i=0; i < 100; ++i )
{
bs[i] = gameObject.AddComponent<BComponent>().Init(i);
}
}
}
Note that in the example above all components will be attached to the same GameObject, this might be not what you want. Eventually try to give more details.

Can I generate fields automatically in eclipse from a constructor?

When I'm coding in eclipse, I like to be as lazy as possible. So I frequently type something like:
myObject = new MyClass(myParam1, myParam2, myParam3);
Even though MyClass doesn't exist and neither does it's constructor. A few clicks later and eclipse has created MyClass with a constructor inferred from what I typed. My question is, is it possible to also get eclipse to generate fields in the class which correspond to what I passed to the constructor? I realize it's super lazy, but that's the whole joy of eclipse!
If you have a class A.
class A{
A(int a |){}
}
| is the cursor. Crtl + 1 "assign parameter to new field"
Result:
class A{
private final int a;
A(int a){
this.a = a;
}
}
This works also for methods:
void method(int b){}
Will result in:
private int b;
void method(int b){
this.b = b;
}
I know you can do the other way round. Define the fields and let Eclipse generate a constructor using these fields for you: Source | Generate Constructor using Fields
Since Eclipse Neon it is possible to assign all parameters to fields.
Using the quick assist Ctrl + 1 it suggest Assign all parameters to new fields. You can call for the quick assist if the cursor is anywhere between the parenthesis of the constructor.
This option is available for methods as well.
source