Example usage for org.eclipse.jdt.internal.compiler.ast FieldDeclaration FieldDeclaration

List of usage examples for org.eclipse.jdt.internal.compiler.ast FieldDeclaration FieldDeclaration

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast FieldDeclaration FieldDeclaration.

Prototype

public FieldDeclaration() 

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//  www.j  a  v a2  s. 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:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private FieldDeclaration convert(SourceField fieldHandle, TypeDeclaration type,
        CompilationResult compilationResult) throws JavaModelException {

    SourceFieldElementInfo fieldInfo = (SourceFieldElementInfo) fieldHandle.getElementInfo();
    FieldDeclaration field = new FieldDeclaration();

    int start = fieldInfo.getNameSourceStart();
    int end = fieldInfo.getNameSourceEnd();

    field.name = fieldHandle.getElementName().toCharArray();
    field.sourceStart = start;// ww  w  .j av a 2 s . co m
    field.sourceEnd = end;
    field.declarationSourceStart = fieldInfo.getDeclarationSourceStart();
    field.declarationSourceEnd = fieldInfo.getDeclarationSourceEnd();
    int modifiers = fieldInfo.getModifiers();
    boolean isEnumConstant = (modifiers & ClassFileConstants.AccEnum) != 0;
    if (isEnumConstant) {
        field.modifiers = modifiers & ~ClassFileConstants.AccEnum; // clear AccEnum bit onto AST (binding will add it)
    } else {
        field.modifiers = modifiers;
        field.type = createTypeReference(fieldInfo.getTypeName(), start, end);
    }

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        field.annotations = convertAnnotations(fieldHandle);
    }

    /* conversion of field constant */
    if ((this.flags & FIELD_INITIALIZATION) != 0) {
        char[] initializationSource = fieldInfo.getInitializationSource();
        if (initializationSource != null) {
            if (this.parser == null) {
                this.parser = new Parser(this.problemReporter, true);
            }
            this.parser.parse(field, type, this.unit, initializationSource);
        }
    }

    /* conversion of local and anonymous types */
    if ((this.flags & LOCAL_TYPE) != 0) {
        IJavaElement[] children = fieldInfo.getChildren();
        int childrenLength = children.length;
        if (childrenLength == 1) {
            field.initialization = convert(children[0], isEnumConstant ? field : null, compilationResult);
        } else if (childrenLength > 1) {
            ArrayInitializer initializer = new ArrayInitializer();
            field.initialization = initializer;
            Expression[] expressions = new Expression[childrenLength];
            initializer.expressions = expressions;
            for (int i = 0; i < childrenLength; i++) {
                expressions[i] = convert(children[i], isEnumConstant ? field : null, compilationResult);
            }
        }
    }
    return field;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

private FieldDeclaration convert(IField field, IType type) throws JavaModelException {
    TypeReference typeReference = createTypeReference(field.getTypeSignature());
    if (typeReference == null)
        return null;
    FieldDeclaration fieldDeclaration = new FieldDeclaration();

    fieldDeclaration.name = field.getElementName().toCharArray();
    fieldDeclaration.type = typeReference;
    fieldDeclaration.modifiers = field.getFlags();

    return fieldDeclaration;
}

From source file:org.eclipse.ajdt.core.parserbridge.ITDInserter.java

License:Open Source License

private FieldDeclaration createField(IProgramElement field, TypeDeclaration type) {
    FieldDeclaration decl = new FieldDeclaration();
    String[] split = field.getName().split("\\.");
    decl.name = split[split.length - 1].toCharArray();
    decl.type = createTypeReference(field.getCorrespondingType(true));
    decl.modifiers = field.getRawModifiers();
    return decl;/*from ww  w  .j  a v  a2  s  . c  o  m*/
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstConverter.java

License:Open Source License

public static FieldDeclaration createField(FieldBinding fieldBinding, TypeDeclaration roleDeclaration,
        AstGenerator gen) {/*  w w w. ja v  a 2 s . c o  m*/

    if (fieldBinding == null)
        return null;

    FieldDeclaration fieldDeclaration = new FieldDeclaration();
    fieldDeclaration.type = gen.typeReference(fieldBinding.type);

    fieldDeclaration.modifiers = (fieldBinding.modifiers & ~ExtraCompilerModifiers.AccBlankFinal); // this modifier is not used on fieldDecl (AST), overlaps with AccReadOnly
    fieldDeclaration.name = fieldBinding.name;
    if (fieldBinding.copyInheritanceSrc != null)
        fieldDeclaration.copyInheritanceSrc = fieldBinding.copyInheritanceSrc;
    else
        fieldDeclaration.copyInheritanceSrc = fieldBinding;

    AnnotationBinding[] annotBindings = fieldBinding.getAnnotations();
    if (annotBindings != Binding.NO_ANNOTATIONS) {
        ProblemReporter pr = fieldBinding.isStatic() ? roleDeclaration.staticInitializerScope.problemReporter()
                : roleDeclaration.initializerScope.problemReporter();
        Annotation[] annotDecls = new Annotation[annotBindings.length];
        boolean hasAnnotationError = false;
        for (int i = 0; i < annotBindings.length; i++) {
            AnnotationBinding binding = annotBindings[i];
            ElementValuePair[] elementValuePairs = binding.getElementValuePairs();
            char[][] annotTypeName = binding.getAnnotationType().compoundName;
            if (elementValuePairs == Binding.NO_ELEMENT_VALUE_PAIRS) {
                annotDecls[i] = gen.markerAnnotation(annotTypeName);
            } else {
                int numPairs = elementValuePairs.length;
                char[][] names = new char[numPairs][];
                Expression[] values = new Expression[numPairs];
                for (int j = 0; j < names.length; j++) {
                    names[j] = elementValuePairs[j].getName();
                    Object elementValue = elementValuePairs[j].getValue();
                    values[j] = annotationValues(elementValue, gen, pr);
                }
                if (values.length == 0 || values[0] == null) {
                    pr.unexpectedAnnotationStructure(annotTypeName, fieldBinding.name, gen.sourceStart,
                            gen.sourceEnd);
                    hasAnnotationError = true;
                } else if (numPairs == 1 && CharOperation.equals(names[0], TypeConstants.VALUE)) {
                    annotDecls[i] = gen.singleMemberAnnotation(annotTypeName, values[0]);
                } else {
                    annotDecls[i] = gen.normalAnnotation(annotTypeName, names, values);
                }
            }
        }
        if (!hasAnnotationError)
            fieldDeclaration.annotations = annotDecls;
    }

    //field initializations are copied using a RoleInitializationMethod
    return fieldDeclaration;
}