org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException. 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: ClassFile.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
private IBinaryType getJarBinaryTypeInfo(PackageFragment pkg, boolean fullyInitialize) throws CoreException, IOException, ClassFormatException {
	JarPackageFragmentRoot root = (JarPackageFragmentRoot) pkg.getParent();
	ZipFile zip = null;
	try {
		zip = root.getJar();
		String entryName = Util.concatWith(pkg.names, getElementName(), '/');
		ZipEntry ze = zip.getEntry(entryName);
		if (ze != null) {
			byte contents[] = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
			String fileName = root.getHandleIdentifier() + IDependent.JAR_FILE_ENTRY_SEPARATOR + entryName;
			return new ClassFileReader(contents, fileName.toCharArray(), fullyInitialize);
		}
	} finally {
		JavaModelManager.getJavaModelManager().closeZipFile(zip);
	}
	return null;
}
 
Example #2
Source File: AbstractSisuIndexMojo.java    From takari-lifecycle with Eclipse Public License 1.0 6 votes vote down vote up
Map<String, String> gleanNamedType(File classfile) throws IOException {
  // use jdt class reader to avoid extra runtime dependency, otherwise could use asm

  try {
    ClassFileReader type = ClassFileReader.read(classfile);
    IBinaryAnnotation[] annotations = type.getAnnotations();
    if (annotations != null) {
      for (IBinaryAnnotation annotation : annotations) {
        if ("Ljavax/inject/Named;".equals(new String(annotation.getTypeName()))) {
          return Collections.singletonMap(new String(type.getName()).replace('/', '.'), null);
        }
      }
    }
  } catch (ClassFormatException e) {
    // silently ignore classes we can't read/parse
  }

  return null;
}
 
Example #3
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ClassFileReader newClassFileReader(IResource resource) throws CoreException, ClassFormatException, IOException {
	InputStream in = null;
	try {
		in = ((IFile) resource).getContents(true);
		return ClassFileReader.read(in, resource.getFullPath().toString());
	} finally {
		if (in != null)
			in.close();
	}
}
 
Example #4
Source File: AppCompiler.java    From actframework with Apache License 2.0 5 votes vote down vote up
private NameEnvironmentAnswer findType(String type) {
    try {
        byte[] bytes;
        Source source;
        if (Act.isDev()) {
            source = classLoader.source(type);
            if (null != source) {
                return new NameEnvironmentAnswer(source.compilationUnit(), null);
            }
        }
        bytes = classLoader.enhancedBytecode(type);
        if (bytes != null) {
            ClassFileReader classFileReader = new ClassFileReader(bytes, type.toCharArray(), true);
            return new NameEnvironmentAnswer(classFileReader, null);
        } else {
            if (type.startsWith("org.osgl") || type.startsWith("java.") || type.startsWith("javax.")) {
                return null;
            }
        }
        if (Act.isDev()) {
            return null;
        }
        source = classLoader.source(type);
        if (null == source) {
            return null;
        } else {
            return new NameEnvironmentAnswer(source.compilationUnit(), null);
        }
    } catch (ClassFormatException e) {
        throw E.unexpected(e);
    }
}
 
Example #5
Source File: CompilerJdt.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
private byte[] digestClassFile(File outputFile, byte[] definition) {
  try {
    ClassFileReader reader = new ClassFileReader(definition, outputFile.getAbsolutePath().toCharArray());
    return digester.digest(reader);
  } catch (ClassFormatException e) {
    // ignore this class
  }
  return null;
}
 
Example #6
Source File: ClasspathJar.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public NameEnvironmentAnswer findType(String packageName, String typeName, AccessRestriction accessRestriction) {
  try {
    String qualifiedFileName = packageName + "/" + typeName + SUFFIX_STRING_class;
    ClassFileReader reader = ClassFileReader.read(this.zipFile, qualifiedFileName);
    if (reader != null) {
      return new NameEnvironmentAnswer(reader, accessRestriction);
    }
  } catch (ClassFormatException | IOException e) {
    // treat as if class file is missing
  }
  return null;
}
 
Example #7
Source File: ClasspathDirectory.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public NameEnvironmentAnswer findType(String packageName, String typeName, AccessRestriction accessRestriction) {
  try {
    Path classFile = getFile(packageName, typeName);
    if (classFile != null) {
      try (InputStream is = Files.newInputStream(classFile)) {
        return new NameEnvironmentAnswer(ClassFileReader.read(is, classFile.getFileName().toString(), false), accessRestriction);
      }
    }
  } catch (ClassFormatException | IOException e) {
    // treat as if type is missing
  }
  return null;
}
 
