-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annotations #977
Annotations #977
Changes from 11 commits
8bb0e06
861ac63
82fdef1
605c3f9
39f22de
f047cc8
9d15a71
505efe6
12ec7d5
bb74cf3
af7502e
bed6940
080e5f8
c30a7c9
14c7ec2
d261dc4
8b944e5
d515475
9b38e25
b33484b
5de2169
b24e5b5
8b0d893
c92516e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
import org.lflang.lf.Action; | ||
import org.lflang.lf.ActionOrigin; | ||
import org.lflang.lf.Assignment; | ||
import org.lflang.lf.Attribute; | ||
import org.lflang.lf.Code; | ||
import org.lflang.lf.Connection; | ||
import org.lflang.lf.Element; | ||
|
@@ -1747,16 +1748,73 @@ public static String findAnnotationInComments(EObject object, String key) { | |
return null; | ||
} | ||
|
||
/** | ||
* Return the value of the {@code @label} attribute if | ||
* present, otherwise return null. | ||
* | ||
* @throws IllegalArgumentException If the node cannot have attributes | ||
*/ | ||
public static String findLabelAttribute(EObject node) { | ||
List<Attribute> attrs = getAttributes(node); | ||
return attrs.stream() | ||
.filter(it -> it.getAttrName().equals("label")) | ||
.map(it -> it.getAttrParms().get(0).getValue()) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
} | ||
|
||
/** | ||
* Return the attributes declared on the given node. Throws | ||
* if the node does not support declaring attributes. | ||
* | ||
* @throws IllegalArgumentException If the node cannot have attributes | ||
*/ | ||
public static List<Attribute> getAttributes(EObject node) { | ||
if (node instanceof Reactor) { | ||
return ((Reactor) node).getAttributes(); | ||
} else if (node instanceof Reaction) { | ||
return ((Reaction) node).getAttributes(); | ||
} else if (node instanceof Action) { | ||
return ((Action) node).getAttributes(); | ||
} else if (node instanceof Timer) { | ||
return ((Timer) node).getAttributes(); | ||
} else if (node instanceof StateVar) { | ||
return ((StateVar) node).getAttributes(); | ||
} else if (node instanceof Parameter) { | ||
return ((Parameter) node).getAttributes(); | ||
} else if (node instanceof Input) { | ||
return ((Input) node).getAttributes(); | ||
} else if (node instanceof Output) { | ||
return ((Output) node).getAttributes(); | ||
} | ||
throw new IllegalArgumentException("Not annotatable: " + node); | ||
} | ||
|
||
/** | ||
* Search for an `@label` annotation for a given reaction. | ||
* | ||
* | ||
* @param n the reaction for which the label should be searched | ||
* @return The annotated string if an `@label` annotation was found. `null` otherwise. | ||
*/ | ||
public static String label(Reaction n) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that this overload could be removed if binary compatibility is not important, which seems likely. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I didn't get this comment. What binary compatibility? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone has compiled a class that depends on this method and we remove it, their class will break. Minor releases generally preserve binary compatibility. I'm not sure whether this java API is publicly supported since you've started public releases... If the only ide extensions that use the java API are in this repo then we can most likely remove it (we're also pre-1.0.0) |
||
return findAnnotationInComments(n, "@label"); | ||
return label((EObject) n); | ||
} | ||
|
||
|
||
/** | ||
* Return the declared label of the node, as given by the @label | ||
* annotation (or an @label comment). | ||
* | ||
* @throws IllegalArgumentException If the node cannot have attributes | ||
*/ | ||
public static String label(EObject n) { | ||
String fromAttr = findLabelAttribute(n); | ||
if (fromAttr == null) { | ||
return findAnnotationInComments(n, "@label"); | ||
} | ||
return fromAttr; | ||
} | ||
|
||
/** | ||
* Find the main reactor and set its name if none was defined. | ||
* @param resource The resource to find the main reactor in. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense if this was in a separate class like
AttributeUtils
? Same for other stuff here that relate to attributes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I like this idea.