Search in sources :

Example 1 with HashtableOfObject

use of org.eclipse.jdt.internal.compiler.util.HashtableOfObject in project bazel-jdt-java-toolchain by salesforce.

the class EclipseCompilerImpl method getCompilationUnits.

@Override
public CompilationUnit[] getCompilationUnits() {
    // This method is largely a copy of Main#getCompilationUnits()
    if (this.compilationUnits == null)
        return EclipseCompilerImpl.NO_UNITS;
    HashtableOfObject knownFileNames = new HashtableOfObject();
    ArrayList<CompilationUnit> units = new ArrayList<>();
    for (int round = 0; round < 2; round++) {
        int i = 0;
        for (final JavaFileObject javaFileObject : this.compilationUnits) {
            String name = javaFileObject.getName();
            char[] charName = name.toCharArray();
            boolean isModuleInfo = CharOperation.endsWith(charName, TypeConstants.MODULE_INFO_FILE_NAME);
            if (isModuleInfo == (round == 0)) {
                // 1st round: modules, 2nd round others (to ensure populating pathToModCU well in time)
                if (knownFileNames.get(charName) != null)
                    // $NON-NLS-1$
                    throw new IllegalArgumentException(this.bind("unit.more", name));
                knownFileNames.put(charName, charName);
                boolean found = false;
                try {
                    if (this.fileManager.hasLocation(StandardLocation.SOURCE_PATH)) {
                        found = this.fileManager.contains(StandardLocation.SOURCE_PATH, javaFileObject);
                    }
                    if (!found) {
                        if (this.fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH)) {
                            found = this.fileManager.contains(StandardLocation.MODULE_SOURCE_PATH, javaFileObject);
                        }
                    }
                } catch (IOException e) {
                // Not found.
                }
                if (!found) {
                    File file = new File(name);
                    if (!file.exists())
                        // $NON-NLS-1$
                        throw new IllegalArgumentException(this.bind("unit.missing", name));
                }
                CompilationUnit cu = new CompilationUnit(null, name, null, this.destinationPaths[i], shouldIgnoreOptionalProblems(this.ignoreOptionalProblemsFromFolders, name.toCharArray()), this.modNames[i]) {

                    @Override
                    public char[] getContents() {
                        try {
                            return javaFileObject.getCharContent(true).toString().toCharArray();
                        } catch (IOException e) {
                            e.printStackTrace();
                            throw new AbortCompilationUnit(null, e, null);
                        }
                    }
                };
                units.add(cu);
                this.javaFileObjectMap.put(cu, javaFileObject);
            }
            i++;
        }
    }
    CompilationUnit[] result = new CompilationUnit[units.size()];
    units.toArray(result);
    return result;
}
Also used : AbortCompilationUnit(org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit) CompilationUnit(org.eclipse.jdt.internal.compiler.batch.CompilationUnit) AbortCompilationUnit(org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) HashtableOfObject(org.eclipse.jdt.internal.compiler.util.HashtableOfObject) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) File(java.io.File)

Example 2 with HashtableOfObject

use of org.eclipse.jdt.internal.compiler.util.HashtableOfObject in project bazel-jdt-java-toolchain by salesforce.

the class ModuleDeclaration method resolvePackageDirectives.

/**
 * Resolve those module directives that relate to packages (exports, opens).
 */
