List of usage examples for org.eclipse.jdt.core.util IConstantPool getEntryKind
int getEntryKind(int index);
From source file:com.siteview.mde.internal.ui.search.dependencies.PackageFinder.java
License:Open Source License
static void computeReferencedTypes(IClassFileReader cfr, Set packages) { char[][] interfaces = cfr.getInterfaceNames(); if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) { //note: have to convert names like Ljava/lang/Object; to java.lang.Object packages.add(getPackage(new String(interfaces[i]).replace('/', '.'))); }//from ww w . jav a2 s. c o m } char[] scn = cfr.getSuperclassName(); if (scn != null) { packages.add(getPackage(new String(scn).replace('/', '.'))); } IFieldInfo[] fieldInfo = cfr.getFieldInfos(); for (int i = 0; i < fieldInfo.length; i++) { String fieldName = new String(fieldInfo[i].getDescriptor()); if (!isPrimitiveTypeSignature(fieldName)) { String fieldDescriptor = extractFullyQualifiedTopLevelType(fieldName); packages.add(getPackage(new String(fieldDescriptor))); } } IMethodInfo[] methodInfo = cfr.getMethodInfos(); for (int i = 0; i < methodInfo.length; i++) { IExceptionAttribute exceptionAttribute = methodInfo[i].getExceptionAttribute(); if (exceptionAttribute != null) { char[][] exceptionNames = exceptionAttribute.getExceptionNames(); for (int j = 0; j < exceptionNames.length; j++) { packages.add(getPackage(new String(exceptionNames[j]).replace('/', '.'))); } } String descriptor = new String(methodInfo[i].getDescriptor()); //add parameter types String[] parameterTypes = Signature.getParameterTypes(descriptor); for (int j = 0; j < parameterTypes.length; j++) { //have to parse to convert [Ljava/lang/String; to java.lang.String if (!isPrimitiveTypeSignature(parameterTypes[j])) { packages.add(getPackage(new String(extractFullyQualifiedTopLevelType(parameterTypes[j])))); } } //add return type String returnType = Signature.getReturnType(descriptor); if (!isPrimitiveTypeSignature(returnType)) { returnType = extractFullyQualifiedTopLevelType(returnType); packages.add(getPackage(returnType)); } } // Is there more to extract from the constant pool?? IConstantPoolEntry entry; IConstantPool pool = cfr.getConstantPool(); int length = pool.getConstantPoolCount(); for (int i = 1; i < length; i++) { switch (pool.getEntryKind(i)) { case IConstantPoolConstant.CONSTANT_Class: // add reference to the class entry = pool.decodeEntry(i); //note: may have to convert names like Ljava/lang/Object; to java.lang.Object String className = new String(entry.getClassInfoName()).replace('/', '.'); className = className.indexOf(';') >= 0 ? extractFullyQualifiedTopLevelType(className) : className; packages.add(getPackage(className)); break; case IConstantPoolConstant.CONSTANT_NameAndType: // add reference to the name and type entry = pool.decodeEntry(i); int descIndex = entry.getNameAndTypeInfoDescriptorIndex(); if (pool.getEntryKind(descIndex) == IConstantPoolConstant.CONSTANT_Utf8) { entry = pool.decodeEntry(descIndex); char[] type = entry.getUtf8Value(); if (type[0] == '(') { // Method signature. //add parameter types String descriptor = new String(type); String[] parameterTypes = Signature.getParameterTypes(descriptor); for (int j = 0; j < parameterTypes.length; j++) { if (!isPrimitiveTypeSignature(parameterTypes[j])) { packages.add(getPackage(extractFullyQualifiedTopLevelType(parameterTypes[j]))); } } //add return type String returnType = Signature.getReturnType(descriptor); if (!isPrimitiveTypeSignature(returnType)) { returnType = extractFullyQualifiedTopLevelType(returnType); packages.add(getPackage(returnType)); } } else { // Field type. String typeString = new String(type); if (!isPrimitiveTypeSignature(typeString)) { packages.add(getPackage(extractFullyQualifiedTopLevelType(typeString))); } } } break; } } packages.remove(""); // removes default package if it exists //$NON-NLS-1$ }
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 va2 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.ant.internal.ui.datatransfer.SourceAnalyzer.java
License:Open Source License
/** * Find all classes that are required by given class file. * //w ww . ja v a 2 s .c o m * @param file * a ".class" file * @return set of strings, each contains a full qualified class name (forward slash as package separator) */ public static Set<String> getRequiredClasses(IFile file) { Set<String> classes = new TreeSet<>(); IClassFile classFile = JavaCore.createClassFileFrom(file); IClassFileReader reader = ToolFactory.createDefaultClassFileReader(classFile, IClassFileReader.CONSTANT_POOL); if (reader == null) { // class not compiled return classes; } IConstantPool pool = reader.getConstantPool(); for (int i = 0; i < pool.getConstantPoolCount(); i++) { if (pool.getEntryKind(i) == IConstantPoolConstant.CONSTANT_Class) { IConstantPoolEntry entry = pool.decodeEntry(i); String classname = new String(entry.getClassInfoName()); // don't return inner classes int index = classname.indexOf('$'); if (index != -1) { classname = classname.substring(0, index); } classes.add(classname); } } return classes; }
From source file:org.eclipse.pde.internal.ui.search.dependencies.PackageFinder.java
License:Open Source License
static void computeReferencedTypes(IClassFileReader cfr, Set<String> packages) { char[][] interfaces = cfr.getInterfaceNames(); if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) { //note: have to convert names like Ljava/lang/Object; to java.lang.Object packages.add(getPackage(new String(interfaces[i]).replace('/', '.'))); }//from w w w . ja va 2s . co m } char[] scn = cfr.getSuperclassName(); if (scn != null) { packages.add(getPackage(new String(scn).replace('/', '.'))); } IFieldInfo[] fieldInfo = cfr.getFieldInfos(); for (int i = 0; i < fieldInfo.length; i++) { String fieldName = new String(fieldInfo[i].getDescriptor()); if (!isPrimitiveTypeSignature(fieldName)) { String fieldDescriptor = extractFullyQualifiedTopLevelType(fieldName); packages.add(getPackage(new String(fieldDescriptor))); } } IMethodInfo[] methodInfo = cfr.getMethodInfos(); for (int i = 0; i < methodInfo.length; i++) { IExceptionAttribute exceptionAttribute = methodInfo[i].getExceptionAttribute(); if (exceptionAttribute != null) { char[][] exceptionNames = exceptionAttribute.getExceptionNames(); for (int j = 0; j < exceptionNames.length; j++) { packages.add(getPackage(new String(exceptionNames[j]).replace('/', '.'))); } } String descriptor = new String(methodInfo[i].getDescriptor()); //add parameter types String[] parameterTypes = Signature.getParameterTypes(descriptor); for (int j = 0; j < parameterTypes.length; j++) { //have to parse to convert [Ljava/lang/String; to java.lang.String if (!isPrimitiveTypeSignature(parameterTypes[j])) { packages.add(getPackage(new String(extractFullyQualifiedTopLevelType(parameterTypes[j])))); } } //add return type String returnType = Signature.getReturnType(descriptor); if (!isPrimitiveTypeSignature(returnType)) { returnType = extractFullyQualifiedTopLevelType(returnType); packages.add(getPackage(returnType)); } } // Is there more to extract from the constant pool?? IConstantPoolEntry entry; IConstantPool pool = cfr.getConstantPool(); int length = pool.getConstantPoolCount(); for (int i = 1; i < length; i++) { switch (pool.getEntryKind(i)) { case IConstantPoolConstant.CONSTANT_Class: // add reference to the class entry = pool.decodeEntry(i); //note: may have to convert names like Ljava/lang/Object; to java.lang.Object String className = new String(entry.getClassInfoName()).replace('/', '.'); className = className.indexOf(';') >= 0 ? extractFullyQualifiedTopLevelType(className) : className; packages.add(getPackage(className)); break; case IConstantPoolConstant.CONSTANT_NameAndType: // add reference to the name and type entry = pool.decodeEntry(i); int descIndex = entry.getNameAndTypeInfoDescriptorIndex(); if (pool.getEntryKind(descIndex) == IConstantPoolConstant.CONSTANT_Utf8) { entry = pool.decodeEntry(descIndex); char[] type = entry.getUtf8Value(); if (type[0] == '(') { // Method signature. //add parameter types String descriptor = new String(type); String[] parameterTypes = Signature.getParameterTypes(descriptor); for (int j = 0; j < parameterTypes.length; j++) { if (!isPrimitiveTypeSignature(parameterTypes[j])) { packages.add(getPackage(extractFullyQualifiedTopLevelType(parameterTypes[j]))); } } //add return type String returnType = Signature.getReturnType(descriptor); if (!isPrimitiveTypeSignature(returnType)) { returnType = extractFullyQualifiedTopLevelType(returnType); packages.add(getPackage(returnType)); } } else { // Field type. String typeString = new String(type); if (!isPrimitiveTypeSignature(typeString)) { packages.add(getPackage(extractFullyQualifiedTopLevelType(typeString))); } } } break; } } packages.remove(""); // removes default package if it exists //$NON-NLS-1$ }
From source file:org.seasar.diigu.eclipse.operation.NameEnhanceJob.java
License:Apache License
public static IResource toSource(IClassFile clazz) throws CoreException { IJavaProject project = clazz.getJavaProject(); IClassFileReader reader = ToolFactory.createDefaultClassFileReader(clazz, IClassFileReader.CONSTANT_POOL); if (reader != null) { IConstantPool pool = reader.getConstantPool(); for (int i = 0; i < pool.getConstantPoolCount(); i++) { if (pool.getEntryKind(i) == IConstantPoolConstant.CONSTANT_Class) { IConstantPoolEntry entry = pool.decodeEntry(i); char[] data = entry.getClassInfoName(); String name = String.valueOf(data); name = name.replace('/', '.'); IType type = project.findType(name); if (type != null && type.isBinary() == false) { return type.getResource(); }/*w w w .java 2s . c o m*/ } } } return null; }