Example usage for org.eclipse.jdt.internal.compiler.classfmt ClassFileReader getFileName

List of usage examples for org.eclipse.jdt.internal.compiler.classfmt ClassFileReader getFileName

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.classfmt ClassFileReader getFileName.

Prototype

@Override
public char[] getFileName() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

/**
 * Asks the engine to compute the selection of the given type
 * from the given context// w ww .  j a v a  2s. c o m
 *
 *  @param typeName char[]
 *      a type name which is to be resolved in the context of a compilation unit.
 *      NOTE: the type name is supposed to be correctly reduced (no whitespaces, no unicodes left)
 *
 *  @param context org.eclipse.jdt.core.IType
 *      the context in which code assist is invoked.
 */
public void selectType(char[] typeName, IType context) throws JavaModelException {
    try {
        this.acceptedAnswer = false;

        // only the type erasure are returned by IType.resolvedType(...)
        if (CharOperation.indexOf('<', typeName) != -1) {
            char[] typeSig = Signature.createCharArrayTypeSignature(typeName, false/*not resolved*/);
            typeSig = Signature.getTypeErasure(typeSig);
            typeName = Signature.toCharArray(typeSig);
        }

        CompilationUnitDeclaration parsedUnit = null;
        TypeDeclaration typeDeclaration = null;
        org.eclipse.jdt.core.ICompilationUnit cu = context.getCompilationUnit();
        if (cu != null) {
            IType[] topLevelTypes = cu.getTypes();
            int length = topLevelTypes.length;
            SourceTypeElementInfo[] topLevelInfos = new SourceTypeElementInfo[length];
            for (int i = 0; i < length; i++) {
                topLevelInfos[i] = (SourceTypeElementInfo) ((SourceType) topLevelTypes[i]).getElementInfo();
            }
            CompilationResult result = new CompilationResult(
                    (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) cu, 1, 1,
                    this.compilerOptions.maxProblemsPerUnit);
            int flags = SourceTypeConverter.FIELD_AND_METHOD | SourceTypeConverter.MEMBER_TYPE;
            if (context.isAnonymous() || context.isLocal())
                flags |= SourceTypeConverter.LOCAL_TYPE;
            parsedUnit = SourceTypeConverter.buildCompilationUnit(topLevelInfos, flags,
                    this.parser.problemReporter(), result);
            if (parsedUnit != null && parsedUnit.types != null) {
                if (DEBUG) {
                    System.out.println("SELECTION - Diet AST :"); //$NON-NLS-1$
                    System.out.println(parsedUnit.toString());
                }
                // find the type declaration that corresponds to the original source type
                typeDeclaration = new ASTNodeFinder(parsedUnit).findType(context);
            }
        } else { // binary type
            ClassFile classFile = (ClassFile) context.getClassFile();
            ClassFileReader reader = (ClassFileReader) classFile.getBinaryTypeInfo(
                    (IFile) null/*classFile.resource()*/,
                    false/*don't fully initialize so as to keep constant pool (used below)*/);
            CompilationResult result = new CompilationResult(reader.getFileName(), 1, 1,
                    this.compilerOptions.maxProblemsPerUnit);
            parsedUnit = new CompilationUnitDeclaration(this.parser.problemReporter(), result, 0);
            HashSetOfCharArrayArray typeNames = new HashSetOfCharArrayArray();

            BinaryTypeConverter converter = new BinaryTypeConverter(this.parser.problemReporter(), result,
                    typeNames);
            typeDeclaration = converter.buildTypeDeclaration(context, parsedUnit);
            parsedUnit.imports = converter.buildImports(reader);
        }

        if (typeDeclaration != null) {

            // add fake field with the type we're looking for
            // note: since we didn't ask for fields above, there is no field defined yet
            FieldDeclaration field = new FieldDeclaration();
            int dot;
            if ((dot = CharOperation.lastIndexOf('.', typeName)) == -1) {
                this.selectedIdentifier = typeName;
                field.type = new SelectionOnSingleTypeReference(typeName, -1);
                // position not used
            } else {
                char[][] previousIdentifiers = CharOperation.splitOn('.', typeName, 0, dot);
                char[] selectionIdentifier = CharOperation.subarray(typeName, dot + 1, typeName.length);
                this.selectedIdentifier = selectionIdentifier;
                field.type = new SelectionOnQualifiedTypeReference(previousIdentifiers, selectionIdentifier,
                        new long[previousIdentifiers.length + 1]);
            }
            field.name = "<fakeField>".toCharArray(); //$NON-NLS-1$
            typeDeclaration.fields = new FieldDeclaration[] { field };

            // build bindings
            this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
            if ((this.unitScope = parsedUnit.scope) != null) {
                try {
                    // build fields
                    // note: this builds fields only in the parsed unit (the buildFieldsAndMethods flag is not passed along)
                    this.lookupEnvironment.completeTypeBindings(parsedUnit, true);

                    // resolve
                    parsedUnit.scope.faultInTypes();
                    parsedUnit.resolve();
                } catch (SelectionNodeFound e) {
                    if (e.binding != null) {
                        if (DEBUG) {
                            System.out.println("SELECTION - Selection binding :"); //$NON-NLS-1$
                            System.out.println(e.binding.toString());
                        }
                        // if null then we found a problem in the selection node
                        selectFrom(e.binding, parsedUnit, e.isDeclaration);
                    }
                }
            }
        }
        if (this.noProposal && this.problem != null) {
            this.requestor.acceptError(this.problem);
        }
    } catch (AbortCompilation e) { // ignore this exception for now since it typically means we cannot find java.lang.Object
    } finally {
        reset(true);
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.model.MethodModel.java

License:Open Source License

/**
 * Ensure we have bytes and constantPoolOffsets ready to use.
 *//*  w ww  .j a  v  a  2  s.c o m*/
private void setupByteCode(boolean bytesRequired) {
    if (this._bytes == null || this._constantPoolOffsets == null) {
        try {
            MethodBinding binding = (this._binding == null) ? this._decl.binding : this._binding;
            assert binding.declaringClass.isTeam();

            ClassFileReader reader = null;
            if (this._bytes == null) {
                if (this._classFile == null && this._decl != null) {
                    char[] className = binding.declaringClass.constantPoolName();
                    this._classFile = (ClassFile) this._decl.compilationResult.compiledTypes.get(className);
                    if (this._classFile != null && !this._classFile.isForType(binding.declaringClass)) {
                        this._classFile = null; // has been reset thus cannot be used for this type any more.
                    }
                }
                // here we made the best attempt to obtain a classfile, use it if possible:
                if (this._classFile != null && this._classFile.isForType(this._binding.declaringClass)) {
                    this._bytes = this._classFile.getBytes();
                    this._structOffset += this._classFile.headerOffset; // structOffset did not yet include the headerOffset
                    int olen = this._classFile.constantPool.currentIndex;
                    System.arraycopy(this._classFile.constantPool.offsets, 0,
                            this._constantPoolOffsets = new int[olen], 0, olen);
                    this._classFile = null; // don't use any more
                    return;
                }
            }
            if (this._bytes != null) {
                // create a reader for in-memory bytes in order to recalculate constant pool offsets
                reader = new ClassFileReader(this._bytes, RoleModel.NO_SOURCE_FILE); // STATE_BYTECODE_PREPARED
            } else {
                // Currently only team-ctors use a MethodModel for byte code retrieval.
                // Use the stored file name for reading the byte code from disc:
                if (binding.declaringClass.isTeam())
                    reader = binding.declaringClass.getTeamModel().read();
                if (reader == null) {
                    if (bytesRequired)
                        throw new InternalCompilerError(
                                "No byte code available for " + new String(binding.readableName())); //$NON-NLS-1$
                    return;
                }
                this._bytes = reader.getBytes();
            }
            this._classFile = null; // don't use any more
            // now we have both a reader and bytes
            this._constantPoolOffsets = reader.getConstantPoolOffsets();
            // find bytecode offset of this method:
            char[] mySignature = this._binding.signature();
            for (IBinaryMethod m : reader.getMethods()) {
                if (CharOperation.equals(m.getSelector(), this._binding.selector)
                        && CharOperation.equals(m.getMethodDescriptor(), mySignature)) {
                    this._structOffset = ((MethodInfo) m).getStructOffset();
                    return;
                }
            }
            if (bytesRequired)
                throw new InternalCompilerError("Method " + String.valueOf(this._binding.readableName()) //$NON-NLS-1$
                        + "not found in class file " + String.valueOf(reader.getFileName())); //$NON-NLS-1$
        } catch (ClassFormatException ex) {
            throw new InternalCompilerError(ex.getMessage());
        } catch (IOException ex) {
            throw new InternalCompilerError(ex.getMessage());
        }
    }

}

From source file:org.springframework.ide.eclipse.core.java.TypeStructure.java

License:Open Source License

public TypeStructure(ClassFileReader cfr) {
    //It shouldn't really matter what arguments we provide to the constructor
    // since this class implements all the methods, except for getTypeAnnotations,
    // which just returns 'null'. So all that really matters is we pass in 
    // something that doesn't make the super constructor crash. We will nevertheless
    // try our best to pass in sensible values.
    super(cfr.getModifiers(), computeQualification(cfr), cfr.getSourceName(), cfr.getEnclosingTypeName(),
            (char[][]) null, '?' //?? appears not used in super class, so not sure what its for
    );//from w ww. j a  v a 2s . c om

    this.enclosingTypeName = cfr.getEnclosingTypeName();
    this.isLocal = cfr.isLocal();
    this.isAnonymous = cfr.isAnonymous();
    this.isMember = cfr.isMember();
    this.sourceFileName = cfr.sourceFileName();
    this.fileName = cfr.getFileName();
    this.tagBits = cfr.getTagBits();
    this.isBinaryType = cfr.isBinaryType();
    this.binFields = cfr.getFields();
    if (binFields == null)
        binFields = NoField;
    this.binMethods = cfr.getMethods();
    if (binMethods == null)
        binMethods = NoMethod;
    this.memberTypes = cfr.getMemberTypes();
    this.annotations = cfr.getAnnotations();
    this.sourceName = cfr.getSourceName();
    this.className = cfr.getName(); // slashes...
    this.modifiers = cfr.getModifiers();
    this.genericSignature = cfr.getGenericSignature();
    // if (this.genericSignature.length == 0) {
    // this.genericSignature = null;
    // }
    this.superclassName = cfr.getSuperclassName(); // slashes...
    interfaces = cfr.getInterfaceNames();

}