public void resolvePackageDirectives(CompilationUnitScope cuScope) {
    if (this.binding == null) {
        this.ignoreFurtherInvestigation = true;
        return;
    }
    if (this.hasResolvedPackageDirectives)
        return;
    this.hasResolvedPackageDirectives = true;
    Set<PlainPackageBinding> exportedPkgs = new HashSet<>();
    for (int i = 0; i < this.exportsCount; i++) {
        ExportsStatement ref = this.exports[i];
        if (ref != null && ref.resolve(cuScope)) {
            if (!exportedPkgs.add(ref.resolvedPackage)) {
                cuScope.problemReporter().invalidPackageReference(IProblem.DuplicateExports, ref);
            }
            char[][] targets = null;
            if (ref.targets != null) {
                targets = new char[ref.targets.length][];
                for (int j = 0; j < targets.length; j++) targets[j] = ref.targets[j].moduleName;
            }
            this.binding.addResolvedExport(ref.resolvedPackage, targets);
        }
    }
    HashtableOfObject openedPkgs = new HashtableOfObject();
    for (int i = 0; i < this.opensCount; i++) {
        OpensStatement ref = this.opens[i];
        if (isOpen()) {
            cuScope.problemReporter().invalidOpensStatement(ref, this);
        } else {
            if (openedPkgs.containsKey(ref.pkgName)) {
                cuScope.problemReporter().invalidPackageReference(IProblem.DuplicateOpens, ref);
            } else {
                openedPkgs.put(ref.pkgName, ref);
                ref.resolve(cuScope);
            }
            char[][] targets = null;
            if (ref.targets != null) {
                targets = new char[ref.targets.length][];
                for (int j = 0; j < targets.length; j++) targets[j] = ref.targets[j].moduleName;
            }
            this.binding.addResolvedOpens(ref.resolvedPackage, targets);
        }
    }
}
Also used : PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) HashtableOfObject(org.eclipse.jdt.internal.compiler.util.HashtableOfObject) HashSet(java.util.HashSet)

Example 3 with HashtableOfObject

use of org.eclipse.jdt.internal.compiler.util.HashtableOfObject in project bazel-jdt-java-toolchain by salesforce.

the class PackageVisibilityStatement method resolve.

public boolean resolve(Scope scope) {
    boolean errorsExist = resolvePackageReference(scope) == null;
    if (this.isQualified()) {
        HashtableOfObject modules = new HashtableOfObject(this.targets.length);
        for (int i = 0; i < this.targets.length; i++) {
            ModuleReference ref = this.targets[i];
            // targets will be resolved later (during ModuleDeclaration.resolveModuleDirectives())
            if (modules.containsKey(ref.moduleName)) {
                scope.problemReporter().duplicateModuleReference(IProblem.DuplicateModuleRef, ref);
                errorsExist = true;
            } else {
                modules.put(ref.moduleName, ref);
            }
        }
    }
    return !errorsExist;
}
Also used : HashtableOfObject(org.eclipse.jdt.internal.compiler.util.HashtableOfObject)

Example 4 with HashtableOfObject

use of org.eclipse.jdt.internal.compiler.util.HashtableOfObject in project bazel-jdt-java-toolchain by salesforce.

the class ClassScope method buildComponents.

void buildComponents() {
    SourceTypeBinding sourceType = this.referenceContext.binding;
    if (!sourceType.isRecord())
        return;
    if (sourceType.areComponentsInitialized())
        return;
    if (this.referenceContext.recordComponents == null) {
        sourceType.setComponents(Binding.NO_COMPONENTS);
        return;
    }
    // count the number of fields vs. initializers
    RecordComponent[] recComps = this.referenceContext.recordComponents;
    int size = recComps.length;
    int count = size;
    // iterate the field declarations to create the bindings, lose all duplicates
    RecordComponentBinding[] componentBindings = new RecordComponentBinding[count];
    HashtableOfObject knownComponentNames = new HashtableOfObject(count);
    count = 0;
    for (int i = 0; i < size; i++) {
        RecordComponent recComp = recComps[i];
        RecordComponentBinding compBinding = new RecordComponentBinding(sourceType, recComp, null, recComp.modifiers | ExtraCompilerModifiers.AccUnresolved);
        compBinding.id = count;
        checkAndSetModifiersForComponents(compBinding, recComp);
        if (knownComponentNames.containsKey(recComp.name)) {
            RecordComponentBinding previousBinding = (RecordComponentBinding) knownComponentNames.get(recComp.name);
            if (previousBinding != null) {
                for (int f = 0; f < i; f++) {
                    RecordComponent previousComponent = recComps[f];
                    if (previousComponent.binding == previousBinding) {
                        // flag the duplicate component name error here.
                        problemReporter().recordDuplicateComponent(previousComponent);
                        break;
                    }
                }
            }
            // ensure that the duplicate field is found & removed
            knownComponentNames.put(recComp.name, null);
            problemReporter().recordDuplicateComponent(recComp);
            recComp.binding = null;
        } else {
            knownComponentNames.put(recComp.name, compBinding);
            // remember that we have seen a component with this name
            componentBindings[count++] = compBinding;
        }
    }
    // remove duplicate components
    if (count != componentBindings.length)
        System.arraycopy(componentBindings, 0, componentBindings = new RecordComponentBinding[count], 0, count);
    sourceType.setComponents(componentBindings);
    if (size > 0) {
        sourceType.isVarArgs = recComps[size - 1].isVarArgs();
    }
}
Also used : HashtableOfObject(org.eclipse.jdt.internal.compiler.util.HashtableOfObject) RecordComponent(org.eclipse.jdt.internal.compiler.ast.RecordComponent)

