Search in sources :

Example 11 with ConstructorInvocation

use of org.eclipse.jdt.core.dom.ConstructorInvocation in project eap-additional-testsuite by jboss-set.

the class MethodInfo method blockIterate.

private static void blockIterate(final Block block, final CompilationUnit cu, HashMap<String, String> blockDeclarations) {
    List<Statement> statements = new ArrayList<>();
    HashMap<String, String> bDeclarations = new HashMap();
    bDeclarations.putAll(blockDeclarations);
    if (block != null) {
        statements = block.statements();
    }
    for (Statement s : statements) {
        s.accept(new ASTVisitor() {

            public boolean visit(SingleVariableDeclaration node) {
                String name = node.getName().toString();
                String type = node.getType().toString();
                bDeclarations.put(name, type);
                ArrayList<String> types0 = new ArrayList<>();
                String type2 = null;
                do {
                    if (type.contains("[")) {
                        type = type.replaceAll("\\[\\]", "");
                        if (type.contains("[")) {
                            type = type.substring(0, type.indexOf("["));
                        }
                    }
                    if (type.contains("<")) {
                        String type3 = type;
                        type = type.substring(0, type.indexOf("<"));
                        if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
                            type2 = null;
                        } else {
                            type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
                            if (type2.contains(",")) {
                                if (type2.substring(0, type2.indexOf(",")).contains("<")) {
                                    types0.add(type2);
                                } else {
                                    types0.add(type2.substring(0, type2.indexOf(",")));
                                    types0.add(type2.substring(type2.indexOf(",") + 1));
                                }
                            } else {
                                types0.add(type2);
                            }
                        }
                    }
                    types.addAll(Arrays.asList(type.split(" extends ")));
                    if (types0.size() != 0) {
                        type = types0.remove(0);
                    } else {
                        type = null;
                    }
                } while (type != null);
                return true;
            }

            public boolean visit(VariableDeclarationStatement node) {
                String name = node.fragments().get(0).toString().split("=")[0].trim();
                String type = node.getType().toString();
                bDeclarations.put(name, type);
                ArrayList<String> types0 = new ArrayList<>();
                String type2 = null;
                do {
                    if (type.contains("[")) {
                        type = type.replaceAll("\\[\\]", "");
                        if (type.contains("[")) {
                            type = type.substring(0, type.indexOf("["));
                        }
                    }
                    if (type.contains("<")) {
                        String type3 = type;
                        type = type.substring(0, type.indexOf("<"));
                        if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
                            type2 = null;
                        } else {
                            type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
                            if (type2.contains(",")) {
                                if (type2.substring(0, type2.indexOf(",")).contains("<")) {
                                    types0.add(type2);
                                } else {
                                    types0.add(type2.substring(0, type2.indexOf(",")));
                                    types0.add(type2.substring(type2.indexOf(",") + 1));
                                }
                            } else {
                                types0.add(type2);
                            }
                        }
                    }
                    types.addAll(Arrays.asList(type.split(" extends ")));
                    if (types0.size() != 0) {
                        type = types0.remove(0);
                    } else {
                        type = null;
                    }
                } while (type != null);
                return true;
            }

            public boolean visit(SimpleType node) {
                String type = node.getName().toString();
                String name = node.getName().toString();
                bDeclarations.put(name, type);
                ArrayList<String> types0 = new ArrayList<>();
                String type2 = null;
                do {
                    if (type.contains("[")) {
                        type = type.replaceAll("\\[\\]", "");
                        if (type.contains("[")) {
                            type = type.substring(0, type.indexOf("["));
                        }
                    }
                    if (type.contains("<")) {
                        String type3 = type;
                        type = type.substring(0, type.indexOf("<"));
                        if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
                            type2 = null;
                        } else {
                            type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
                            if (type2.contains(",")) {
                                if (type2.substring(0, type2.indexOf(",")).contains("<")) {
                                    types0.add(type2);
                                } else {
                                    types0.add(type2.substring(0, type2.indexOf(",")));
                                    types0.add(type2.substring(type2.indexOf(",") + 1));
                                }
                            } else {
                                types0.add(type2);
                            }
                        }
                    }
                    types.addAll(Arrays.asList(type.split(" extends ")));
                    if (types0.size() != 0) {
                        type = types0.remove(0);
                    } else {
                        type = null;
                    }
                } while (type != null);
                return true;
            }

            public boolean visit(ClassInstanceCreation node) {
                ClassInfo clInfo = new ClassInfo();
                clInfo.className = node.getType().toString();
                List params = node.arguments();
                for (Object s : params) {
                    boolean resolved = true;
                    String arg = ((Expression) s).toString();
                    if (arg.contains("[") && !arg.contains("\"")) {
                        arg = arg.substring(0, arg.indexOf("["));
                    }
                    if (arg.startsWith("\"") && arg.endsWith("\"")) {
                        arg = "String";
                    } else if (arg.startsWith("\'") && arg.endsWith("\'")) {
                        arg = "Character";
                    } else if (arg.contains("+") && (arg.contains("\"") || arg.contains("\'"))) {
                        arg = "String";
                    } else if (arg.contains("instanceof")) {
                        arg = arg.substring(arg.indexOf("instanceof") + 11);
                    } else if (bDeclarations.containsKey(arg)) {
                        arg = bDeclarations.get(arg);
                    } else if (arg.equals("true") || arg.equals("false")) {
                        arg = "Boolean";
                    } else if (arg.contains("==") || arg.contains(">") || arg.contains("<") || arg.contains("!=") || arg.contains(">=") || arg.contains("<=")) {
                        arg = "Boolean";
                    } else if (arg.contains("TimeUnit.SECONDS")) {
                        arg = "Numeric";
                    } else if (arg.contains(".class") && !arg.contains(".className")) {
                        arg = arg.replaceAll(".class", "");
                    } else if (arg.startsWith("new ")) {
                        arg = arg.replaceAll("new ", "");
                        if (arg.contains("(")) {
                            arg = arg.substring(0, arg.indexOf("("));
                        } else if (arg.contains("[")) {
                            arg = arg.substring(0, arg.indexOf("["));
                        }
                    } else if (NumberUtils.isNumber(arg)) {
                        arg = "Numeric";
                    } else if ((arg.contains("-") || arg.contains("+") || arg.contains("*")) && !arg.contains("\"") && !arg.contains("NAME")) {
                        arg = "Numeric";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("String")) {
                        arg = "String";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".is")) {
                        arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".contains")) {
                        arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Int")) {
                        arg = "Integer";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Double")) {
                        arg = "Double";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Boolean")) {
                        arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".equals")) {
                        arg = "Boolean";
                    } else {
                        resolved = false;
                        if (!arg.contains(".") && !arg.contains("(") && !arg.contains(")")) {
                            typesNotResolved.add(arg);
                        } else {
                            methodsNotResolved.add(arg);
                        }
                    }
                    clInfo.params.add(arg);
                    clInfo.isResolvedParam.add(resolved);
                }
                classInstanceCreations.add(clInfo);
                return true;
            }

            public boolean visit(ConstructorInvocation node) {
                List params = node.arguments();
                for (Object s : params) {
                    boolean resolved = true;
                    String arg = ((Expression) s).toString();
                    if (arg.contains("[") && !arg.contains("\"")) {
                        arg = arg.substring(0, arg.indexOf("["));
                    }
                    if (arg.startsWith("\"") && arg.endsWith("\"")) {
                        arg = "String";
                    } else if (arg.startsWith("\'") && arg.endsWith("\'")) {
                        arg = "Character";
                    } else if (arg.contains("+") && (arg.contains("\"") || arg.contains("\'"))) {
                        arg = "String";
                    } else if (arg.contains("instanceof")) {
                        arg = arg.substring(arg.indexOf("instanceof") + 11);
                    } else if (bDeclarations.containsKey(arg)) {
                        arg = bDeclarations.get(arg);
                    } else if (arg.equals("true") || arg.equals("false")) {
                        arg = "Boolean";
                    } else if (arg.contains("==") || arg.contains(">") || arg.contains("<") || arg.contains("!=") || arg.contains(">=") || arg.contains("<=")) {
                        arg = "Boolean";
                    } else if (arg.contains("TimeUnit.SECONDS")) {
                        arg = "Numeric";
                    } else if (arg.contains(".class") && !arg.contains(".className")) {
                        arg = arg.replaceAll(".class", "");
                    } else if (arg.startsWith("new ")) {
                        arg = arg.replaceAll("new ", "");
                        if (arg.contains("(")) {
                            arg = arg.substring(0, arg.indexOf("("));
                        }
                    } else if (NumberUtils.isNumber(arg)) {
                        arg = "Numeric";
                    } else if ((arg.contains("-") || arg.contains("+") || arg.contains("*")) && !arg.contains("\"") && !arg.contains("NAME")) {
                        arg = "Numeric";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("String")) {
                        arg = "String";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".is")) {
                        arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".contains")) {
                        arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Int")) {
                        arg = "Integer";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Double")) {
                        arg = "Double";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Boolean")) {
                        arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".equals")) {
                        arg = "Boolean";
                    } else {
                        resolved = false;
                        if (!arg.contains(".") && !arg.contains("(") && !arg.contains(")")) {
                            typesNotResolved.add(arg);
                        } else {
                            methodsNotResolved.add(arg);
                        }
                    }
                }
                return true;
            }

            public boolean visit(MethodInvocation node) {
                MethodInfo mInfo = new MethodInfo();
                mInfo.expression = node.getExpression() != null ? node.getExpression().toString() : "";
                mInfo.initialexpression = mInfo.expression;
                mInfo.methodName = node.getName().toString();
                // System.out.println("ccc " + mInfo.initialexpression + " " + mInfo.methodName);
                boolean methodUnResolved = false;
                if (node.getExpression() != null) {
                    mInfo.expression = node.getExpression().toString().replaceAll("this.", "");
                    if (mInfo.expression.startsWith("\"") && mInfo.expression.endsWith("\"")) {
                        mInfo.expression = "String";
                    } else if (mInfo.expression.equals("this")) {
                        mInfo.expression = "";
                    } else if (mInfo.expression.startsWith("new ")) {
                        mInfo.expression = mInfo.expression.replaceAll("new ", "");
                        if (mInfo.expression.contains("(")) {
                            mInfo.expression = mInfo.expression.substring(0, mInfo.expression.indexOf("("));
                        }
                    }
                    if (mInfo.expression.contains("[")) {
                        mInfo.expression = mInfo.expression.substring(0, mInfo.expression.indexOf("["));
                    }
                    if (!bDeclarations.containsKey(mInfo.expression)) {
                        methodsNotResolved.add(node.getExpression().toString() + "." + node.getName() + "(" + node.arguments() + ")");
                        methodUnResolved = true;
                    } else if (bDeclarations.get(mInfo.expression) != null) {
                        if (!bDeclarations.get(mInfo.expression).contains("<")) {
                            mInfo.expression = bDeclarations.get(mInfo.expression);
                        } else {
                            mInfo.expression = bDeclarations.get(mInfo.expression).substring(0, bDeclarations.get(mInfo.expression).indexOf("<"));
                        }
                    }
                    if (mInfo.expression.contains(".") && Character.isLowerCase(mInfo.expression.substring(0, mInfo.expression.indexOf(".")).charAt(0)) && bDeclarations.containsKey(mInfo.expression.substring(0, mInfo.expression.indexOf(".")))) {
                        mInfo.expression = bDeclarations.get(mInfo.expression.substring(0, mInfo.expression.indexOf("."))) + mInfo.expression.substring(mInfo.expression.indexOf("."));
                    }
                }
                List params = node.arguments();
                // System.out.println("params : " + params);
                for (Object s : params) {
                    // System.out.println("param " + s);
                    boolean resolved = true;
                    boolean isMatrix = false;
                    boolean noMatrix = false;
                    String arg = ((Expression) s).toString();
                    mInfo.initialparams.add(arg);
                    if (arg.contains("[") && arg.indexOf("]") == arg.length() - 1 && !arg.contains("\"")) {
                        if (arg.endsWith("[]")) {
                            isMatrix = true;
                        } else
                            noMatrix = true;
                        arg = arg.substring(0, arg.indexOf("["));
                    }
                    if (arg.startsWith("this."))
                        arg = arg.replaceFirst("this.", "");
                    if (arg.startsWith("class "))
                        arg = arg.replaceFirst("class ", "");
                    if (arg.startsWith("\"") && arg.endsWith("\"")) {
                        arg = "String";
                    } else if (arg.endsWith("toString()")) {
                        arg = "String";
                    } else if (arg.startsWith("(")) {
                        arg = arg.substring(1, arg.indexOf(")"));
                    } else if (arg.startsWith("\'") && arg.endsWith("\'")) {
                        arg = "Character";
                    } else if (arg.contains("+") && (arg.contains("\"") || arg.contains("\'"))) {
                        arg = "String";
                    } else if (arg.contains("instanceof")) {
                        arg = arg.substring(arg.indexOf("instanceof") + 11);
                    } else if (bDeclarations.containsKey(arg)) {
                        arg = bDeclarations.get(arg);
                        if (arg.contains("[") && noMatrix)
                            arg = arg.substring(0, arg.indexOf("["));
                    } else if (arg.equals("true") || arg.equals("false")) {
                        arg = "Boolean";
                    } else if (arg.contains("==") || arg.contains(">") || arg.contains("<") || arg.contains("!=") || arg.contains(">=") || arg.contains("<=") || arg.contains("||")) {
                        arg = "Boolean";
                    } else if (arg.contains("TimeUnit.SECONDS")) {
                        arg = "Numeric";
                    } else if (arg.contains(".class") && !arg.contains(".className")) {
                        arg = arg.replaceAll(".class", "");
                    } else if (arg.startsWith("new ")) {
                        arg = arg.replaceAll("new ", "");
                        if (arg.contains("(")) {
                            arg = arg.substring(0, arg.lastIndexOf("("));
                        }
                        if (arg.contains("{")) {
                            arg = arg.substring(0, arg.indexOf("{"));
                        }
                    } else if (NumberUtils.isNumber(arg)) {
                        arg = "Numeric";
                    } else if ((arg.contains("-") || arg.contains("+") || arg.contains("*")) && !arg.contains("\"") && !arg.contains("NAME")) {
                        arg = "Numeric";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("String")) {
                        arg = "String";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".is")) {
                        arg = "Boolean";
                    } else if (arg.contains(".contains")) {
                        arg = arg.substring(0, arg.lastIndexOf("("));
                        if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".contains("))
                            arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Int")) {
                        arg = "Integer";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Double")) {
                        arg = "Double";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).contains("Boolean")) {
                        arg = "Boolean";
                    } else if (arg.contains(".") && arg.substring(arg.lastIndexOf(".")).startsWith(".equals")) {
                        arg = "Boolean";
                    } else {
                        resolved = false;
                        if (!arg.contains(".") && !arg.contains("(") && !arg.contains(")")) {
                            typesNotResolved.add(arg);
                        } else if (!methodUnResolved) {
                            methodsNotResolved.add(arg);
                            methodUnResolved = true;
                        }
                    }
                    if (isMatrix)
                        arg = arg + "[]";
                    mInfo.params.add(arg);
                    // System.out.println("arg " + arg);
                    mInfo.isResolvedParam.add(resolved);
                }
                // System.out.println("Invocation : " + node.getExpression() + " " + mInfo.expression +  " " + mInfo.methodName + " " + mInfo.params);
                methodInvocations.add(mInfo);
                return true;
            }

            public boolean visit(Block node) {
                if (node != null) {
                    blockIterate(node, cu, bDeclarations);
                }
                return false;
            }
        });
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) HashMap(java.util.HashMap) Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with ConstructorInvocation

