Search in sources :

Example 1 with Parser

use of org.eclipse.jdt.internal.compiler.parser.Parser in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a field source element into a parsed field declaration
	 */
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;
    field.sourceEnd = end;
    field.declarationSourceStart = fieldInfo.getDeclarationSourceStart();
    field.declarationSourceEnd = fieldInfo.getDeclarationSourceEnd();
    int modifiers = fieldInfo.getModifiers();
    boolean isEnumConstant = (modifiers & ClassFileConstants.AccEnum) != 0;
    if (isEnumConstant) {
        // clear AccEnum bit onto AST (binding will add it)
        field.modifiers = modifiers & ~ClassFileConstants.AccEnum;
    } 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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SourceFieldElementInfo(org.eclipse.jdt.internal.core.SourceFieldElementInfo) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 2 with Parser

use of org.eclipse.jdt.internal.compiler.parser.Parser in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a set of source element types into a parsed compilation unit declaration
	 * The argument types are then all grouped in the same unit. The argument types must
	 * at least contain one type.
	 */
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException {
    this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
    if (sourceTypes.length == 0)
        return this.unit;
    SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
    org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
    this.cu = (ICompilationUnit) cuHandle;
    final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo();
    if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
        // Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
        if ((this.flags & LOCAL_TYPE) == 0) {
            return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
        } else {
            return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
        }
    }
    /* only positions available */
    int start = topLevelTypeInfo.getNameSourceStart();
    int end = topLevelTypeInfo.getNameSourceEnd();
    /* convert package and imports */
    String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
    if (packageName.length > 0)
        // if its null then it is defined in the default package
        this.unit.currentPackage = createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault);
    IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
    int importCount = importDeclarations.length;
    this.unit.imports = new ImportReference[importCount];
    for (int i = 0; i < importCount; i++) {
        ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
        ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
        String nameWithoutStar = importDeclaration.getNameWithoutStar();
        this.unit.imports[i] = createImportReference(Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()), sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(), importDeclaration.isOnDemand(), sourceImport.getModifiers());
    }
    /* convert type(s) */
    try {
        int typeCount = sourceTypes.length;
        final TypeDeclaration[] types = new TypeDeclaration[typeCount];
        /*
			 * We used a temporary types collection to prevent this.unit.types from being null during a call to
			 * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
			 */
        for (int i = 0; i < typeCount; i++) {
            SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
            types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
        }
        this.unit.types = types;
        return this.unit;
    } catch (AnonymousMemberFound e) {
        return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
    }
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) ISourceImport(org.eclipse.jdt.internal.compiler.env.ISourceImport) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) SourceTypeElementInfo(org.eclipse.jdt.internal.core.SourceTypeElementInfo) CompilationUnitElementInfo(org.eclipse.jdt.internal.core.CompilationUnitElementInfo) ImportDeclaration(org.eclipse.jdt.internal.core.ImportDeclaration) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 3 with Parser

use of org.eclipse.jdt.internal.compiler.parser.Parser in project android by JetBrains.

the class LombokPsiConverterTest method parse.

@Nullable
private static Node parse(String code) {
    CompilerOptions options = new CompilerOptions();
    options.complianceLevel = options.sourceLevel = options.targetJDK = ClassFileConstants.JDK1_7;
    options.parseLiteralExpressionsAsConstants = true;
    ProblemReporter problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitOnFirstError(), options, new DefaultProblemFactory());
    Parser parser = new Parser(problemReporter, options.parseLiteralExpressionsAsConstants);
    parser.javadocParser.checkDocComment = false;
    EcjTreeConverter converter = new EcjTreeConverter();
    org.eclipse.jdt.internal.compiler.batch.CompilationUnit sourceUnit = new org.eclipse.jdt.internal.compiler.batch.CompilationUnit(code.toCharArray(), "unitTest", "UTF-8");
    CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
    CompilationUnitDeclaration unit = parser.parse(sourceUnit, compilationResult);
    if (unit == null) {
        return null;
    }
    converter.visit(code, unit);
    List<? extends Node> nodes = converter.getAll();
    for (lombok.ast.Node node : nodes) {
        if (node instanceof lombok.ast.CompilationUnit) {
            return node;
        }
    }
    return null;
}
Also used : CompilationUnit(lombok.ast.CompilationUnit) ProblemReporter(org.eclipse.jdt.internal.compiler.problem.ProblemReporter) Node(lombok.ast.Node) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) EcjTreeConverter(lombok.ast.ecj.EcjTreeConverter) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult) Nullable(com.android.annotations.Nullable)

Aggregations

Parser (org.eclipse.jdt.internal.compiler.parser.Parser)3 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)2 Nullable (com.android.annotations.Nullable)1 CompilationUnit (lombok.ast.CompilationUnit)1 Node (lombok.ast.Node)1 EcjTreeConverter (lombok.ast.ecj.EcjTreeConverter)1 IImportDeclaration (org.eclipse.jdt.core.IImportDeclaration)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 ISourceImport (org.eclipse.jdt.internal.compiler.env.ISourceImport)1 ISourceType (org.eclipse.jdt.internal.compiler.env.ISourceType)1 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)1 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)1 ProblemReporter (org.eclipse.jdt.internal.compiler.problem.ProblemReporter)1 CompilationUnitElementInfo (org.eclipse.jdt.internal.core.CompilationUnitElementInfo)1 ImportDeclaration (org.eclipse.jdt.internal.core.ImportDeclaration)1 PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)1