Example 5 with HashtableOfObject

use of org.eclipse.jdt.internal.compiler.util.HashtableOfObject in project bazel-jdt-java-toolchain by salesforce.

the class ClassScope method buildFields.

void buildFields() {
    SourceTypeBinding sourceType = this.referenceContext.binding;
    if (sourceType.areFieldsInitialized())
        return;
    if (this.referenceContext.fields == null) {
        sourceType.setFields(Binding.NO_FIELDS);
        return;
    }
    // count the number of fields vs. initializers
    FieldDeclaration[] fields = this.referenceContext.fields;
    int size = fields.length;
    int count = 0;
    for (int i = 0; i < size; i++) {
        switch(fields[i].getKind()) {
            case AbstractVariableDeclaration.FIELD:
            case AbstractVariableDeclaration.ENUM_CONSTANT:
                count++;
        }
    }
    // iterate the field declarations to create the bindings, lose all duplicates
    FieldBinding[] fieldBindings = new FieldBinding[count];
    HashtableOfObject knownFieldNames = new HashtableOfObject(count);
    count = 0;
    for (int i = 0; i < size; i++) {
        FieldDeclaration field = fields[i];
        if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
        // We used to report an error for initializers declared inside interfaces, but
        // now this error reporting is moved into the parser itself. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=212713
        } else {
            FieldBinding fieldBinding = new FieldBinding(field, null, field.modifiers | ExtraCompilerModifiers.AccUnresolved, sourceType);
            fieldBinding.id = count;
            // field's type will be resolved when needed for top level types
            checkAndSetModifiersForField(fieldBinding, field);
            if (knownFieldNames.containsKey(field.name)) {
                FieldBinding previousBinding = (FieldBinding) knownFieldNames.get(field.name);
                if (previousBinding != null) {
                    for (int f = 0; f < i; f++) {
                        FieldDeclaration previousField = fields[f];
                        if (previousField.binding == previousBinding) {
                            problemReporter().duplicateFieldInType(sourceType, previousField);
                            break;
                        }
                    }
                }
                // ensure that the duplicate field is found & removed
                knownFieldNames.put(field.name, null);
                problemReporter().duplicateFieldInType(sourceType, field);
                field.binding = null;
            } else {
                knownFieldNames.put(field.name, fieldBinding);
                // remember that we have seen a field with this name
                fieldBindings[count++] = fieldBinding;
            }
        }
    }
    // remove duplicate fields
    if (count != fieldBindings.length)
        System.arraycopy(fieldBindings, 0, fieldBindings = new FieldBinding[count], 0, count);
    // in case some static imports reached already into this type
    sourceType.tagBits &= ~(TagBits.AreFieldsSorted | TagBits.AreFieldsComplete);
    sourceType.setFields(fieldBindings);
}
Also used : HashtableOfObject(org.eclipse.jdt.internal.compiler.util.HashtableOfObject) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Aggregations

HashtableOfObject (org.eclipse.jdt.internal.compiler.util.HashtableOfObject)14 File (java.io.File)4 IOException (java.io.IOException)4 CompilationUnit (org.eclipse.jdt.internal.compiler.batch.CompilationUnit)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)2 List (java.util.List)1 JavaFileObject (javax.tools.JavaFileObject)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)1 RecordComponent (org.eclipse.jdt.internal.compiler.ast.RecordComponent)1 Classpath (org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath)1 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)1 PlainPackageBinding (org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding)1 AbortCompilationUnit (org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit)1 SimpleSet (org.eclipse.jdt.internal.compiler.util.SimpleSet)1