/* ******************************************************************* * Copyright (c) 2016 Contributors * All rights reserved. * This program and the accompanying materials are made available * under the terms of the Eclipse Public License v1.0 * which accompanies this distribution and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Andy Clement - initial implementation * ******************************************************************/ 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.Provide; import org.aspectj.apache.bcel.classfile.Module.Require; import org.aspectj.apache.bcel.classfile.Module.Uses; import org.aspectj.apache.bcel.classfile.SourceFile; import org.aspectj.apache.bcel.classfile.Unknown; /** * http://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.MAJOR_1_9,javaClass.getMajor()); assertEquals(Constants.MINOR_1_9,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 = new Module((Unknown)getAttribute(attrs,Constants.ATTR_UNKNOWN)); 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