List of usage examples for org.eclipse.jdt.core.util IClassFileReader METHOD_INFOS
int METHOD_INFOS
To view the source code for org.eclipse.jdt.core.util IClassFileReader METHOD_INFOS.
Click Source Link
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 {/* w w w . j a v a2 s .co 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:edu.uci.ics.sourcerer.tools.java.extractor.eclipse.NamingAdvisor.java
License:Open Source License
private void addClassFile(IClassFile classFile) { IClassFileReader reader = ToolFactory.createDefaultClassFileReader(classFile, IClassFileReader.CONSTANT_POOL | IClassFileReader.METHOD_INFOS); IConstantPool constants = reader.getConstantPool(); for (int i = 0, max = constants.getConstantPoolCount(); i < max; i++) { switch (constants.getEntryKind(i)) { case IConstantPoolConstant.CONSTANT_Class: { IConstantPoolEntry constant = constants.decodeEntry(i); addClass(new String(constant.getClassInfoName()).replace('/', '.')); break; }/*from w w w .ja v a2 s . c o m*/ case IConstantPoolConstant.CONSTANT_Fieldref: { IConstantPoolEntry constant = constants.decodeEntry(i); addClassSig(constant.getFieldDescriptor()); break; } case IConstantPoolConstant.CONSTANT_Methodref: { IConstantPoolEntry constant = constants.decodeEntry(i); addMethod(new String(constant.getClassName()), new String(constant.getMethodName()), constant.getMethodDescriptor()); break; } case IConstantPoolConstant.CONSTANT_InterfaceMethodref: { IConstantPoolEntry constant = constants.decodeEntry(i); addMethod(new String(constant.getClassName()), new String(constant.getMethodName()), constant.getMethodDescriptor()); break; } default: } } // Add the method parameter / return types for (IMethodInfo method : reader.getMethodInfos()) { for (char[] p : Signature.getParameterTypes(method.getDescriptor())) { addClassSig(p); } addClassSig(Signature.getReturnType(method.getDescriptor())); } }
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);/* w ww . jav a 2 s . co 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]); } } } } } }