List of usage examples for org.eclipse.jdt.internal.core SourceTypeElementInfo getNameSourceStart
@Override public int getNameSourceStart()
From source file:org.eclipse.che.jdt.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException { this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0); // not filled at this point 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();/*from ww w. ja v a 2 s.c o m*/ if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) { // If more than 10 annotations, diet parse as this is faster, but not if // the client wants local and anonymous types to be converted (https://bugs.eclipse.org/bugs/show_bug.cgi?id=254738) // 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); } }
From source file:org.eclipse.che.jdt.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult) throws JavaModelException { SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo(); if (typeInfo.isAnonymousMember()) throw new AnonymousMemberFound(); /* create type declaration - can be member type */ TypeDeclaration type = new TypeDeclaration(compilationResult); if (typeInfo.getEnclosingType() == null) { if (typeHandle.isAnonymous()) { type.name = CharOperation.NO_CHAR; type.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType); } else {/*from w ww . j av a 2 s . co m*/ if (typeHandle.isLocal()) { type.bits |= ASTNode.IsLocalType; } } } else { type.bits |= ASTNode.IsMemberType; } if ((type.bits & ASTNode.IsAnonymousType) == 0) { type.name = typeInfo.getName(); } type.name = typeInfo.getName(); int start, end; // only positions available type.sourceStart = start = typeInfo.getNameSourceStart(); type.sourceEnd = end = typeInfo.getNameSourceEnd(); type.modifiers = typeInfo.getModifiers(); type.declarationSourceStart = typeInfo.getDeclarationSourceStart(); type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd(); type.bodyEnd = type.declarationSourceEnd; // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ type.annotations = convertAnnotations(typeHandle); } /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we must internalize type variables and observe any parameterization of super class and/or super interfaces in order to be able to detect overriding in the presence of generics. */ char[][] typeParameterNames = typeInfo.getTypeParameterNames(); if (typeParameterNames.length > 0) { int parameterCount = typeParameterNames.length; char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds(); type.typeParameters = new TypeParameter[parameterCount]; for (int i = 0; i < parameterCount; i++) { type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end); } } /* set superclass and superinterfaces */ if (typeInfo.getSuperclassName() != null) { type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end, true /* include generics */); type.superclass.bits |= ASTNode.IsSuperType; } char[][] interfaceNames = typeInfo.getInterfaceNames(); int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length; if (interfaceCount > 0) { type.superInterfaces = new TypeReference[interfaceCount]; for (int i = 0; i < interfaceCount; i++) { type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end, true /* include generics */); type.superInterfaces[i].bits |= ASTNode.IsSuperType; } } /* convert member types */ if ((this.flags & MEMBER_TYPE) != 0) { SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles(); int sourceMemberTypeCount = sourceMemberTypes.length; type.memberTypes = new TypeDeclaration[sourceMemberTypeCount]; for (int i = 0; i < sourceMemberTypeCount; i++) { type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult); type.memberTypes[i].enclosingType = type; } } /* convert intializers and fields*/ InitializerElementInfo[] initializers = null; int initializerCount = 0; if ((this.flags & LOCAL_TYPE) != 0) { initializers = typeInfo.getInitializers(); initializerCount = initializers.length; } SourceField[] sourceFields = null; int sourceFieldCount = 0; if ((this.flags & FIELD) != 0) { sourceFields = typeInfo.getFieldHandles(); sourceFieldCount = sourceFields.length; } int length = initializerCount + sourceFieldCount; if (length > 0) { type.fields = new FieldDeclaration[length]; for (int i = 0; i < initializerCount; i++) { type.fields[i] = convert(initializers[i], compilationResult); } int index = 0; for (int i = initializerCount; i < length; i++) { type.fields[i] = convert(sourceFields[index++], type, compilationResult); } } /* convert methods - need to add default constructor if necessary */ boolean needConstructor = (this.flags & CONSTRUCTOR) != 0; boolean needMethod = (this.flags & METHOD) != 0; if (needConstructor || needMethod) { SourceMethod[] sourceMethods = typeInfo.getMethodHandles(); int sourceMethodCount = sourceMethods.length; /* source type has a constructor ? */ /* by default, we assume that one is needed. */ int extraConstructor = 0; int methodCount = 0; int kind = TypeDeclaration.kind(type.modifiers); boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL; if (!isAbstract) { extraConstructor = needConstructor ? 1 : 0; for (int i = 0; i < sourceMethodCount; i++) { if (sourceMethods[i].isConstructor()) { if (needConstructor) { extraConstructor = 0; // Does not need the extra constructor since one constructor already exists. methodCount++; } } else if (needMethod) { methodCount++; } } } else { methodCount = needMethod ? sourceMethodCount : 0; } type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor]; if (extraConstructor != 0) { // add default constructor in first position type.methods[0] = type.createDefaultConstructor(false, false); } int index = 0; boolean hasAbstractMethods = false; for (int i = 0; i < sourceMethodCount; i++) { SourceMethod sourceMethod = sourceMethods[i]; SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) sourceMethod.getElementInfo(); boolean isConstructor = methodInfo.isConstructor(); if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) { hasAbstractMethods = true; } if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) { AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult); if (isAbstract || method.isAbstract()) { // fix-up flag method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody; } type.methods[extraConstructor + index++] = method; } } if (hasAbstractMethods) type.bits |= ASTNode.HasAbstractMethods; } return type; }
From source file:org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException { // GROOVY start /* old {/* w w w .j a v a2 s . co m*/ this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0); } new */ this.unit = LanguageSupportFactory.newCompilationUnitDeclaration( (ICompilationUnit) ((SourceTypeElementInfo) sourceTypes[0]).getHandle().getCompilationUnit(), this.problemReporter, compilationResult, 0); // GROOVY end // not filled at this point 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; // GROOVY start // trying to avoid building an incorrect TypeDeclaration below (when it should be a GroovyTypeDeclaration). // similar to code below that creates the Parser and calls dietParse // FIXASC think about doing the necessary rewrite below rather than this - does it make things too slow? // final boolean isInterestingProject = LanguageSupportFactory.isInterestingProject(compilationResult.getCompilationUnit().getjavaBuilder.getProject()); // GROOVY should be 'true' here? if (LanguageSupportFactory.isInterestingSourceFile(new String(compilationResult.getFileName()))) { try { return LanguageSupportFactory .getParser(this, this.problemReporter.options, this.problemReporter, true, 3) .dietParse(this.cu, compilationResult); } catch (Throwable t) { t.printStackTrace(); } } // GROOVY end if (this.has1_5Compliance && ((CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo()).annotationNumber > 10) { // experimental value // If more than 10 annotations, diet parse as this is faster, but not if // the client wants local and anonymous types to be converted (https://bugs.eclipse.org/bugs/show_bug.cgi?id=254738) if ((this.flags & LOCAL_TYPE) == 0) { return new Parser(this.problemReporter, true).dietParse(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); } }