use of org.eclipse.jdt.core.dom.ConstructorInvocation in project eclipse.jdt.ls by eclipse.

the class JDTUtils method resolveBinding.

private static IBinding resolveBinding(ASTNode node) {
    if (node instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) node;
        // workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
        ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
        if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
            IMethodBinding constructorBinding = cic.resolveConstructorBinding();
            if (constructorBinding == null) {
                return null;
            }
            ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
            if (!declaringClass.isAnonymous()) {
                return constructorBinding;
            }
            ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
            return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
        }
        return simpleName.resolveBinding();
    } else if (node instanceof SuperConstructorInvocation) {
        return ((SuperConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof ConstructorInvocation) {
        return ((ConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof LambdaExpression) {
        return ((LambdaExpression) node).resolveMethodBinding();
    } else {
        return null;
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 13 with ConstructorInvocation

use of org.eclipse.jdt.core.dom.ConstructorInvocation in project eclipse.jdt.ls by eclipse.

the class ChangeSignatureProcessor method containsImplicitCallToSuperConstructor.

private static boolean containsImplicitCallToSuperConstructor(MethodDeclaration constructor) {
    Assert.isTrue(constructor.isConstructor());
    Block body = constructor.getBody();
    if (body == null) {
        return false;
    }
    if (body.statements().size() == 0) {
        return true;
    }
    if (body.statements().get(0) instanceof ConstructorInvocation) {
        return false;
    }
    if (body.statements().get(0) instanceof SuperConstructorInvocation) {
        return false;
    }
    return true;
}
Also used : SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Aggregations

ConstructorInvocation (org.eclipse.jdt.core.dom.ConstructorInvocation)13 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)10 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)6 Block (org.eclipse.jdt.core.dom.Block)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 Expression (org.eclipse.jdt.core.dom.Expression)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)4 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)3 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)3 SimpleType (org.eclipse.jdt.core.dom.SimpleType)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2