Example #8
Source File: BinaryIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private char[] decodeFieldType(char[] signature) throws ClassFormatException {
	if (signature == null) return null;
	int arrayDim = 0;
	for (int i = 0, max = signature.length; i < max; i++) {
		switch(signature[i]) {
			case 'B':
				if (arrayDim > 0)
					return convertToArrayType(BYTE, arrayDim);
				return BYTE;

			case 'C':
				if (arrayDim > 0)
					return convertToArrayType(CHAR, arrayDim);
				return CHAR;

			case 'D':
				if (arrayDim > 0)
					return convertToArrayType(DOUBLE, arrayDim);
				return DOUBLE;

			case 'F':
				if (arrayDim > 0)
					return convertToArrayType(FLOAT, arrayDim);
				return FLOAT;

			case 'I':
				if (arrayDim > 0)
				return convertToArrayType(INT, arrayDim);
				return INT;

			case 'J':
				if (arrayDim > 0)
					return convertToArrayType(LONG, arrayDim);
				return LONG;

			case 'L':
				int indexOfSemiColon = CharOperation.indexOf(';', signature, i+1);
				if (indexOfSemiColon == -1) throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
				if (arrayDim > 0) {
					return convertToArrayType(replace('/','.',CharOperation.subarray(signature, i + 1, indexOfSemiColon)), arrayDim);
				}
				return replace('/','.',CharOperation.subarray(signature, i + 1, indexOfSemiColon));

			case 'S':
				if (arrayDim > 0)
					return convertToArrayType(SHORT, arrayDim);
				return SHORT;

			case 'Z':
				if (arrayDim > 0)
					return convertToArrayType(BOOLEAN, arrayDim);
				return BOOLEAN;

			case 'V':
				return VOID;

			case '[':
				arrayDim++;
				break;

			default:
				throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
		}
	}
	return null;
}
 
Example #9
Source File: BinaryIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private char[] decodeReturnType(char[] signature) throws ClassFormatException {
	if (signature == null) return null;
	int indexOfClosingParen = CharOperation.lastIndexOf(')', signature);
	if (indexOfClosingParen == -1) throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
	int arrayDim = 0;
	for (int i = indexOfClosingParen + 1, max = signature.length; i < max; i++) {
		switch(signature[i]) {
			case 'B':
				if (arrayDim > 0)
					return convertToArrayType(BYTE, arrayDim);
				return BYTE;

			case 'C':
				if (arrayDim > 0)
					return convertToArrayType(CHAR, arrayDim);
				return CHAR;

			case 'D':
				if (arrayDim > 0)
					return convertToArrayType(DOUBLE, arrayDim);
				return DOUBLE;

			case 'F':
				if (arrayDim > 0)
					return convertToArrayType(FLOAT, arrayDim);
				return FLOAT;

			case 'I':
				if (arrayDim > 0)
					return convertToArrayType(INT, arrayDim);
				return INT;

			case 'J':
				if (arrayDim > 0)
					return convertToArrayType(LONG, arrayDim);
				return LONG;

			case 'L':
				int indexOfSemiColon = CharOperation.indexOf(';', signature, i+1);
				if (indexOfSemiColon == -1) throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
				if (arrayDim > 0) {
					return convertToArrayType(replace('/','.',CharOperation.subarray(signature, i + 1, indexOfSemiColon)), arrayDim);
				}
				return replace('/','.',CharOperation.subarray(signature, i + 1, indexOfSemiColon));

			case 'S':
				if (arrayDim > 0)
					return convertToArrayType(SHORT, arrayDim);
				return SHORT;

			case 'Z':
				if (arrayDim > 0)
					return convertToArrayType(BOOLEAN, arrayDim);
				return BOOLEAN;

			case 'V':
				return VOID;

			case '[':
				arrayDim++;
				break;

			default:
				throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
		}
	}
	return null;
}
 
Example #10
Source File: BinaryIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int extractArgCount(char[] signature, char[] className) throws ClassFormatException {
	int indexOfClosingParen = CharOperation.lastIndexOf(')', signature);
	if (indexOfClosingParen == 1) {
		// there is no parameter
		return 0;
	}
	if (indexOfClosingParen == -1) {
		throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
	}
	int parameterTypesCounter = 0;
	for (int i = 1; i < indexOfClosingParen; i++) {
		switch(signature[i]) {
			case 'B':
			case 'C':
			case 'D':
			case 'F':
			case 'I':
			case 'J':
			case 'S':
			case 'Z':
				parameterTypesCounter++;
				break;
			case 'L':
				int indexOfSemiColon = CharOperation.indexOf(';', signature, i+1);
				if (indexOfSemiColon == -1) throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
				// verify if first parameter is synthetic
				if (className != null && parameterTypesCounter == 0) {
					char[] classSignature = Signature.createCharArrayTypeSignature(className, true);
					int length = indexOfSemiColon-i+1;
					if (classSignature.length > (length+1)) {
						// synthetic means that parameter type has same signature than given class
						for (int j=i, k=0; j<indexOfSemiColon; j++, k++) {
							if (!(signature[j] == classSignature[k] || (signature[j] == '/' && classSignature[k] == '.' ))) {
								parameterTypesCounter++;
								break;
							}
						}
					} else {
						parameterTypesCounter++;
					}
					className = null; // do not verify following parameters
				} else {
					parameterTypesCounter++;
				}
				i = indexOfSemiColon;
				break;
			case '[':
				break;
			default:
				throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
		}
	}
	return parameterTypesCounter;
}
 
Example #11
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;
		}
	}
}