List of usage examples for org.eclipse.jdt.core.util IMethodInfo getDescriptor
char[] getDescriptor();
From source file:com.inepex.classtemplater.plugin.logic.Annotation.java
License:Eclipse Public License
public Annotation(IAnnotation jdtAnnotation, ICompilationUnit compilationUnit) throws Exception { this.compilationUnit = compilationUnit; name = jdtAnnotation.getElementName(); //default values aren't found in JDT so using AST to get them String[][] type = compilationUnit.findPrimaryType().resolveType(jdtAnnotation.getElementName()); if (type != null) { IType annType = jdtAnnotation.getJavaProject().findType(type[0][0] + "." + type[0][1]); //hint to read annotation default value from a classfile // if (annType.getCompilationUnit() != null) { AnnotationASTVisitor annASTVisitor = new AnnotationASTVisitor(); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(annType.getCompilationUnit()); parser.setResolveBindings(true); CompilationUnit aParser = (CompilationUnit) parser.createAST(null); aParser.accept(annASTVisitor); Map<String, Object> defaultValues = annASTVisitor.getDefaultValueObjects(); for (Entry<String, Object> entry : defaultValues.entrySet()) { paramObjects.put(entry.getKey(), entry.getValue()); params.put(entry.getKey(), String.valueOf(entry.getValue())); }/*from w w w. ja v a2 s.c o m*/ } else { //read annotation default value from .class file IClassFileReader reader = ToolFactory.createDefaultClassFileReader(annType.getClassFile(), IClassFileReader.ALL); if (reader != null) { for (IMethodInfo methodInfo : reader.getMethodInfos()) { if (methodInfo.getAttributes().length > 0 && methodInfo.getAttributes()[0] instanceof AnnotationDefaultAttribute) { String name = new String(methodInfo.getName()); Object value = parseDefaultObjectValueFromAnnotationDefaultAttribute( (AnnotationDefaultAttribute) (methodInfo.getAttributes()[0]), new String(methodInfo.getDescriptor())); if (value != null) { paramObjects.put(name, value); params.put(name, String.valueOf(value)); } } // System.out.println(methodInfo.getName()); } } } } for (IMemberValuePair pair : jdtAnnotation.getMemberValuePairs()) { try { params.put(pair.getMemberName(), String.valueOf(pair.getValue())); paramObjects.put(pair.getMemberName(), pair.getValue()); } catch (ClassCastException e) { Log.log("Could not cast value of annotation-attribute: " + name + ", " + pair.getMemberName() + ". \n" + "Only string values can be used for annotation-attribute"); } } }
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 . j a v a2s . co 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.ajdt.internal.ui.editor.quickfix.AJSerialVersionHashOperation.java
License:Open Source License
private static IMethodInfo[] getSortedMethods(IClassFileReader cfReader) { IMethodInfo[] allMethods = cfReader.getMethodInfos(); Arrays.sort(allMethods, new Comparator() { public int compare(Object o1, Object o2) { IMethodInfo mi1 = (IMethodInfo) o1; IMethodInfo mi2 = (IMethodInfo) o2; if (mi1.isConstructor() != mi2.isConstructor()) { return mi1.isConstructor() ? -1 : 1; } else if (mi1.isConstructor()) { return 0; }//from www .j av a 2 s .c o m int res = CharOperation.compareTo(mi1.getName(), mi2.getName()); if (res != 0) { return res; } return CharOperation.compareTo(mi1.getDescriptor(), mi2.getDescriptor()); } }); return allMethods; }
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. jav a 2s . c om } 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]); } } } } } }
From source file:org.eclipse.pde.tools.internal.versioning.JavaClassVersionCompare.java
License:Open Source License
/** * converts the given object signature to a readable string * @param object IFieldInfo instance of IMethodInfo instance * @return String type signature/* ww w . j a v a 2 s . co m*/ */ private String getSignatureString(Object object) { StringBuffer buffer = new StringBuffer(); if (object instanceof IMethodInfo) { IMethodInfo method = (IMethodInfo) object; buffer.append(Flags.toString(method.getAccessFlags())); buffer.append(SPACE); buffer.append(Signature.toString(charsToString(method.getDescriptor()), charsToString(method.getName()), null, false, true)); } else { IFieldInfo field = (IFieldInfo) object; buffer.append(Flags.toString(field.getAccessFlags())); buffer.append(SPACE); buffer.append(Signature.toString(charsToString(field.getDescriptor()))); buffer.append(SPACE); buffer.append(charsToString(field.getName())); } return buffer.toString(); }
From source file:org.eclipse.pde.tools.internal.versioning.JavaClassVersionCompare.java
License:Open Source License
/** * get key of a method. Key of a method is a String which * includes the retype of the method,name of the method and the parameter types of the method * (e.g. key of public int foo(int , String) is "int#foo#int#String" * @param method//from w ww.j a v a 2s . c o m * @return key of a method */ private String getMethodKey(IMethodInfo method) { StringBuffer buffer = new StringBuffer(); // append return type buffer.append(method.getDescriptor()); // append name of method buffer.append(charsToString(method.getName())); char[][] parameters = Signature.getParameterTypes(method.getDescriptor()); // append parameter types if (parameters != null) { for (int i = 0; i < parameters.length; i++) { buffer.append(KEY_SEPARATOR); buffer.append(charsToString(parameters[i])); } } return buffer.toString(); }