Java Code Examples for org.eclipse.jdt.core.util.IClassFileReader

The following examples show how to use org.eclipse.jdt.core.util.IClassFileReader. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static Long calculateSerialVersionId(ITypeBinding typeBinding, final IProgressMonitor monitor) throws CoreException, IOException {
	try {
		IFile classfileResource= getClassfile(typeBinding);
		if (classfileResource == null)
			return null;

		InputStream contents= classfileResource.getContents();
		try {
			IClassFileReader cfReader= ToolFactory.createDefaultClassFileReader(contents, IClassFileReader.ALL);
			if (cfReader != null) {
				return calculateSerialVersionId(cfReader);
			}
		} finally {
			contents.close();
		}
		return null;
	} finally {
		if (monitor != null)
			monitor.done();
	}
}
 
Example 2
private static int getClassModifiers(IClassFileReader cfReader) {
	IInnerClassesAttribute innerClassesAttribute= cfReader.getInnerClassesAttribute();
	if (innerClassesAttribute != null) {
		IInnerClassesAttributeEntry[] entries = innerClassesAttribute.getInnerClassAttributesEntries();
		for (int i= 0; i < entries.length; i++) {
			IInnerClassesAttributeEntry entry = entries[i];
			char[] innerClassName = entry.getInnerClassName();
			if (innerClassName != null) {
				if (CharOperation.equals(cfReader.getClassName(), innerClassName)) {
					return entry.getAccessFlags();
				}
			}
		}
	}
	return cfReader.getAccessFlags();
}
 
Example 3
private static IMethodInfo[] getSortedMethods(IClassFileReader cfReader) {
	IMethodInfo[] allMethods= cfReader.getMethodInfos();
	Arrays.sort(allMethods, new Comparator<IMethodInfo>() {
		public int compare(IMethodInfo mi1, IMethodInfo mi2) {
			if (mi1.isConstructor() != mi2.isConstructor()) {
				return mi1.isConstructor() ? -1 : 1;
			} else if (mi1.isConstructor()) {
				return 0;
			}
			int res= CharOperation.compareTo(mi1.getName(), mi2.getName());
			if (res != 0) {
				return res;
			}
			return CharOperation.compareTo(mi1.getDescriptor(), mi2.getDescriptor());
		}
	});
	return allMethods;
}
 
Example 4
private static char[][] getSortedInterfacesNames(IClassFileReader cfReader) {
	char[][] interfaceNames= cfReader.getInterfaceNames();
	Arrays.sort(interfaceNames, new Comparator<char[]>() {
		public int compare(char[] o1, char[] o2) {
			return CharOperation.compareTo(o1, o2);
		}
	});
	return interfaceNames;
}
 
Example 5
private static IFieldInfo[] getSortedFields(IClassFileReader cfReader) {
	IFieldInfo[] allFields= cfReader.getFieldInfos();
	Arrays.sort(allFields, new Comparator<IFieldInfo>() {
		public int compare(IFieldInfo o1, IFieldInfo o2) {
			return CharOperation.compareTo(o1.getName(), o2.getName());
		}
	});
	return allFields;
}
 
Example 6
private static boolean hasStaticClassInitializer(IClassFileReader cfReader) {
	IMethodInfo[] methodInfos= cfReader.getMethodInfos();
	for (int i= 0; i < methodInfos.length; i++) {
		if (methodInfos[i].isClinit()) {
			return true;
		}
	}
	return false;
}
 
Example 7
private boolean checkClassFile(IClassFileReader classFileReader) {
	IMethodInfo[] methodInfos = classFileReader.getMethodInfos();
	for (int i = 0, max = methodInfos.length; i < max; i++) {
		ICodeAttribute codeAttribute = methodInfos[i].getCodeAttribute();
		if (codeAttribute != null && codeAttribute.getLineNumberAttribute() != null) {
			return true;
		}
	}
	return false;
}
 
Example 8
/**
 * @see Openable#openBuffer(IProgressMonitor, Object)
 */
protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {

	// create buffer
	IBuffer buffer = BufferManager.createBuffer(this);

	// set the buffer source
	IBuffer classFileBuffer = this.classFile.getBuffer();
	if (classFileBuffer != null) {
		buffer.setContents(classFileBuffer.getCharacters());
	} else {
		// Disassemble
		IClassFileReader reader = ToolFactory.createDefaultClassFileReader(this.classFile, IClassFileReader.ALL);
		Disassembler disassembler = new Disassembler();
		String contents = disassembler.disassemble(reader, Util.getLineSeparator("", getJavaProject()), ClassFileBytesDisassembler.WORKING_COPY); //$NON-NLS-1$
		buffer.setContents(contents);
	}

	// add buffer to buffer cache
	BufferManager bufManager = getBufferManager();
	bufManager.addBuffer(buffer);

	// listen to buffer changes
	buffer.addBufferChangedListener(this);

	return buffer;
}
 
