Example usage for org.eclipse.jdt.core.util IMethodInfo getCodeAttribute

List of usage examples for org.eclipse.jdt.core.util IMethodInfo getCodeAttribute

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.util IMethodInfo getCodeAttribute.

Prototype

ICodeAttribute getCodeAttribute();

Source Link

Document

Answer the code attribute of this method info, null if none or if the decoding flag doesn't include METHOD_BODIES.

Usage

From source file:org.eclipse.objectteams.otdt.tests.compiler.smap.Requestor.java

License:Open Source License

protected void outputClassFiles(CompilationResult unitResult) {
    if ((unitResult != null) && (!unitResult.hasErrors() || forceOutputGeneration)) {
        ClassFile[] classFiles = unitResult.getClassFiles();
        for (int i = 0, fileCount = classFiles.length; i < fileCount; i++) {
            // retrieve the key and the corresponding classfile
            ClassFile classFile = classFiles[i];
            if (outputPath != null) {
                String relativeName = new String(classFile.fileName()).replace('/', File.separatorChar)
                        + ".class";
                try {
                    org.eclipse.jdt.internal.compiler.util.Util.writeToDisk(true, outputPath, relativeName,
                            classFile);//from   ww w .  j a va 2 s  . c  o m
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (this.lineNumbers != null) {
                ClassFileReader cfr;
                try {
                    cfr = new ClassFileReader(classFile.getBytes(),
                            IClassFileReader.METHOD_INFOS | IClassFileReader.METHOD_BODIES);
                } catch (ClassFormatException e) {
                    throw new AssertionFailedError("Can't read class file: " + e.getMessage());
                }
                for (IMethodInfo method : cfr.getMethodInfos()) {
                    String fullMethodDesignator = String
                            .valueOf(CharOperation.concatWith(classFile.getCompoundName(),
                                    CharOperation.concat(method.getName(), method.getDescriptor()), '.'));
                    int[] expectedNumbers = this.lineNumbers.get(fullMethodDesignator);
                    if (expectedNumbers != null) {
                        this.lineNumbers.remove(fullMethodDesignator);
                        ILineNumberAttribute lineNumberAttribute = method.getCodeAttribute()
                                .getLineNumberAttribute();
                        int[][] table = lineNumberAttribute.getLineNumberTable();
                        Assert.assertEquals("wrong number of line numbers", expectedNumbers.length,
                                table.length);
                        for (int n = 0; n < expectedNumbers.length; n++)
                            Assert.assertEquals("wrong line numeber", expectedNumbers[n], table[n][1]);
                    }
                }
            }
        }
    }
}