MetamodelToken.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package de.jplag.emf;
  2. import java.util.Optional;
  3. import org.eclipse.emf.ecore.EObject;
  4. import de.jplag.Token;
  5. import de.jplag.TokenConstants;
  6. /**
  7. * EMF metamodel token.
  8. * @author Timur Saglam
  9. */
  10. public class MetamodelToken extends Token implements MetamodelTokenConstants {
  11. private final Optional<EObject> eObject;
  12. /**
  13. * Creates an Ecore metamodel token that corresponds to an EObject.
  14. * @param type is the corresponding ID of the {@link TokenConstants}.
  15. * @param file is the name of the source model file.
  16. * @param eObject is the corresponding eObject in the model from which this token was extracted.
  17. */
  18. public MetamodelToken(int type, String file, EObject eObject) {
  19. super(type, file, -1);
  20. this.eObject = Optional.of(eObject);
  21. }
  22. /**
  23. * Creates an Ecore metamodel token.
  24. * @param type is the corresponding ID of the {@link TokenConstants}.
  25. * @param file is the name of the source model file.
  26. */
  27. public MetamodelToken(int type, String file) {
  28. super(type, file, -1);
  29. this.eObject = Optional.empty();
  30. }
  31. /**
  32. * @return the optional corresponding EObject of the token.
  33. */
  34. public Optional<EObject> getEObject() {
  35. return eObject;
  36. }
  37. @Override
  38. protected String type2string() {
  39. return switch (type) {
  40. case PACKAGE -> "EPackage";
  41. case ANNOTATION -> "EAnnotation";
  42. case CLASS -> "EClass";
  43. case DATATYPE -> "EDatatype";
  44. case ENUM -> "EEnum";
  45. case ENUM_LITERAL -> "EEnumLiteral";
  46. case OPERATION -> "EOperation";
  47. case REFERENCE -> "EReference";
  48. case ATTRIBUTE -> "EAttribute";
  49. case PARAMETER -> "EParameter";
  50. case INTERFACE -> "EClass (Interface)";
  51. case SUPER_TYPE -> "ESuperType";
  52. case ID_ATTRIBUTE -> "EAttribute (ID)";
  53. case CONTAINMENT -> "EReference (Containment)";
  54. case ABSTRACT_CLASS -> "EClass (Abstract)";
  55. case RETURN_TYPE -> "EClassifier (Return Type)";
  56. case THROWS_DECLARATION -> "EClassifier (Exception)";
  57. case FILE_END -> "(EOF)";
  58. default -> "<UNKNOWN" + type + ">";
  59. };
  60. }
  61. }