Example 9
public static IClassFileAttribute getAttribute(IClassFileReader classFileReader, char[] attributeName) {
	IClassFileAttribute[] attributes = classFileReader.getAttributes();
	for (int i = 0, max = attributes.length; i < max; i++) {
		if (CharOperation.equals(attributes[i].getAttributeName(), attributeName)) {
			return attributes[i];
		}
	}
	return null;
}
 
Example 10
/**
 * Answers whether children should be visited.
 * <p>
 * If the associated resource is a class file which has been changed, record it.
 * </p>
 */
@Override
public boolean visit(IResourceDelta delta) {
    if (delta == null || 0 == (delta.getKind() & IResourceDelta.CHANGED)) {
        return false;
    }
    IResource resource = delta.getResource();
    if (resource != null) {
        switch (resource.getType()) {
            case IResource.FILE:
                if (0 == (delta.getFlags() & IResourceDelta.CONTENT)) {
                    return false;
                }
                if (CLASS_FILE_EXTENSION.equals(resource.getFullPath().getFileExtension())) {
                    IPath localLocation = resource.getLocation();
                    if (localLocation != null) {
                        String path = localLocation.toOSString();
                        IClassFileReader reader = ToolFactory.createDefaultClassFileReader(path,
                                IClassFileReader.CLASSFILE_ATTRIBUTES);
                        if (reader != null) {
                            // this name is slash-delimited
                            String qualifiedName = new String(reader.getClassName());
                            boolean hasBlockingErrors = false;
                            try {
                                // If the user doesn't want to replace
                                // classfiles containing
                                // compilation errors, get the source
                                // file associated with
                                // the class file and query it for
                                // compilation errors
                                IJavaProject pro = JavaCore.create(resource.getProject());
                                ISourceAttribute sourceAttribute = reader.getSourceFileAttribute();
                                String sourceName = null;
                                if (sourceAttribute != null) {
                                    sourceName = new String(sourceAttribute.getSourceFileName());
                                }
                                IResource sourceFile = getSourceFile(pro, qualifiedName, sourceName);
                                if (sourceFile != null) {
                                    IMarker[] problemMarkers = null;
                                    problemMarkers = sourceFile.findMarkers(
                                            IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true,
                                            IResource.DEPTH_INFINITE);
                                    for (IMarker problemMarker : problemMarkers) {
                                        if (problemMarker.getAttribute(IMarker.SEVERITY,
                                                -1) == IMarker.SEVERITY_ERROR) {
                                            hasBlockingErrors = true;
                                            break;
                                        }
                                    }
                                }
                            } catch (CoreException e) {
                                logger.log(Level.SEVERE, "Failed to visit classes: " + e.getMessage(), e);
                            }
                            if (!hasBlockingErrors) {
                                changedFiles.add(resource);
                                // dot-delimit the name
                                fullyQualifiedNames.add(qualifiedName.replace('/', '.'));
                            }
                        }
                    }
                }
                return false;

            default:
                return true;
        }
    }
    return true;
}
 
Example 11
private Map<String, ArrayList<IResource>> buildJavaToClassMap(IContainer container, IProgressMonitor monitor) throws CoreException {
	if (container == null || !container.isAccessible())
		return new HashMap<String, ArrayList<IResource>>(0);
	/*
	 * XXX: Bug 6584: Need a way to get class files for a java file (or CU)
	 */
	IClassFileReader cfReader= null;
	IResource[] members= container.members();
	Map<String, ArrayList<IResource>> map= new HashMap<String, ArrayList<IResource>>(members.length);
	for (int i= 0;  i < members.length; i++) {
		if (isClassFile(members[i])) {
			IFile classFile= (IFile)members[i];
			URI location= classFile.getLocationURI();
			if (location != null) {
				InputStream contents= null;
				try {
					contents= EFS.getStore(location).openInputStream(EFS.NONE, monitor);
					cfReader= ToolFactory.createDefaultClassFileReader(contents, IClassFileReader.CLASSFILE_ATTRIBUTES);
				} finally {
					try {
						if (contents != null)
							contents.close();
					} catch (IOException e) {
						throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.ERROR,
							Messages.format(JarPackagerMessages.JarFileExportOperation_errorCannotCloseConnection, BasicElementLabels.getURLPart(Resources.getLocationString(classFile))),
							e));
					}
				}
				if (cfReader != null) {
					ISourceAttribute sourceAttribute= cfReader.getSourceFileAttribute();
					if (sourceAttribute == null) {
						/*
						 * Can't fully build the map because one or more
						 * class file does not contain the name of its
						 * source file.
						 */
						addWarning(Messages.format(
							JarPackagerMessages.JarFileExportOperation_classFileWithoutSourceFileAttribute,
							BasicElementLabels.getURLPart(Resources.getLocationString(classFile))), null);
						return null;
					}
					String javaName= new String(sourceAttribute.getSourceFileName());
					ArrayList<IResource> classFiles= map.get(javaName);
					if (classFiles == null) {
						classFiles= new ArrayList<IResource>(3);
						map.put(javaName, classFiles);
					}
					classFiles.add(classFile);
				}
			}
		}
	}
	return map;
}
 
