Search in sources :

Example 16 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project processing by processing.

the class CompletionGenerator method getMembersForType.

public static ArrayList<CompletionCandidate> getMembersForType(PreprocessedSketch ps, ClassMember tehClass, String childToLookFor, boolean noCompare, boolean staticOnly) {
    String child = childToLookFor.toLowerCase();
    ArrayList<CompletionCandidate> candidates = new ArrayList<>();
    log("getMemFoType-> Looking for match " + child + " inside " + tehClass + " noCompare " + noCompare + " staticOnly " + staticOnly);
    if (tehClass == null) {
        return candidates;
    }
    // tehClass will either be a TypeDecl defined locally
    if (tehClass.getDeclaringNode() instanceof TypeDeclaration) {
        TypeDeclaration td = (TypeDeclaration) tehClass.getDeclaringNode();
        {
            FieldDeclaration[] fields = td.getFields();
            for (FieldDeclaration field : fields) {
                if (staticOnly && !isStatic(field.modifiers())) {
                    continue;
                }
                List<VariableDeclarationFragment> vdfs = field.fragments();
                for (VariableDeclarationFragment vdf : vdfs) {
                    if (noCompare) {
                        candidates.add(new CompletionCandidate(vdf));
                    } else if (vdf.getName().toString().toLowerCase().startsWith(child))
                        candidates.add(new CompletionCandidate(vdf));
                }
            }
        }
        {
            MethodDeclaration[] methods = td.getMethods();
            for (MethodDeclaration method : methods) {
                if (staticOnly && !isStatic(method.modifiers())) {
                    continue;
                }
                if (noCompare) {
                    candidates.add(new CompletionCandidate(method));
                } else if (method.getName().toString().toLowerCase().startsWith(child))
                    candidates.add(new CompletionCandidate(method));
            }
        }
        ArrayList<CompletionCandidate> superClassCandidates;
        if (td.getSuperclassType() != null) {
            log(getNodeAsString(td.getSuperclassType()) + " <-Looking into superclass of " + tehClass);
            superClassCandidates = getMembersForType(ps, new ClassMember(ps, td.getSuperclassType()), childToLookFor, noCompare, staticOnly);
        } else {
            superClassCandidates = getMembersForType(ps, new ClassMember(Object.class), childToLookFor, noCompare, staticOnly);
        }
        for (CompletionCandidate cc : superClassCandidates) {
            candidates.add(cc);
        }
        return candidates;
    }
    // Or tehClass will be a predefined class
    Class<?> probableClass;
    if (tehClass.getClass_() != null) {
        probableClass = tehClass.getClass_();
    } else {
        probableClass = findClassIfExists(ps, tehClass.getTypeAsString());
        if (probableClass == null) {
            log("Couldn't find class " + tehClass.getTypeAsString());
            return candidates;
        }
        log("Loaded " + probableClass.toString());
    }
    for (Method method : probableClass.getMethods()) {
        if (!Modifier.isStatic(method.getModifiers()) && staticOnly) {
            continue;
        }
        StringBuilder label = new StringBuilder(method.getName() + "(");
        for (int i = 0; i < method.getParameterTypes().length; i++) {
            label.append(method.getParameterTypes()[i].getSimpleName());
            if (i < method.getParameterTypes().length - 1)
                label.append(",");
        }
        label.append(")");
        if (noCompare) {
            candidates.add(new CompletionCandidate(method));
        } else if (label.toString().toLowerCase().startsWith(child)) {
            candidates.add(new CompletionCandidate(method));
        }
    }
    for (Field field : probableClass.getFields()) {
        if (!Modifier.isStatic(field.getModifiers()) && staticOnly) {
            continue;
        }
        if (noCompare) {
            candidates.add(new CompletionCandidate(field));
        } else if (field.getName().toLowerCase().startsWith(child)) {
            candidates.add(new CompletionCandidate(field));
        }
    }
    if (probableClass.isArray() && !staticOnly) {
        // add array members manually, they can't be fetched through code
        String className = probableClass.getSimpleName();
        if (noCompare || "clone()".startsWith(child)) {
            String methodLabel = "<html>clone() : " + className + " - <font color=#777777>" + className + "</font></html>";
            candidates.add(new CompletionCandidate("clone()", methodLabel, "clone()", CompletionCandidate.PREDEF_METHOD));
        }
        if ("length".startsWith(child)) {
            String fieldLabel = "<html>length : int - <font color=#777777>" + className + "</font></html>";
            candidates.add(new CompletionCandidate("length", fieldLabel, "length", CompletionCandidate.PREDEF_FIELD));
        }
    }
    return candidates;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Field(java.lang.reflect.Field) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) List(java.util.List) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 17 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project bndtools by bndtools.

the class BaselineErrorHandler method generateRemovedMethodMarker.

