Java Code Examples for org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader#u1At()

The following examples show how to use org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader#u1At() . 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
Source File: BinaryTypeConverter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ImportReference[] buildImports(ClassFileReader reader) {
	// add remaining references to the list of type names
	// (code extracted from BinaryIndexer#extractReferenceFromConstantPool(...))
	int[] constantPoolOffsets = reader.getConstantPoolOffsets();
	int constantPoolCount = constantPoolOffsets.length;
	for (int i = 0; i < constantPoolCount; i++) {
		int tag = reader.u1At(constantPoolOffsets[i]);
		char[] name = null;
		switch (tag) {
			case ClassFileConstants.MethodRefTag :
			case ClassFileConstants.InterfaceMethodRefTag :
				int constantPoolIndex = reader.u2At(constantPoolOffsets[i] + 3);
				int utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[constantPoolIndex] + 3)];
				name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1));
				break;
			case ClassFileConstants.ClassTag :
				utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[i] + 1)];
				name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1));
				break;
		}
		if (name == null || (name.length > 0 && name[0] == '['))
			break; // skip over array references
		this.typeNames.add(CharOperation.splitOn('/', name));
	}

	// convert type names into import references
	int typeNamesLength = this.typeNames.size();
	ImportReference[] imports = new ImportReference[typeNamesLength];
	char[][][] set = this.typeNames.set;
	int index = 0;
	for (int i = 0, length = set.length; i < length; i++) {
		char[][] typeName = set[i];
		if (typeName != null) {
			imports[index++] = new ImportReference(typeName, new long[typeName.length]/*dummy positions*/, false/*not on demand*/, 0);
		}
	}
	return imports;
}
 
Example 2
Source File: BinaryIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Extract all type, method, field and interface method references from the constant pool
 */
private void extractReferenceFromConstantPool(byte[] contents, ClassFileReader reader) throws ClassFormatException {
	int[] constantPoolOffsets = reader.getConstantPoolOffsets();
	int constantPoolCount = constantPoolOffsets.length;
	for (int i = 1; i < constantPoolCount; i++) {
		int tag = reader.u1At(constantPoolOffsets[i]);
		/**
		 * u1 tag
		 * u2 class_index
		 * u2 name_and_type_index
		 */
		char[] name = null;
		char[] type = null;
		switch (tag) {
			case ClassFileConstants.FieldRefTag :
				// add reference to the class/interface and field name and type
				name = extractName(constantPoolOffsets, reader, i);
				addFieldReference(name);
				break;
			case ClassFileConstants.MethodRefTag :
				// add reference to the class and method name and type
			case ClassFileConstants.InterfaceMethodRefTag :
				// add reference to the interface and method name and type
				name = extractName(constantPoolOffsets, reader, i);
				type = extractType(constantPoolOffsets, reader, i);
				if (CharOperation.equals(INIT, name)) {
					// get class name and see if it's a local type or not
					char[] className = extractClassName(constantPoolOffsets, reader, i);
					boolean localType = false;
					if (className !=  null) {
						for (int c = 0, max = className.length; c < max; c++) {
							switch (className[c]) {
								case '/':
									className[c] = '.';
									break;
								case '$':
									localType = true;
									break;
							}
						}
					}
					// add a constructor reference, use class name to extract arg count if it's a local type to remove synthetic parameter
					addConstructorReference(className, extractArgCount(type, localType?className:null));
				} else {
					// add a method reference
					addMethodReference(name, extractArgCount(type, null));
				}
				break;
			case ClassFileConstants.ClassTag :
				// add a type reference
				name = extractClassReference(constantPoolOffsets, reader, i);
				if (name.length > 0 && name[0] == '[')
					break; // skip over array references
				name = replace('/', '.', name); // so that it looks like java.lang.String
				addTypeReference(name);

				// also add a simple reference on each segment of the qualification (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=24741)
				char[][] qualification = CharOperation.splitOn('.', name);
				for (int j = 0, length = qualification.length; j < length; j++) {
					addNameReference(qualification[j]);
				}
				break;
		}
	}
}