Example usage for org.eclipse.jdt.core.util IClassFileReader METHOD_BODIES

List of usage examples for org.eclipse.jdt.core.util IClassFileReader METHOD_BODIES

Introduction

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

Prototype

int METHOD_BODIES

To view the source code for org.eclipse.jdt.core.util IClassFileReader METHOD_BODIES.

Click Source Link

Document

This value should be used to read the method bodies.

Usage

From source file:de.bodden.tamiflex.resolution.MethodResolver.java

License:Open Source License

private static void disambiguateMethodByLineNumber(String containerClassName, String methodName,
        IProject containerProject, int lineNumber) {
    IJavaProject javaProject = JavaCore.create(containerProject);
    try {// ww w  . j ava2s . c o  m
        IType type = javaProject.findType(containerClassName, (IProgressMonitor) null);
        ICompilationUnit compilationUnit = type.getCompilationUnit();

        if (compilationUnit != null) {
            ASTParser parser = ASTParser.newParser(AST.JLS3);
            parser.setSource(compilationUnit);
            parser.setResolveBindings(true);

            CompilationUnit cu = (CompilationUnit) parser.createAST(null);
            final int linePosition = cu.getPosition(lineNumber, 0);

            cu.accept(new ASTVisitor() {

                public boolean visit(MethodDeclaration method) {
                    if (method.getStartPosition() <= linePosition
                            && method.getStartPosition() + method.getLength() >= linePosition) {
                        //method is resolved
                        resolvedMethods.clear();
                        resolvedMethods.add((IMethod) method.resolveBinding().getJavaElement());
                    }
                    return false;
                }

            });
        } else {
            IClassFile classFile = type.getClassFile();
            if (classFile != null) {
                IClassFileReader reader = ToolFactory.createDefaultClassFileReader(classFile,
                        IClassFileReader.METHOD_INFOS | IClassFileReader.METHOD_BODIES);
                for (IMethodInfo method : reader.getMethodInfos()) {
                    String currMethodName = new String(method.getName());
                    if (!currMethodName.equals(methodName))
                        continue;

                    ICodeAttribute codeAttribute = method.getCodeAttribute();
                    ILineNumberAttribute lineNumberAttribute = codeAttribute.getLineNumberAttribute();
                    if (lineNumberAttribute != null) {
                        int[][] lineNumberTable = lineNumberAttribute.getLineNumberTable();
                        if (lineNumberTable != null && lineNumberTable.length > 0) {
                            int startLine = Integer.MAX_VALUE;
                            int endLine = 0;
                            for (int[] entry : lineNumberTable) {
                                int line = entry[1];
                                startLine = Math.min(startLine, line);
                                endLine = Math.max(endLine, line);
                            }
                            if (startLine >= lineNumber && endLine <= lineNumber) {
                                char[][] parameterTypes = Signature.getParameterTypes(method.getDescriptor());
                                String[] parameterTypeNames = new String[parameterTypes.length];
                                int i = 0;
                                for (char[] cs : parameterTypes) {
                                    parameterTypeNames[i] = new String(cs);
                                    i++;
                                }
                                IMethod iMethod = type.getMethod(currMethodName, parameterTypeNames);

                                //method resolved
                                resolvedMethods.clear();
                                resolvedMethods.add(iMethod);
                            }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

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 w ww.j  a v  a  2s. com*/
                } 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]);
                    }
                }
            }
        }
    }
}