List<MarkerData> generateRemovedMethodMarker(IJavaProject javaProject, final String className, final String methodName, final Delta requiresDelta) throws JavaModelException {
    final List<MarkerData> markers = new LinkedList<MarkerData>();
    final CompilationUnit ast = createAST(javaProject, className);
    if (ast != null) {
        ast.accept(new ASTVisitor() {

            @Override
            public boolean visit(TypeDeclaration typeDecl) {
                ITypeBinding typeBinding = typeDecl.resolveBinding();
                if (typeBinding != null) {
                    if (typeBinding.getBinaryName().equals(className)) {
                        Map<String, Object> attribs = new HashMap<String, Object>();
                        SimpleName nameNode = typeDecl.getName();
                        attribs.put(IMarker.CHAR_START, nameNode.getStartPosition());
                        attribs.put(IMarker.CHAR_END, nameNode.getStartPosition() + nameNode.getLength());
                        String message = String.format("The method '%s' was removed, which requires a %s change to the package.", methodName, requiresDelta);
                        attribs.put(IMarker.MESSAGE, message);
                        markers.add(new MarkerData(ast.getJavaElement().getResource(), attribs, false));
                        return false;
                    }
                }
                return true;
            }
        });
    }
    return markers;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MarkerData(org.bndtools.build.api.MarkerData) SimpleName(org.eclipse.jdt.core.dom.SimpleName) LinkedList(java.util.LinkedList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project bndtools by bndtools.

the class AbstractBuildErrorDetailsHandler method createTypeMarkerData.

/**
     * Create a marker on a Java Type
     *
     * @param javaProject
     * @param className
     *            - the fully qualified class name (e.g java.lang.String)
     * @param markerAttributes
     * @param hasResolutions
     *            - true if the marker will have resolutions
     * @return Marker Data that can be used to create an {@link IMarker}, or null if no location can be found
     * @throws JavaModelException
     */
public static final MarkerData createTypeMarkerData(IJavaProject javaProject, final String className, final Map<String, Object> markerAttributes, boolean hasResolutions) throws JavaModelException {
    final CompilationUnit ast = createAST(javaProject, className);
    if (ast == null)
        return null;
    ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(TypeDeclaration typeDecl) {
            ITypeBinding typeBinding = typeDecl.resolveBinding();
            if (typeBinding != null) {
                if (typeBinding.getBinaryName().equals(className)) {
                    SimpleName nameNode = typeDecl.getName();
                    markerAttributes.put(IMarker.CHAR_START, nameNode.getStartPosition());
                    markerAttributes.put(IMarker.CHAR_END, nameNode.getStartPosition() + nameNode.getLength());
                    return false;
                }
            }
            return true;
        }
    });
    if (!markerAttributes.containsKey(IMarker.CHAR_START))
        return null;
    return new MarkerData(ast.getJavaElement().getResource(), markerAttributes, hasResolutions);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 19 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.

the class PromoteTempToFieldRefactoring method checkInitialConditions.

/*
     * @see org.eclipse.jdt.internal.corext.refactoring.base.Refactoring#checkActivation(org.eclipse.core.runtime.IProgressMonitor)
     */
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
    RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext());
    if (result.hasFatalError())
        return result;
    initAST(pm);
    if (fTempDeclarationNode == null)
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_select_declaration);
    if (!Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class))
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_only_declared_in_methods);
    if (isMethodParameter())
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_method_parameters);
    if (isTempAnExceptionInCatchBlock())
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_exceptions);
    ASTNode declaringType = ASTResolving.findParentType(fTempDeclarationNode);
    if (declaringType instanceof TypeDeclaration && ((TypeDeclaration) declaringType).isInterface())
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_interface_methods);
    result.merge(checkTempTypeForLocalTypeUsage());
    if (result.hasFatalError())
        return result;
    checkTempInitializerForLocalTypeUsage();
    if (!fSelfInitializing)
        initializeDefaults();
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 20 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.

the class PromoteTempToFieldRefactoring method getAllConstructors.

private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) {
    if (typeDeclaration instanceof TypeDeclaration) {
        MethodDeclaration[] allMethods = ((TypeDeclaration) typeDeclaration).getMethods();
        List<MethodDeclaration> result = new ArrayList<MethodDeclaration>(Math.min(allMethods.length, 1));
        for (int i = 0; i < allMethods.length; i++) {
            MethodDeclaration declaration = allMethods[i];
            if (declaration.isConstructor())
                result.add(declaration);
        }
        return result.toArray(new MethodDeclaration[result.size()]);
    }
    return new MethodDeclaration[] {};
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)37 ASTNode (org.eclipse.jdt.core.dom.ASTNode)24 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)17 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 Type (org.eclipse.jdt.core.dom.Type)11 ArrayList (java.util.ArrayList)8 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8 AST (org.eclipse.jdt.core.dom.AST)7 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 SimpleType (org.eclipse.jdt.core.dom.SimpleType)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 List (java.util.List)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)5 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)4