Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 311503

Summary: hasRole(Object, class) getRole(Object, class) by (Sub-)type
Product: [Tools] Objectteams Reporter: Jan Marc Hoffmann <exelnet>
Component: OTJAssignee: Stephan Herrmann <stephan.herrmann>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: stephan.herrmann
Version: 1.4   
Target Milestone: 0.7 M3   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Jan Marc Hoffmann CLA 2010-05-04 05:58:26 EDT
The current hasRole and getRole function only work on the exact dynamic type. It would be useful if they would also work on dynamic subtypes. This would make the methods equivalent to the regular java instanceof function.
Comment 1 Stephan Herrmann CLA 2010-05-04 06:41:53 EDT
To me this sounds like what we have, e.g., in this test case:

http://www.objectteams.org/distrib/jacks/1.4.0/results/9.2.2-otjld-has-role-method-6.html

This test case passes. What does it take to reproduce the problem?
Comment 2 Jan Marc Hoffmann CLA 2010-05-04 06:54:40 EDT
Reproduce:

public team class OuterTeam extends HasRoleFixTeam {
	
	public team class InnerTeam extends HasRoleFixTeam playedBy OuterBase {
	
		protected class SomeRole extends MyRoles playedBy InnerBase {
		}
		
		public void createRole(InnerBase as SomeRole b) {
		}	
		
		public boolean habRole(InnerBase b){
			boolean hasRole = InnerTeam.this.hasRole(b, MyRoles.class);
			System.out.println("hasRole: " + hasRole);
			return hasRole;
		}
	}
	
	public InnerTeam registerInnerTeam(OuterBase as InnerTeam o) {
		return o;
	}
}

Call:

public class Main {

	public static void main(String[] args) {
		OuterBase ob = new OuterBase();
		InnerBase ib = new InnerBase();
		final OuterTeam ot = new OuterTeam();
		InnerTeam<@ot> it = ot.registerInnerTeam(ob);
		it.createRole(ib);
		it.habRole(ib);
	}
}

The difference might be that the test uses a hierarchy of roles. In my example its a regular class.
Comment 3 Stephan Herrmann CLA 2010-05-04 08:10:56 EDT
(In reply to comment #2)
> The difference might be that the test uses a hierarchy of roles. In my example
> its a regular class.

So it seems your (missing) class MyRoles is a regular class?
That won't work and in this case you should actually just use hasRole(Object).

Technical note: the second argument of hasRole(Object,Class) helps the runtime
to find the appropriate internal role cache for searching, thus this class
must be a role.

I'll leave this bug open for now, to remind me that the documentation
(OTJLD ยง6.1(a)) could be made more explicit about this constraint.
Comment 4 Stephan Herrmann CLA 2010-05-18 18:50:01 EDT
Documentation of reflective API has been clarified in r344 (source & OTJLD).
Comment 5 Stephan Herrmann CLA 2010-05-29 17:32:15 EDT
verified for M3 using 201005282024