PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/OPAL/bi/src/test/fixtures-java/projects/type_annotations/RVTypeAnnotationUser.java

https://bitbucket.org/delors/opal
Java | 114 lines | 82 code | 21 blank | 11 comment | 2 complexity | 924f0748a9414abde304ceca71ffc02f MD5 | raw file
  1. /* BSD 2-Clause License - see OPAL/LICENSE for details. */
  2. package type_annotations;
  3. import java.io.Serializable;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.File;
  7. import java.util.function.Supplier;
  8. import java.util.function.Function;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Vector;
  12. /**
  13. * This class tests some (corner) cases related to type annotations.
  14. *
  15. * NOTE<br />
  16. * This class is only meant to be (automatically) compiled by OPAL's build script.
  17. *
  18. * @author Michael Eichberg
  19. */
  20. public abstract class RVTypeAnnotationUser<@RVTypeAnnotation T extends @RVTypeAnnotation Serializable & Cloneable>
  21. implements List<@RVTypeAnnotation Object>, @RVTypeAnnotation("superinterface") Serializable {
  22. public @RVTypeAnnotation("on nested class declaration") class Nested {
  23. public @RVTypeAnnotation("on nested class declaration") class SubNested {
  24. public @RVTypeAnnotation("on nested class declaration") class SubSubNested {
  25. }
  26. }
  27. }
  28. private static final Object process(Object o) {return o;} // just a helper method
  29. private static final @RVTypeAnnotation("field declaration") long serialVersionUID = 1L;
  30. public List<@RVTypeAnnotation("annotation of generic type parameter") T> ser;
  31. @SuppressWarnings("all")
  32. public Object onNestedTypeAnnotations() throws Exception {
  33. Nested nested = new RVTypeAnnotationUser.@RVTypeAnnotation("on nested class instantiation") Nested();
  34. Nested.SubNested subNested = nested.new @RVTypeAnnotation("on nested^2 class instantiation") SubNested();
  35. Nested.SubNested.SubSubNested subSubNested = subNested.new @RVTypeAnnotation("on nested^2 class instantiation") SubSubNested();
  36. return subSubNested;
  37. }
  38. public Object localVariableDeclarationTypeAnnotations() throws Exception {
  39. @RVTypeAnnotation("local variable annotation")
  40. List<@RVTypeAnnotation("type parameter annotation") T> l = new ArrayList<>();
  41. return l;
  42. }
  43. public void wildcardsRelatedTypeAnnotation() throws Exception {
  44. List<@RVTypeAnnotation ? extends Cloneable> l = new ArrayList<>();
  45. process(l);
  46. }
  47. @SuppressWarnings("unchecked")
  48. public void arrayRelatedTypeAnnotation() throws Exception {
  49. @RVTypeAnnotation("array as such") int[] @RVTypeAnnotation("2nd dimension of array")[] ls = new int[10][1];
  50. process(ls);
  51. List<@RVTypeAnnotation("type parameter annotation of array of generic type") Serializable>[] lgs = new List[10];
  52. process(lgs);
  53. }
  54. public Supplier<Vector<?>> instanceCreationRelatedTypeAnnotation() throws Exception {
  55. List<?> l = new @RVTypeAnnotation ArrayList<>();
  56. process(l);
  57. return (@RVTypeAnnotation Vector::new);
  58. }
  59. public Supplier<Object> methodCallRelatedTypeAnnotation() throws Exception {
  60. return (@RVTypeAnnotation("receiver class") System::lineSeparator);
  61. }
  62. public @RVTypeAnnotation("return type") Function<@RVTypeAnnotation("first parameter of generic type") Object, @RVTypeAnnotation("second parameter of generic type") Integer> methodSignatureRelatedTypeAnnotations() throws Exception {
  63. List<Object> l = new ArrayList<>();
  64. return (l::indexOf);
  65. }
  66. @SuppressWarnings("all")
  67. public <X extends @RVTypeAnnotation Serializable & @RVTypeAnnotation("annotation of second type of intersection type") Cloneable> void typeCheckRelatedTypeAnnotations(X x) {
  68. // annotated type test
  69. if(x instanceof @RVTypeAnnotation("annotated instanceof") List) {
  70. List<X> l = (@RVTypeAnnotation("annotated type cast") List< @RVTypeAnnotation("annotated generic type variable in (unsafe) type cast")X>) x;
  71. process(l);
  72. }
  73. // annotated type cast
  74. if(x instanceof List) {
  75. Object l = (Serializable & @RVTypeAnnotation("annotation of second type of a case to an intersection type") Cloneable) x;
  76. process(l);
  77. }
  78. }
  79. public String doIt(File file) throws Exception{
  80. try (
  81. @RVTypeAnnotation("resource variable") BufferedReader br =
  82. new BufferedReader(new FileReader(file))
  83. ) {
  84. return br.readLine();
  85. }
  86. }
  87. public String doArray(File[] file) throws Exception{
  88. @RVTypeAnnotation("the array")File[] newFiles = file.clone();
  89. Object[]@RVTypeAnnotation("an inner array")[] os = new @RVTypeAnnotation("a new array") Object[10]@RVTypeAnnotation("a new inner array")[5];
  90. process(os);
  91. return newFiles[0].toString();
  92. }
  93. }