question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a reason to use getFields instead of getDeclaredField in BTreeParser

See original GitHub issue

Issue details

On my way to try the framework I struggled to get the @TaskAttribute annotation to work. I always got the following exception : throw stackedTaskException(getCurrentTask(), “unknown attribute '” + name + “'”);

The issue was that I set my field as private and in the BehaviorTreeParser the findMetadata method() use the method ClassReflection.getField which only return the public field.

Is there a reason for that ? Wouldn’t it be more intuitive to use ClassReflection.getDeclaredField method instead ? If not I think we should update the documentation to notify this behaviour.

Reproduction steps/code

Create a simple LeaftTask with a private field annotated with @TaskAttribute, set the TaskAttribute in the .btree file and try to parse it. You will get a unknow attribute error.

Version of gdx-ai and/or relevant dependencies

1.8.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
exertionrivercommented, Mar 13, 2022

As of Mar 2022, the unexpected behavior described in this issue still appears for LeafTasks in Kotlin when annotated as documented for Java.

What seems to have worked for me was adding @JvmField annotation before the @TaskAttribute annotation in the LeafTask class as so:

@JvmField
@TaskAttribute
var doing : String? = null

Also important appears to be how one creates the tree. I was unable to get attribute data into the LeafTask class from the btree using the following tree parsing approach (from GdxDemo3d):

// Create behavior tree through the library
BehaviorTreeLibraryManager btlm = BehaviorTreeLibraryManager.getInstance();
this.tree = btlm.createBehaviorTree("btrees/dog.btree", this);

However, the following tree parsing approach (inspired by Benoit Dumas’s Alchemist project) populates the LeafTask attribute as I would expect:

var tree : BehaviorTree<KoboldCharacter>

init {
    val reader = FileReader("android/assets/btree/kobold.btree")
    val parser = BehaviorTreeParser<KoboldCharacter>(BehaviorTreeParser.DEBUG_HIGH)
    tree = parser.parse(reader, this)
}

Hope this helps.

Thank you, Ian

1reaction
tommyettingercommented, Nov 25, 2021

@jhoukem Since using getDeclaredFields() won’t find inherited members but getFields() will, 117 was just reverted (117 breaks existing code that inherited fields, and existing code couldn’t have used private fields before and so still can’t). I’d like to be able to have this handle the Kotlin case mentioned above and the current inheritance use case, but it doesn’t seem straightforward. I’m not sure why you actually would need private fields in your behavior tree Tasks, except for Kotlin’s quirks which 117 wouldn’t be able to handle (since it would need to handle setters).

So there’s your reason, getFields() allows inheriting fields as long as they are public, and getDeclaredFields() does not allow inheriting fields.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between getFields and ... - Stack Overflow
I read that getDeclaredFields gives you access to all the fields of the class and that getFields only returns public fields. If this...
Read more >
Class (Java Platform SE 8 ) - Oracle Help Center
Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object. Field[], getDeclaredFields().
Read more >
Retrieve Fields from a Java Class Using Reflection - Baeldung
We'll achieve this by using the Class::getDeclaredFields method. As its name suggests, this returns all the declared fields of a class, ...
Read more >
Java Reflection: Class.getFields() and Class ...
getDeclaredFields(). Reflection in Java is not something used regularly in software development unless you work with frameworks or development ...
Read more >
Type.GetFields Method (System) - Microsoft Learn
When overridden in a derived class, searches for the fields defined for the current Type, using the specified binding constraints. GetFields(). Returns all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found