View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import org.apache.bcel.AbstractTest;
31  import org.apache.bcel.Const;
32  import org.apache.bcel.util.SyntheticRepository;
33  import org.apache.bcel.visitors.CountingVisitor;
34  import org.junit.jupiter.api.Test;
35  
36  class RecordTest extends AbstractTest {
37  
38      /**
39       * Check that we can copy a attribute correctly.
40       */
41      @Test
42      void recordsCanBeCopied() throws ClassNotFoundException, IOException {
43          final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
44          final JavaClass copyClazz = clazz.copy();
45          assertEquals(clazz.toString(), copyClazz.toString(), "both records should have the same value");
46      }
47  
48      /**
49       * Check that a record can be visited by our visitors
50       */
51      @Test
52      void recordsCanBeVisited() throws ClassNotFoundException, IOException {
53          final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
54          final CountingVisitor countVisitor = new CountingVisitor();
55          final DescendingVisitor desendingVisitor = new DescendingVisitor(clazz, countVisitor);
56          desendingVisitor.visit();
57          assertEquals(1, countVisitor.recordCount, "should count one record");
58          assertEquals(2, countVisitor.recordComponentCount, "should count two record components");
59      }
60  
61      /**
62       * Check that we can save and load the attribute correctly.
63       */
64      @Test
65      void testAttributeSerializtion() throws ClassNotFoundException, IOException {
66          final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
67          final File tfile = createTestdataFile("SimpleRecord.class");
68          final Record recordAttribute = (Record) findAttribute("Record", clazz)[0];
69          clazz.dump(tfile);
70          // Read in the new version and check it is OK
71          final SyntheticRepository repos2 = createRepos(".");
72          final JavaClass clazzFromRepo = repos2.loadClass("SimpleRecord");
73          assertNotNull(clazzFromRepo); // Use the variable to avoid a warning
74          final Record recordAttributeFromRepo = (Record) findAttribute("Record", clazzFromRepo)[0];
75          assertEquals(recordAttribute.toString(), recordAttributeFromRepo.toString(), "Both attributes need to be equal");
76          tfile.deleteOnExit();
77      }
78  
79      /**
80       * A record type, once compiled, should result in a class file that is
81       * marked such that we can determine from the access flags
82       * (through BCEL) that it is in fact a record.
83       *
84       * @throws IOException Thrown if an I/O error occurs.
85       * @throws ClassFormatException Thrown if the class file format is invalid.
86       */
87      @Test
88      void testRecordClassSaysItIs() throws ClassNotFoundException, ClassFormatException, IOException {
89          final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
90          assertTrue(clazz.isRecord(), "Expected SimpleRecord class to say it was a record - but it didn't!");
91          final JavaClass simpleClazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.SimpleClass");
92          assertFalse(simpleClazz.isRecord(), "Expected SimpleClass class to say it was not a record - but it didn't!");
93      }
94  
95      @Test
96      void testRecordComponentGetAttribute() throws ClassFormatException, IOException {
97          final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
98          final Record recordAttribute = (Record) findAttribute("Record", clazz)[0];
99          final RecordComponentInfo secondComponent = recordAttribute.getComponents()[1];
100         final RuntimeVisibleAnnotations ann = secondComponent.getAttribute(Const.ATTR_RUNTIME_VISIBLE_ANNOTATIONS);
101         assertEquals("RuntimeVisibleAnnotations:\n"
102                 + "  @Ljavax/annotation/Nonnull;", ann.toString());
103         assertNull(secondComponent.getAttribute(Const.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS));
104     }
105 
106     /**
107      * A simple record with two simple fields, an integer and a String field, should
108      * show its content in its string representation.
109      *
110      * @throws ClassNotFoundException Thrown if the class cannot be found.
111      * @throws ClassFormatException Thrown if the class file format is invalid.
112      * @throws IOException Thrown if an I/O error occurs.
113      */
114     @Test
115     void testRecordToString() throws ClassNotFoundException, ClassFormatException, IOException {
116         final JavaClass clazz = new ClassParser("src/test/resources/record/SimpleRecord.class").parse();
117         final Attribute[] attributes = clazz.getAttributes();
118         final Record recordAttribute = (Record) findAttribute("Record", clazz)[0];
119         assertEquals(4, attributes.length);
120         assertEquals("SourceFile: SimpleRecord.java", attributes[0].toString());
121         assertEquals("Record(2):\n"
122                 + "  RecordComponentInfo(aNumber,I,0):\n"
123                 + "  RecordComponentInfo(aString,Ljava/lang/String;,1):\n"
124                 + "  RuntimeVisibleAnnotations:\n"
125                 + "  @Ljavax/annotation/Nonnull;", recordAttribute.toString());
126         final RecordComponentInfo firstComponent = recordAttribute.getComponents()[0];
127         assertEquals(5, firstComponent.getIndex());
128         assertEquals(6, firstComponent.getDescriptorIndex());
129         assertEquals(0, firstComponent.getAttributes().length);
130         assertEquals(recordAttribute.getConstantPool(), firstComponent.getConstantPool());
131         assertEquals("RecordComponentInfo(aNumber,I,0):", firstComponent.toString());
132     }
133 }