/* ******************************************************************* * Copyright (c) 2016-2017 Contributors * All rights reserved. * This program and the accompanying materials are made available * under the terms of the Eclipse Public License v 2.0 * which accompanies this distribution and is available at * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt * ******************************************************************/ package org.aspectj.apache.bcel.classfile.tests; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import org.aspectj.apache.bcel.Constants; import org.aspectj.apache.bcel.classfile.Attribute; import org.aspectj.apache.bcel.classfile.ClassParser; import org.aspectj.apache.bcel.classfile.JavaClass; import org.aspectj.apache.bcel.classfile.Module; import org.aspectj.apache.bcel.classfile.Module.Export; import org.aspectj.apache.bcel.classfile.Module.Open; import org.aspectj.apache.bcel.classfile.Module.Provide; import org.aspectj.apache.bcel.classfile.Module.Require; import org.aspectj.apache.bcel.classfile.Module.Uses; import org.aspectj.apache.bcel.classfile.SourceFile; /** * https://cr.openjdk.java.net/~mr/jigsaw/spec/lang-vm.html * * @author Andy Clement */ public class ModuleTest extends BcelTestCase { public void testLoadSimpleModuleClass() throws Exception { String moduleFilename = "testdata/modules/one/module-info.class"; ClassParser classParser = new ClassParser(moduleFilename); JavaClass javaClass = classParser.parse(); assertNotNull(javaClass); assertEquals(Constants.ClassFileVersion.of(9).MAJOR, javaClass.getMajor()); assertEquals(Constants.ClassFileVersion.of(9).MINOR, javaClass.getMinor()); assertEquals(Constants.ACC_MODULE,javaClass.getModifiers()); assertEquals(0,javaClass.getSuperclassNameIndex()); assertEquals(0,javaClass.getInterfaceIndices().length); assertEquals(0,javaClass.getFields().length); assertEquals(0,javaClass.getMethods().length); Attribute[] attrs = javaClass.getAttributes(); assertEquals(2,attrs.length); SourceFile sourceFile = (SourceFile) getAttribute(attrs,Constants.ATTR_SOURCE_FILE); Module moduleAttr = (Module) getAttribute(attrs, Constants.ATTR_MODULE); byte[] originalData = moduleAttr.getBytes(); String[] requiredModuleNames = moduleAttr.getRequiredModuleNames(); assertEquals(1,requiredModuleNames.length); assertEquals("java.base",requiredModuleNames[0]); ByteArrayOutputStream baos = new ByteArrayOutputStream(); moduleAttr.dump(new DataOutputStream(baos)); byte[] newData = baos.toByteArray(); // The 6 offset here is because the newdata includes the 2byte cpool pointer for the name 'Module' // and the 4byte int length field for the attribute data if (newData.length!=originalData.length+6) { fail("Expected the length of the original attribute ("+originalData.length+") to match the new written length ("+newData.length+")"); } for (int i=0;i