Example usage for org.eclipse.jdt.core.dom Name getFullyQualifiedName

List of usage examples for org.eclipse.jdt.core.dom Name getFullyQualifiedName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Name getFullyQualifiedName.

Prototype

public final String getFullyQualifiedName() 

Source Link

Document

Returns the standard dot-separated representation of this name.

Usage

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

private void handleLink(List<? extends ASTNode> fragments) {
    //TODO: Javadoc shortens type names to minimal length according to context
    int fs = fragments.size();
    if (fs > 0) {
        Object first = fragments.get(0);
        String refTypeName = null;
        String refMemberName = null;
        String[] refMethodParamTypes = null;
        String[] refMethodParamNames = null;
        if (first instanceof Name) {
            Name name = (Name) first;
            refTypeName = name.getFullyQualifiedName();
        } else if (first instanceof MemberRef) {
            MemberRef memberRef = (MemberRef) first;
            Name qualifier = memberRef.getQualifier();
            refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName(); //$NON-NLS-1$
            refMemberName = memberRef.getName().getIdentifier();
        } else if (first instanceof MethodRef) {
            MethodRef methodRef = (MethodRef) first;
            Name qualifier = methodRef.getQualifier();
            refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName(); //$NON-NLS-1$
            refMemberName = methodRef.getName().getIdentifier();
            List<MethodRefParameter> params = methodRef.parameters();
            int ps = params.size();
            refMethodParamTypes = new String[ps];
            refMethodParamNames = new String[ps];
            for (int i = 0; i < ps; i++) {
                MethodRefParameter param = params.get(i);
                refMethodParamTypes[i] = ASTNodes.asString(param.getType());
                SimpleName paramName = param.getName();
                if (paramName != null)
                    refMethodParamNames[i] = paramName.getIdentifier();
            }//from  w  ww. j  a va 2 s.c  o m
        }

        if (refTypeName != null) {
            fBuf.append("<a href='"); //$NON-NLS-1$
            try {
                String scheme = JavaElementLinks.JAVADOC_SCHEME;
                String uri = JavaElementLinks.createURI(scheme, fElement, refTypeName, refMemberName,
                        refMethodParamTypes);
                fBuf.append(uri);
            } catch (URISyntaxException e) {
                //TODO
                e.printStackTrace();
            }
            fBuf.append("'>"); //$NON-NLS-1$
            if (fs > 1 && !(fs == 2 && isWhitespaceTextElement(fragments.get(1)))) {
                handleContentElements(fragments.subList(1, fs), true);
            } else {
                fBuf.append(refTypeName);
                if (refMemberName != null) {
                    if (refTypeName.length() > 0) {
                        fBuf.append('.');
                    }
                    fBuf.append(refMemberName);
                    if (refMethodParamTypes != null) {
                        fBuf.append('(');
                        for (int i = 0; i < refMethodParamTypes.length; i++) {
                            String pType = refMethodParamTypes[i];
                            fBuf.append(pType);
                            String pName = refMethodParamNames[i];
                            if (pName != null) {
                                fBuf.append(' ').append(pName);
                            }
                            if (i < refMethodParamTypes.length - 1) {
                                fBuf.append(", "); //$NON-NLS-1$
                            }
                        }
                        fBuf.append(')');
                    }
                }
            }
            fBuf.append("</a>"); //$NON-NLS-1$
        } else {
            handleContentElements(fragments);
        }
    }
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

/**
 * /*from   ww w.ja  va2s.  c o m*/
 * @param name
 * @return The root FQN of a name.
 */
public static String getFQNFromAnyName(Name name) {
    String fqn = null;
    ASTNode node = name;

    do {
        Name tempName = (Name) node;
        fqn = tempName.getFullyQualifiedName();
        node = tempName.getParent();
    } while (node != null && node instanceof Name);

    return fqn;
}

From source file:ca.mcgill.cs.swevo.ppa.PPANameVisitor.java

License:Open Source License

public static String getKey(Name node) {
    return node.getFullyQualifiedName() + "" + node.getStartPosition() + "" + node.getLength();
}

From source file:ca.mcgill.cs.swevo.ppa.ui.actions.PPAOnNameAction.java

License:Open Source License

public void run(IAction action) {
    if (javaEditor != null) {
        ICompilationUnit icu = (ICompilationUnit) SelectionConverter.getInput(javaEditor);
        IDocument document = JavaUI.getDocumentProvider().getDocument(javaEditor.getEditorInput());
        JavaTextSelection javaSelection = new JavaTextSelection(icu, document, selection.getOffset(),
                selection.getLength());//from w w  w  .j  a v a2  s  .c  o m
        ASTNode[] nodes = javaSelection.resolveSelectedNodes();

        if (nodes != null && nodes.length == 1) {
            ASTNode node = nodes[0];
            if (node instanceof Name) {

                // 1- Get the PPA CU:
                CompilationUnit cu = PPAUtil.getCU((IFile) icu.getResource(), new PPAOptions());
                if (cu == null) {
                    return;
                }
                PPANameVisitor nameVisitor = new PPANameVisitor();
                cu.accept(nameVisitor);

                // 2- Get the corresponding name in the PPA CU
                Name ppaNode = nameVisitor.getName((Name) node);
                if (ppaNode != null) {
                    String bindingText = PPABindingsUtil.getBindingText(ppaNode.resolveBinding());
                    MessageDialog.openInformation(javaEditor.getEditorSite().getShell(),
                            "Binding for " + ppaNode.getFullyQualifiedName(), bindingText);
                }

                // 3- Clean-up
                PPAUtil.cleanUp(cu);
            }
        }
    }
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

private Annotation findExistingSuppressWarningsAnnotation(final List<IExtendedModifier> modifiers) {
    for (final IExtendedModifier modifier : modifiers) {
        if (modifier.isAnnotation()) {
            final Annotation annotation = (Annotation) modifier;
            final Name typeName = annotation.getTypeName();
            if (typeName.isSimpleName() && "SuppressWarnings".equals(typeName.getFullyQualifiedName())
                    || typeName.isQualifiedName()
                            && "java.lang.SuppressWarnings".equals(typeName.getFullyQualifiedName())) {
                return annotation;
            }/*from  ww w . j  av  a 2 s.  c om*/
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavadocContentAccess2.java

License:Open Source License

private void handleLink(List<? extends ASTNode> fragments) {
    //TODO: Javadoc shortens type names to minimal length according to context
    int fs = fragments.size();
    if (fs > 0) {
        Object first = fragments.get(0);
        String refTypeName = null;
        String refMemberName = null;
        String[] refMethodParamTypes = null;
        String[] refMethodParamNames = null;
        if (first instanceof Name) {
            Name name = (Name) first;
            refTypeName = name.getFullyQualifiedName();
        } else if (first instanceof MemberRef) {
            MemberRef memberRef = (MemberRef) first;
            Name qualifier = memberRef.getQualifier();
            refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName(); //$NON-NLS-1$
            refMemberName = memberRef.getName().getIdentifier();
        } else if (first instanceof MethodRef) {
            MethodRef methodRef = (MethodRef) first;
            Name qualifier = methodRef.getQualifier();
            refTypeName = qualifier == null ? "" : qualifier.getFullyQualifiedName(); //$NON-NLS-1$
            refMemberName = methodRef.getName().getIdentifier();
            List<MethodRefParameter> params = methodRef.parameters();
            int ps = params.size();
            refMethodParamTypes = new String[ps];
            refMethodParamNames = new String[ps];
            for (int i = 0; i < ps; i++) {
                MethodRefParameter param = params.get(i);
                refMethodParamTypes[i] = ASTNodes.asString(param.getType());
                SimpleName paramName = param.getName();
                if (paramName != null)
                    refMethodParamNames[i] = paramName.getIdentifier();
            }/*from  w w w .j a v  a  2s  . com*/
        }

        if (refTypeName != null) {
            fBuf.append("<a href='"); //$NON-NLS-1$
            try {
                String scheme = urlPrefix;
                String uri = JavaElementLinks.createURI(scheme, fElement, refTypeName, refMemberName,
                        refMethodParamTypes);
                fBuf.append(uri);
            } catch (URISyntaxException e) {
                LOG.error(e.getMessage(), e);
            }
            fBuf.append("'>"); //$NON-NLS-1$
            if (fs > 1 && !(fs == 2 && isWhitespaceTextElement(fragments.get(1)))) {
                handleContentElements(fragments.subList(1, fs), true);
            } else {
                fBuf.append(refTypeName);
                if (refMemberName != null) {
                    if (refTypeName.length() > 0) {
                        fBuf.append('.');
                    }
                    fBuf.append(refMemberName);
                    if (refMethodParamTypes != null) {
                        fBuf.append('(');
                        for (int i = 0; i < refMethodParamTypes.length; i++) {
                            String pType = refMethodParamTypes[i];
                            fBuf.append(pType);
                            String pName = refMethodParamNames[i];
                            if (pName != null) {
                                fBuf.append(' ').append(pName);
                            }
                            if (i < refMethodParamTypes.length - 1) {
                                fBuf.append(", "); //$NON-NLS-1$
                            }
                        }
                        fBuf.append(')');
                    }
                }
            }
            fBuf.append("</a>"); //$NON-NLS-1$
        } else {
            handleContentElements(fragments);
        }
    }
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.reverse.ReversePackage_JavaTypes.java

License:Open Source License

/**
 * @throws CodeSyncException //  www .  ja v  a 2  s.  c  o  m
 * @flowerModelElementId _IFwlserfEd6QaI9daHlzjA
 */
@SuppressWarnings("unchecked")
@Override
protected ReverseStatus reverseElement(Type modelElement, FileAndAST<CompilationUnit> astElement)
        throws CodeSyncException {
    ReverseStatus status = new ReverseStatus();
    TypeDeclaration typeDeclaration = JavaSyncUtils.getMasterClass(astElement.getAstElement());

    getFullyQualifiedImports().clear();
    for (ImportDeclaration id : (List<ImportDeclaration>) astElement.getAstElement().imports()) {
        Name importedName = id.getName();
        SimpleName name;
        if (importedName.isQualifiedName()) {
            name = ((QualifiedName) importedName).getName();
        } else {
            name = (SimpleName) importedName;
        }
        getFullyQualifiedImports().put(name.toString(), importedName.getFullyQualifiedName());
    }

    if (typeDeclaration.isInterface()) {
        reverseJavaInterface.currentElement = modelElement;
        status.applyOr(reverseJavaInterface.reverse((Interface) modelElement, astElement.getAstElement()));
        reverseJavaInterface.currentElement = null;
    } else {
        reverseJavaClass.currentElement = modelElement;
        status.applyOr(reverseJavaClass.reverse((org.eclipse.uml2.uml.Class) modelElement,
                astElement.getAstElement()));
        reverseJavaClass.currentElement = null;
    }

    updateSynchronizationTimeStamp(modelElement, astElement);
    return status;
}

From source file:com.facebook.buck.jvm.java.JavaFileParser.java

License:Apache License

public JavaFileFeatures extractFeaturesFromJavaCode(String code) {
    // For now, we will harcode this. Ultimately, we probably want to make this configurable via
    // .buckconfig. For example, the Buck project itself is diligent about disallowing wildcard
    // imports, but the one exception is the Java code generated via Thrift in src-gen.
    boolean shouldThrowForUnsupportedWildcardImport = false;

    AtomicBoolean isPoisonedByUnsupportedWildcardImport = new AtomicBoolean(false);

    CompilationUnit compilationUnit = makeCompilationUnitFromSource(code);

    ImmutableSortedSet.Builder<String> providedSymbols = ImmutableSortedSet.naturalOrder();
    ImmutableSortedSet.Builder<String> requiredSymbols = ImmutableSortedSet.naturalOrder();
    ImmutableSortedSet.Builder<String> exportedSymbols = ImmutableSortedSet.naturalOrder();
    ImmutableSortedSet.Builder<String> requiredSymbolsFromExplicitImports = ImmutableSortedSet.naturalOrder();

    compilationUnit.accept(new ASTVisitor() {

        @Nullable//w  ww  .jav a 2s  .  com
        private String packageName;

        /** Maps simple name to fully-qualified name. */
        private Map<String, String> simpleImportedTypes = new HashMap<>();

        /**
         * Maps wildcard import prefixes, such as {@code "java.util"} to the types in the
         * respective package if a wildcard import such as {@code import java.util.*} is used.
         */
        private Map<String, ImmutableSet<String>> wildcardImports = new HashMap<>();

        @Override
        public boolean visit(PackageDeclaration node) {
            Preconditions.checkState(packageName == null, "There should be at most one package declaration");
            packageName = node.getName().getFullyQualifiedName();
            return false;
        }

        // providedSymbols

        @Override
        public boolean visit(TypeDeclaration node) {
            // Local classes can be declared inside of methods. Skip over these.
            if (node.getParent() instanceof TypeDeclarationStatement) {
                return true;
            }

            String fullyQualifiedName = getFullyQualifiedTypeName(node);
            if (fullyQualifiedName != null) {
                providedSymbols.add(fullyQualifiedName);
            }

            @SuppressWarnings("unchecked")
            List<Type> interfaceTypes = node.superInterfaceTypes();
            for (Type interfaceType : interfaceTypes) {
                tryAddType(interfaceType, DependencyType.EXPORTED);
            }

            Type superclassType = node.getSuperclassType();
            if (superclassType != null) {
                tryAddType(superclassType, DependencyType.EXPORTED);
            }

            return true;
        }

        @Override
        public boolean visit(EnumDeclaration node) {
            String fullyQualifiedName = getFullyQualifiedTypeName(node);
            if (fullyQualifiedName != null) {
                providedSymbols.add(fullyQualifiedName);
            }
            return true;
        }

        @Override
        public boolean visit(AnnotationTypeDeclaration node) {
            String fullyQualifiedName = getFullyQualifiedTypeName(node);
            if (fullyQualifiedName != null) {
                providedSymbols.add(fullyQualifiedName);
            }
            return true;
        }

        // requiredSymbols

        /**
         * Uses heuristics to try to figure out what type of QualifiedName this is. Returns a
         * non-null value if this is believed to be a reference that qualifies as a "required
         * symbol" relationship.
         */
        @Override
        public boolean visit(QualifiedName node) {
            QualifiedName ancestor = findMostQualifiedAncestor(node);
            ASTNode parent = ancestor.getParent();
            if (!(parent instanceof PackageDeclaration) && !(parent instanceof ImportDeclaration)) {
                String symbol = ancestor.getFullyQualifiedName();

                // If it does not start with an uppercase letter, it is probably because it is a
                // property lookup.
                if (CharMatcher.javaUpperCase().matches(symbol.charAt(0))) {
                    addTypeFromDotDelimitedSequence(symbol, DependencyType.REQUIRED);
                }
            }

            return false;
        }

        /**
         * @param expr could be "Example", "Example.field", "com.example.Example". Note it could
         *     also be a built-in type, such as "java.lang.Integer", in which case it will not be
         *     added to the set of required symbols.
         */
        private void addTypeFromDotDelimitedSequence(String expr, DependencyType dependencyType) {
            // At this point, symbol could be `System.out`. We want to reduce it to `System` and
            // then check it against JAVA_LANG_TYPES.
            if (startsWithUppercaseChar(expr)) {
                int index = expr.indexOf('.');
                if (index >= 0) {
                    String leftmostComponent = expr.substring(0, index);
                    if (JAVA_LANG_TYPES.contains(leftmostComponent)) {
                        return;
                    }
                }
            }

            expr = qualifyWithPackageNameIfNecessary(expr);
            addSymbol(expr, dependencyType);
        }

        @Override
        public boolean visit(ImportDeclaration node) {
            String fullyQualifiedName = node.getName().getFullyQualifiedName();

            // Apparently, "on demand" means "uses a wildcard," such as "import java.util.*".
            // Although we can choose to prohibit these in our own code, it is much harder to
            // enforce for third-party code. As such, we will tolerate these for some of the common
            // cases.
            if (node.isOnDemand()) {
                ImmutableSet<String> value = SUPPORTED_WILDCARD_IMPORTS.get(fullyQualifiedName);
                if (value != null) {
                    wildcardImports.put(fullyQualifiedName, value);
                    return false;
                } else if (shouldThrowForUnsupportedWildcardImport) {
                    throw new RuntimeException(String.format(
                            "Use of wildcard 'import %s.*' makes it impossible to statically determine "
                                    + "required symbols in this file. Please enumerate explicit imports.",
                            fullyQualifiedName));
                } else {
                    isPoisonedByUnsupportedWildcardImport.set(true);
                    return false;
                }
            }

            // Only worry about the dependency on the enclosing type.
            Optional<String> simpleName = getSimpleNameFromFullyQualifiedName(fullyQualifiedName);
            if (simpleName.isPresent()) {
                String name = simpleName.get();
                int index = fullyQualifiedName.indexOf("." + name);
                String enclosingType = fullyQualifiedName.substring(0, index + name.length() + 1);
                requiredSymbolsFromExplicitImports.add(enclosingType);

                simpleImportedTypes.put(name, enclosingType);
            } else {
                LOG.warn("Suspicious import lacks obvious enclosing type: %s", fullyQualifiedName);
                // The one example we have seen of this in the wild is
                // "org.whispersystems.curve25519.java.curve_sigs". In practice, we still need to add
                // it as a required symbol in this case.
                requiredSymbols.add(fullyQualifiedName);
            }
            return false;
        }

        @Override
        public boolean visit(MethodInvocation node) {
            if (node.getExpression() == null) {
                return true;
            }

            String receiver = node.getExpression().toString();
            if (looksLikeAType(receiver)) {
                addTypeFromDotDelimitedSequence(receiver, DependencyType.REQUIRED);
            }
            return true;
        }

        /** An annotation on a member with zero arguments. */
        @Override
        public boolean visit(MarkerAnnotation node) {
            DependencyType dependencyType = findDependencyTypeForAnnotation(node);
            addSimpleTypeName(node.getTypeName(), dependencyType);
            return true;
        }

        /** An annotation on a member with named arguments. */
        @Override
        public boolean visit(NormalAnnotation node) {
            DependencyType dependencyType = findDependencyTypeForAnnotation(node);
            addSimpleTypeName(node.getTypeName(), dependencyType);
            return true;
        }

        /** An annotation on a member with a single, unnamed argument. */
        @Override
        public boolean visit(SingleMemberAnnotation node) {
            DependencyType dependencyType = findDependencyTypeForAnnotation(node);
            addSimpleTypeName(node.getTypeName(), dependencyType);
            return true;
        }

        private DependencyType findDependencyTypeForAnnotation(Annotation annotation) {
            ASTNode parentNode = annotation.getParent();
            if (parentNode == null) {
                return DependencyType.REQUIRED;
            }

            if (parentNode instanceof BodyDeclaration) {
                // Note that BodyDeclaration is an abstract class. Its subclasses are things like
                // FieldDeclaration and MethodDeclaration. We want to be sure that an annotation on
                // any non-private declaration is considered an exported symbol.
                BodyDeclaration declaration = (BodyDeclaration) parentNode;

                int modifiers = declaration.getModifiers();
                if ((modifiers & Modifier.PRIVATE) == 0) {
                    return DependencyType.EXPORTED;
                }
            }
            return DependencyType.REQUIRED;
        }

        @Override
        public boolean visit(SimpleType node) {
            // This method is responsible for finding the overwhelming majority of the required
            // symbols in the AST.
            tryAddType(node, DependencyType.REQUIRED);
            return true;
        }

        // exportedSymbols

        @Override
        public boolean visit(MethodDeclaration node) {
            // Types from private method signatures need not be exported.
            if ((node.getModifiers() & Modifier.PRIVATE) != 0) {
                return true;
            }

            Type returnType = node.getReturnType2();
            if (returnType != null) {
                tryAddType(returnType, DependencyType.EXPORTED);
            }

            @SuppressWarnings("unchecked")
            List<SingleVariableDeclaration> params = node.parameters();
            for (SingleVariableDeclaration decl : params) {
                tryAddType(decl.getType(), DependencyType.EXPORTED);
            }

            @SuppressWarnings("unchecked")
            List<Type> exceptions = node.thrownExceptionTypes();
            for (Type exception : exceptions) {
                tryAddType(exception, DependencyType.EXPORTED);
            }

            return true;
        }

        @Override
        public boolean visit(FieldDeclaration node) {
            // Types from private fields need not be exported.
            if ((node.getModifiers() & Modifier.PRIVATE) == 0) {
                tryAddType(node.getType(), DependencyType.EXPORTED);
            }
            return true;
        }

        private void tryAddType(Type type, DependencyType dependencyType) {
            if (type.isSimpleType()) {
                SimpleType simpleType = (SimpleType) type;
                Name simpleTypeName = simpleType.getName();
                String simpleName = simpleTypeName.toString();

                // For a Type such as IExample<T>, both "IExample" and "T" will be submitted here as
                // simple types. As such, we use this imperfect heuristic to filter out "T" from being
                // added. Note that this will erroneously exclude "URI". In practice, this should
                // generally be OK. For example, assuming "URI" is also imported, then at least it
                // will end up in the set of required symbols. To this end, we perform a second check
                // for "all caps" types to see if there is a corresponding import and if it should be
                // exported rather than simply required.
                if (!CharMatcher.javaUpperCase().matchesAllOf(simpleName)
                        || (dependencyType == DependencyType.EXPORTED
                                && simpleImportedTypes.containsKey(simpleName))) {
                    addSimpleTypeName(simpleTypeName, dependencyType);
                }
            } else if (type.isArrayType()) {
                ArrayType arrayType = (ArrayType) type;
                tryAddType(arrayType.getElementType(), dependencyType);
            } else if (type.isParameterizedType()) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                tryAddType(parameterizedType.getType(), dependencyType);
                @SuppressWarnings("unchecked")
                List<Type> argTypes = parameterizedType.typeArguments();
                for (Type argType : argTypes) {
                    tryAddType(argType, dependencyType);
                }
            }
        }

        private void addSimpleTypeName(Name simpleTypeName, DependencyType dependencyType) {
            String simpleName = simpleTypeName.toString();
            if (JAVA_LANG_TYPES.contains(simpleName)) {
                return;
            }

            String fullyQualifiedNameForSimpleName = simpleImportedTypes.get(simpleName);
            if (fullyQualifiedNameForSimpleName != null) {
                // May need to promote from required to exported in this case.
                if (dependencyType == DependencyType.EXPORTED) {
                    addSymbol(fullyQualifiedNameForSimpleName, DependencyType.EXPORTED);
                }
                return;
            }

            // For well-behaved source code, this will always be empty, so don't even bother to
            // create the iterator most of the time.
            if (!wildcardImports.isEmpty()) {
                for (Map.Entry<String, ImmutableSet<String>> entry : wildcardImports.entrySet()) {
                    Set<String> types = entry.getValue();
                    if (types.contains(simpleName)) {
                        String packageName = entry.getKey();
                        addSymbol(packageName + "." + simpleName, dependencyType);
                        return;
                    }
                }
            }

            String symbol = simpleTypeName.getFullyQualifiedName();
            symbol = qualifyWithPackageNameIfNecessary(symbol);
            addSymbol(symbol, dependencyType);
        }

        private void addSymbol(String symbol, DependencyType dependencyType) {
            ((dependencyType == DependencyType.REQUIRED) ? requiredSymbols : exportedSymbols).add(symbol);
        }

        private String qualifyWithPackageNameIfNecessary(String symbol) {
            if (!startsWithUppercaseChar(symbol)) {
                return symbol;
            }

            // If the symbol starts with a capital letter, then we assume that it is a reference to
            // a type in the same package.
            int index = symbol.indexOf('.');
            if (index >= 0) {
                symbol = symbol.substring(0, index);
            }
            if (packageName != null) {
                symbol = packageName + "." + symbol;
            }

            return symbol;
        }
    });

    // TODO(mbolin): Special treatment for exportedSymbols when poisoned by wildcard import.
    ImmutableSortedSet<String> totalExportedSymbols = exportedSymbols.build();

    // If we were poisoned by an unsupported wildcard import, then we should rely exclusively on
    // the explicit imports to determine the required symbols.
    Set<String> totalRequiredSymbols = new HashSet<>();
    if (isPoisonedByUnsupportedWildcardImport.get()) {
        totalRequiredSymbols.addAll(requiredSymbolsFromExplicitImports.build());
    } else {
        totalRequiredSymbols.addAll(requiredSymbolsFromExplicitImports.build());
        totalRequiredSymbols.addAll(requiredSymbols.build());
    }
    // Make sure that required and exported symbols are disjoint sets.
    totalRequiredSymbols.removeAll(totalExportedSymbols);

    return new JavaFileFeatures(providedSymbols.build(), ImmutableSortedSet.copyOf(totalRequiredSymbols),
            totalExportedSymbols);
}

From source file:com.google.currysrc.processors.ModifyQualifiedNames.java

License:Apache License

@Override
public void process(Context context, CompilationUnit cu) {
    final ASTRewrite rewrite = context.rewrite();
    ASTVisitor visitor = new ASTVisitor(true /* visitDocTags */) {
        @Override/*  w w w.j a v  a 2s. c  om*/
        public boolean visit(QualifiedName node) {
            Name qualifier = node.getQualifier();
            if (qualifier != null) {
                String fullyQualifiedName = qualifier.getFullyQualifiedName();
                if (fullyQualifiedName.startsWith(oldPrefix)) {
                    String newQualifierString = newPrefix + fullyQualifiedName.substring(oldPrefix.length());
                    Name newQualifier = node.getAST().newName(newQualifierString);
                    rewrite.replace(qualifier, newQualifier, null /* editGroup */);
                }
            }
            return false;
        }
    };
    cu.accept(visitor);
}

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance,
        Map<String, LinkedCorrectionProposal> proposals) throws CoreException {
    Name node = refNode;
    //      do {//from   w ww  .j  av  a  2 s  . c o  m
    String typeName = ASTNodes.getSimpleNameIdentifier(node);
    Name qualifier = null;
    // only propose to create types for qualifiers when the name starts with upper case
    boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
    if (isPossibleName) {
        IPackageFragment enclosingPackage = null;
        IType enclosingType = null;
        if (node.isSimpleName()) {
            enclosingPackage = (IPackageFragment) cu.getParent();
            return;
            // don't suggest member type, user can select it in wizard
        } else {
            Name qualifierName = ((QualifiedName) node).getQualifier();
            IBinding binding = qualifierName.resolveBinding();
            if (binding != null && binding.isRecovered()) {
                binding = null;
            }
            if (binding instanceof ITypeBinding) {
                enclosingType = (IType) binding.getJavaElement();
            } else if (binding instanceof IPackageBinding) {
                qualifier = qualifierName;
                enclosingPackage = (IPackageFragment) binding.getJavaElement();
            } else {
                IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                if (res != null && res.length > 0 && res[0] instanceof IType) {
                    enclosingType = (IType) res[0];
                } else {
                    qualifier = qualifierName;
                    enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu)
                            .getPackageFragment(ASTResolving.getFullName(qualifierName));
                }
            }
        }
        int rel = relevance;
        if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
            rel += 3;
        }

        if (enclosingPackage != null
                && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists()
                || enclosingType != null && !enclosingType.isReadOnly()
                        && !enclosingType.getType(typeName).exists()) { // new member type
            IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;

            String name = node.getFullyQualifiedName();

            if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_CLASS, enclosing, rel + 3));
            } else if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_INTERFACE, enclosing, rel + 2));
            } else if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_ENUM, enclosing, rel));
            }
        }
    }
    node = qualifier;
    //      } while (node != null);
}