Example 12
/**
 * @param classFileBytes byte[]
 * @param constantPool IConstantPool
 * @param offset int
 * @param decodingFlags int
 */
public MethodInfo(byte classFileBytes[], IConstantPool constantPool, int offset, int decodingFlags)
	throws ClassFormatException {

	boolean no_code_attribute = (decodingFlags & IClassFileReader.METHOD_BODIES) == 0;
	final int flags = u2At(classFileBytes, 0, offset);
	this.accessFlags = flags;
	if ((flags & IModifierConstants.ACC_SYNTHETIC) != 0) {
		this.isSynthetic = true;
	}

	this.nameIndex = u2At(classFileBytes, 2, offset);
	IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(this.nameIndex);
	if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
		throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
	}
	this.name = constantPoolEntry.getUtf8Value();

	this.descriptorIndex = u2At(classFileBytes, 4, offset);
	constantPoolEntry = constantPool.decodeEntry(this.descriptorIndex);
	if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
		throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
	}
	this.descriptor = constantPoolEntry.getUtf8Value();

	this.attributesCount = u2At(classFileBytes, 6, offset);
	this.attributes = ClassFileAttribute.NO_ATTRIBUTES;
	if (this.attributesCount != 0) {
		if (no_code_attribute && !isAbstract() && !isNative()) {
			if (this.attributesCount != 1) {
				this.attributes = new IClassFileAttribute[this.attributesCount - 1];
			}
		} else {
			this.attributes = new IClassFileAttribute[this.attributesCount];
		}
	}
	int attributesIndex = 0;
	int readOffset = 8;
	for (int i = 0; i < this.attributesCount; i++) {
		constantPoolEntry = constantPool.decodeEntry(u2At(classFileBytes, readOffset, offset));
		if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
			throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
		}
		char[] attributeName = constantPoolEntry.getUtf8Value();
		if (equals(attributeName, IAttributeNamesConstants.DEPRECATED)) {
			this.isDeprecated = true;
			this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.SYNTHETIC)) {
			this.isSynthetic = true;
			this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.CODE)) {
			if (!no_code_attribute) {
				this.codeAttribute = new CodeAttribute(classFileBytes, constantPool, offset + readOffset);
				this.attributes[attributesIndex++] = this.codeAttribute;
			}
		} else if (equals(attributeName, IAttributeNamesConstants.EXCEPTIONS)) {
			this.exceptionAttribute = new ExceptionAttribute(classFileBytes, constantPool, offset + readOffset);
			this.attributes[attributesIndex++] = this.exceptionAttribute;
		} else if (equals(attributeName, IAttributeNamesConstants.SIGNATURE)) {
			this.attributes[attributesIndex++] = new SignatureAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_ANNOTATIONS)) {
			this.attributes[attributesIndex++] = new RuntimeVisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_ANNOTATIONS)) {
			this.attributes[attributesIndex++] = new RuntimeInvisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS)) {
			this.attributes[attributesIndex++] = new RuntimeVisibleParameterAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS)) {
			this.attributes[attributesIndex++] = new RuntimeInvisibleParameterAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.ANNOTATION_DEFAULT)) {
			this.attributes[attributesIndex++] = new AnnotationDefaultAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_TYPE_ANNOTATIONS)) {
			this.attributes[attributesIndex++] = new RuntimeVisibleTypeAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS)) {
			this.attributes[attributesIndex++] = new RuntimeInvisibleTypeAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
		} else if (equals(attributeName, IAttributeNamesConstants.METHOD_PARAMETERS)) {
			this.attributes[attributesIndex++] = new MethodParametersAttribute(classFileBytes, constantPool, offset + readOffset);
		} else {
			this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
		}
		readOffset += (6 + u4At(classFileBytes, readOffset + 2, offset));
	}
	this.attributeBytes = readOffset;
}