Search in sources :

Example 46 with EntryMessage

use of org.apache.logging.log4j.message.EntryMessage in project meghanada-server by mopemope.

the class MethodSignatureVisitor method visitTypeVariable.

@Override
public void visitTypeVariable(String typeVariable) {
    final EntryMessage message = log.traceEntry("typeVariable={}", typeVariable);
    TypeInfo typeInfo;
    if (this.typeMap != null && typeMap.containsKey(typeVariable)) {
        String val = typeMap.get(typeVariable);
        if (val.equals(typeVariable)) {
            MethodSignatureVisitor.getTopVisitor(this).typeParameters.add(typeVariable);
            typeVariable = ClassNameUtils.CLASS_TYPE_VARIABLE_MARK + typeVariable;
        } else {
            ClassNameUtils.getTypeVariable(val).ifPresent(tv -> MethodSignatureVisitor.getTopVisitor(this).typeParameters.add(tv));
            typeVariable = val;
        }
    } else {
        if (this.classTypeParameters.contains(typeVariable)) {
            // mark
            MethodSignatureVisitor.getTopVisitor(this).typeParameters.add(typeVariable);
            typeVariable = ClassNameUtils.CLASS_TYPE_VARIABLE_MARK + typeVariable;
        } else {
            MethodSignatureVisitor.getTopVisitor(this).typeParameters.add(typeVariable);
            typeVariable = ClassNameUtils.FORMAL_TYPE_VARIABLE_MARK + typeVariable;
        }
    }
    typeInfo = getTypeInfo(typeVariable);
    if (this.isReturn) {
        if (this.current == null) {
            // add direct
            typeInfo.isArray = this.isArray;
            this.parent.returnType = typeInfo;
        } else {
            if (this.current.typeParameters == null) {
                this.current.typeParameters = new ArrayList<>(4);
            }
            this.current.typeParameters.add(typeInfo);
        }
        log.traceExit(message);
        return;
    }
    if (this.isParameter) {
        if (isSuper || isExtends) {
            if (this.current.typeParameters == null) {
                this.current.typeParameters = new ArrayList<>(4);
            }
            this.current.typeParameters.add(typeInfo);
        } else {
            if (this.current != null) {
                if (this.current.typeParameters == null) {
                    this.current.typeParameters = new ArrayList<>(4);
                }
                this.current.typeParameters.add(typeInfo);
            } else {
                typeInfo.isArray = this.isArray;
                this.parent.parameterTypes.add(typeInfo);
            }
        }
    }
    log.traceExit(message);
}
Also used : EntryMessage(org.apache.logging.log4j.message.EntryMessage)

Example 47 with EntryMessage

use of org.apache.logging.log4j.message.EntryMessage in project meghanada-server by mopemope.

the class MethodSignatureVisitor method visitClassBound.

@Override
public SignatureVisitor visitClassBound() {
    final EntryMessage message = log.traceEntry("current={}", this.current);
    MethodSignatureVisitor visitor = new MethodSignatureVisitor(this.name, this);
    visitor.current = this.formalType;
    this.formalType = null;
    visitor.isFormalType = true;
    visitor.isClassBound = true;
    log.traceExit(message);
    return visitor;
}
Also used : EntryMessage(org.apache.logging.log4j.message.EntryMessage)

Example 48 with EntryMessage

use of org.apache.logging.log4j.message.EntryMessage in project meghanada-server by mopemope.

the class TreeAnalyzer method analyzeParsedTree.

private static void analyzeParsedTree(SourceContext context, @Nullable JCTree tree) throws IOException {
    if (isNull(tree)) {
        return;
    }
    JCDiagnostic.DiagnosticPosition pos = tree.pos();
    EndPosTable endPosTable = context.getEndPosTable();
    int startPos = pos.getStartPosition();
    int preferredPos = pos.getPreferredPosition();
    int endPos = pos.getEndPosition(endPosTable);
    EntryMessage em = log.traceEntry("# class={} preferredPos={} endPos={} expr='{}'", tree.getClass().getSimpleName(), preferredPos, endPos, tree);
    if (endPos == -1 && !(tree instanceof JCTree.JCAssign) && !(tree instanceof JCTree.JCIdent)) {
        // skip
        log.trace("skip expr={}", tree);
        log.traceExit(em);
        return;
    }
    if (tree instanceof JCTree.JCVariableDecl) {
        analyzeVariableDecl(context, (JCTree.JCVariableDecl) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCTypeCast) {
        JCTree.JCTypeCast cast = (JCTree.JCTypeCast) tree;
        JCTree.JCExpression expression = cast.getExpression();
        analyzeParsedTree(context, expression);
        if (context.isArgument()) {
            getExpressionType(context.getSource(), cast).ifPresent(context::setArgumentFQCN);
        }
    } else if (tree instanceof JCTree.JCMethodDecl) {
        analyzeMethodDecl(context, (JCTree.JCMethodDecl) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCClassDecl) {
        analyzeInnerClassDecl(context, (JCTree.JCClassDecl) tree, startPos, endPos);
    } else if (tree instanceof JCTree.JCBlock) {
        JCTree.JCBlock block = (JCTree.JCBlock) tree;
        int argumentIndex = context.getArgumentIndex();
        context.setArgumentIndex(-1);
        analyzeSimpleStatements(context, block.getStatements());
        context.setArgumentIndex(argumentIndex);
    } else if (tree instanceof JCTree.JCFieldAccess) {
        analyzeFieldAccess(context, (JCTree.JCFieldAccess) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCArrayAccess) {
        JCTree.JCArrayAccess arrayAccess = (JCTree.JCArrayAccess) tree;
        analyzeParsedTree(context, arrayAccess.getExpression());
        analyzeParsedTree(context, arrayAccess.getIndex());
    } else if (tree instanceof JCTree.JCExpressionStatement) {
        analyzeExpressionStatement(context, (JCTree.JCExpressionStatement) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCLiteral) {
        analyzeLiteral(context, (JCTree.JCLiteral) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCIdent) {
        analyzeIdent(context, (JCTree.JCIdent) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCContinue) {
    // skip
    } else if (tree instanceof JCTree.JCBreak) {
    // skip
    } else if (tree instanceof JCTree.JCUnary) {
        JCTree.JCUnary unary = (JCTree.JCUnary) tree;
        JCTree.JCExpression expression = unary.getExpression();
        analyzeParsedTree(context, expression);
    } else if (tree instanceof JCTree.JCSwitch) {
        analyzeSwitch(context, (JCTree.JCSwitch) tree);
    } else if (tree instanceof JCTree.JCReturn) {
        JCTree.JCReturn ret = (JCTree.JCReturn) tree;
        JCTree.JCExpression expression = ret.getExpression();
        analyzeParsedTree(context, ret.getExpression());
    } else if (tree instanceof JCTree.JCForLoop) {
        analyzeForLoop(context, (JCTree.JCForLoop) tree);
    } else if (tree instanceof JCTree.JCEnhancedForLoop) {
        analyzeEnhancedForLoop(context, (JCTree.JCEnhancedForLoop) tree);
    } else if (tree instanceof JCTree.JCTry) {
        analyzeTry(context, (JCTree.JCTry) tree);
    } else if (tree instanceof JCTree.JCIf) {
        JCTree.JCIf ifExpr = (JCTree.JCIf) tree;
        JCTree.JCExpression condition = ifExpr.getCondition();
        JCTree.JCStatement thenStatement = ifExpr.getThenStatement();
        JCTree.JCStatement elseStatement = ifExpr.getElseStatement();
        analyzeParsedTree(context, condition);
        analyzeParsedTree(context, thenStatement);
        analyzeParsedTree(context, elseStatement);
    } else if (tree instanceof JCTree.JCParens) {
        JCTree.JCParens parens = (JCTree.JCParens) tree;
        JCTree.JCExpression expression = parens.getExpression();
        analyzeParsedTree(context, expression);
    } else if (tree instanceof JCTree.JCNewClass) {
        analyzeNewClass(context, (JCTree.JCNewClass) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCBinary) {
        JCTree.JCBinary binary = (JCTree.JCBinary) tree;
        JCTree.JCExpression leftOperand = binary.getLeftOperand();
        JCTree.JCExpression rightOperand = binary.getRightOperand();
        analyzeParsedTree(context, leftOperand);
        analyzeParsedTree(context, rightOperand);
    } else if (tree instanceof JCTree.JCMethodInvocation) {
        analyzeMethodInvocation(context, (JCTree.JCMethodInvocation) tree, preferredPos, endPos);
    } else if (tree instanceof JCTree.JCAssign) {
        JCTree.JCAssign assign = (JCTree.JCAssign) tree;
        JCTree.JCExpression expression = assign.getExpression();
        JCTree.JCExpression variable = assign.getVariable();
        analyzeParsedTree(context, variable);
        analyzeParsedTree(context, expression);
    } else if (tree instanceof JCTree.JCNewArray) {
        JCTree.JCNewArray newArray = (JCTree.JCNewArray) tree;
        JCTree.JCExpression type = newArray.getType();
        analyzeParsedTree(context, type);
        List<JCTree.JCExpression> initializes = newArray.getInitializers();
        analyzeSimpleExpressions(context, initializes);
        List<JCTree.JCExpression> dimensions = newArray.getDimensions();
        analyzeSimpleExpressions(context, dimensions);
        if (nonNull(newArray.type)) {
            getTypeString(context.getSource(), newArray.type).ifPresent(context::setArgumentFQCN);
        }
    } else if (tree instanceof JCTree.JCPrimitiveTypeTree) {
    // skip
    } else if (tree instanceof JCTree.JCConditional) {
        JCTree.JCConditional conditional = (JCTree.JCConditional) tree;
        JCTree.JCExpression condition = conditional.getCondition();
        analyzeParsedTree(context, condition);
        JCTree.JCExpression trueExpression = conditional.getTrueExpression();
        analyzeParsedTree(context, trueExpression);
        JCTree.JCExpression falseExpression = conditional.getFalseExpression();
        analyzeParsedTree(context, falseExpression);
    } else if (tree instanceof JCTree.JCLambda) {
        analyzeLambda(context, (JCTree.JCLambda) tree);
    } else if (tree instanceof JCTree.JCThrow) {
        JCTree.JCThrow jcThrow = (JCTree.JCThrow) tree;
        JCTree.JCExpression expression = jcThrow.getExpression();
        analyzeParsedTree(context, expression);
    } else if (tree instanceof JCTree.JCInstanceOf) {
        JCTree.JCInstanceOf jcInstanceOf = (JCTree.JCInstanceOf) tree;
        JCTree.JCExpression expression = jcInstanceOf.getExpression();
        analyzeParsedTree(context, expression);
        JCTree typeTree = jcInstanceOf.getType();
        analyzeParsedTree(context, typeTree);
    } else if (tree instanceof JCTree.JCMemberReference) {
        Source src = context.getSource();
        JCTree.JCMemberReference memberReference = (JCTree.JCMemberReference) tree;
        JCTree.JCExpression expression = memberReference.getQualifierExpression();
        com.sun.tools.javac.util.Name name = memberReference.getName();
        String methodName = name.toString();
        if (nonNull(expression)) {
            analyzeParsedTree(context, expression);
            Symbol sym = memberReference.sym;
            if (nonNull(sym)) {
                // method invoke
                int start = expression.getEndPosition(endPosTable) + 2;
                Range range = Range.create(src, start, start + methodName.length());
                String s = memberReference.toString();
                MethodCall methodCall = new MethodCall(s, methodName, preferredPos + 1, range, range);
                if (sym instanceof Symbol.MethodSymbol) {
                    Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) sym;
                    java.util.List<String> arguments = new ArrayList<>(methodSymbol.getParameters().size());
                    for (VarSymbol varSymbol : methodSymbol.getParameters()) {
                        arguments.add(varSymbol.asType().toString());
                    }
                    methodCall.setArguments(arguments);
                }
                Symbol owner = sym.owner;
                if (nonNull(owner) && nonNull(owner.type)) {
                    getTypeString(src, owner.type).ifPresent(fqcn -> methodCall.declaringClass = TreeAnalyzer.markFQCN(src, fqcn));
                }
                if (nonNull(sym.type)) {
                    getTypeString(src, sym.type).ifPresent(fqcn -> {
                        methodCall.returnType = TreeAnalyzer.markFQCN(src, fqcn);
                    // TODO add args
                    });
                }
                src.getCurrentScope().ifPresent(scope -> scope.addMethodCall(methodCall));
            }
        }
    } else if (tree instanceof JCTree.JCWhileLoop) {
        JCTree.JCWhileLoop whileLoop = (JCTree.JCWhileLoop) tree;
        JCTree.JCExpression condition = whileLoop.getCondition();
        analyzeParsedTree(context, condition);
        JCTree.JCStatement statement = whileLoop.getStatement();
        analyzeParsedTree(context, statement);
    } else if (tree instanceof JCTree.JCSynchronized) {
        JCTree.JCSynchronized jcSynchronized = (JCTree.JCSynchronized) tree;
        JCTree.JCExpression expression = jcSynchronized.getExpression();
        analyzeParsedTree(context, expression);
        JCTree.JCBlock block = jcSynchronized.getBlock();
        analyzeParsedTree(context, block);
    } else if (tree instanceof JCTree.JCAssert) {
        JCTree.JCAssert jcAssert = (JCTree.JCAssert) tree;
        JCTree.JCExpression condition = jcAssert.getCondition();
        analyzeParsedTree(context, condition);
        JCTree.JCExpression detail = jcAssert.getDetail();
        analyzeParsedTree(context, detail);
    } else if (tree instanceof JCTree.JCArrayTypeTree) {
        JCTree.JCArrayTypeTree arrayTypeTree = (JCTree.JCArrayTypeTree) tree;
        JCTree type = arrayTypeTree.getType();
        analyzeParsedTree(context, type);
    } else if (tree instanceof JCTree.JCDoWhileLoop) {
        JCTree.JCDoWhileLoop doWhileLoop = (JCTree.JCDoWhileLoop) tree;
        JCTree.JCExpression condition = doWhileLoop.getCondition();
        analyzeParsedTree(context, condition);
        JCTree.JCStatement statement = doWhileLoop.getStatement();
        analyzeParsedTree(context, statement);
    } else if (tree instanceof JCTree.JCLabeledStatement) {
        JCTree.JCLabeledStatement labeledStatement = (JCTree.JCLabeledStatement) tree;
        JCTree.JCStatement statement = labeledStatement.getStatement();
        analyzeParsedTree(context, statement);
    } else if (tree instanceof JCTree.JCTypeApply) {
        JCTree.JCTypeApply typeApply = (JCTree.JCTypeApply) tree;
        JCTree type = typeApply.getType();
        analyzeParsedTree(context, type);
    } else if (tree instanceof JCTree.JCAssignOp) {
        JCTree.JCAssignOp assignOp = (JCTree.JCAssignOp) tree;
        JCTree.JCExpression expression = assignOp.getExpression();
        analyzeParsedTree(context, expression);
    } else if (tree instanceof JCTree.JCAnnotation) {
        JCTree.JCAnnotation annotation = (JCTree.JCAnnotation) tree;
        analyzeSimpleExpressions(context, annotation.getArguments());
        JCTree annotationType = annotation.getAnnotationType();
        analyzeParsedTree(context, annotationType);
    } else if (tree instanceof JCTree.JCSkip) {
    // skip
    } else if (tree instanceof JCTree.JCErroneous) {
    // skip error
    } else {
        Source src = context.getSource();
        log.warn("@@ unknown or broken tree class={} expr={} filePath={}", tree.getClass(), tree, src.filePath);
    }
    log.traceExit(em);
}
Also used : JCDiagnostic(com.sun.tools.javac.util.JCDiagnostic) ArrayList(java.util.ArrayList) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) EntryMessage(org.apache.logging.log4j.message.EntryMessage) JCTree(com.sun.tools.javac.tree.JCTree) EndPosTable(com.sun.tools.javac.tree.EndPosTable) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Example 49 with EntryMessage

use of org.apache.logging.log4j.message.EntryMessage in project meghanada-server by mopemope.

the class DeclarationSearcher method searchLocalVariable.

private static Optional<Declaration> searchLocalVariable(final Source source, final Integer line, final Integer col, final String symbol) {
    final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    final Optional<Variable> variable = source.getVariable(line, col);
    final Optional<Declaration> result = variable.map(var -> {
        final Declaration declaration = new Declaration(symbol, var.fqcn, Declaration.Type.VAR, var.argumentIndex);
        return Optional.of(declaration);
    }).orElseGet(() -> searchFieldVar(source, line, symbol));
    log.traceExit(entryMessage);
    return result;
}
Also used : FileUtils.getSource(meghanada.utils.FileUtils.getSource) ClassIndex(meghanada.reflect.ClassIndex) Set(java.util.Set) IOException(java.io.IOException) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) EntryMessage(org.apache.logging.log4j.message.EntryMessage) File(java.io.File) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ExecutionException(java.util.concurrent.ExecutionException) Variable(meghanada.analyze.Variable) MemberDescriptor(meghanada.reflect.MemberDescriptor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClassNameUtils(meghanada.utils.ClassNameUtils) Optional(java.util.Optional) Project(meghanada.project.Project) MethodCall(meghanada.analyze.MethodCall) ClassScope(meghanada.analyze.ClassScope) Source(meghanada.analyze.Source) LogManager(org.apache.logging.log4j.LogManager) Joiner(com.google.common.base.Joiner) Variable(meghanada.analyze.Variable) EntryMessage(org.apache.logging.log4j.message.EntryMessage)

Example 50 with EntryMessage

use of org.apache.logging.log4j.message.EntryMessage in project meghanada-server by mopemope.

the class DeclarationSearcher method searchClassOrInterface.

private static Optional<Declaration> searchClassOrInterface(final Source source, final Integer line, final Integer col, final String symbol) {
    // TODO need tune
    final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    Optional<Declaration> result;
    String fqcn = source.getImportedClassFQCN(symbol, null);
    if (fqcn == null) {
        if (!source.getPackageName().isEmpty()) {
            fqcn = source.getPackageName() + '.' + symbol;
            result = reflector.containsClassIndex(fqcn).map(classIndex -> {
                final Declaration declaration = new Declaration(symbol, classIndex.getReturnType(), Declaration.Type.CLASS, 0);
                return Optional.of(declaration);
            }).orElseGet(() -> {
                final Set<String> parents = new HashSet<>(8);
                for (final ClassScope classScope : source.getClassScopes()) {
                    final String className = classScope.getFQCN();
                    parents.add(className);
                }
                parents.addAll(source.importClasses);
                for (final ClassIndex ci : reflector.searchInnerClasses(parents)) {
                    final String returnType = ci.getReturnType();
                    if (returnType.endsWith(symbol)) {
                        final Declaration d = new Declaration(symbol, returnType, Declaration.Type.CLASS, 0);
                        return Optional.of(d);
                    }
                }
                return Optional.empty();
            });
        } else {
            result = Optional.empty();
        }
    } else {
        final Declaration declaration = new Declaration(symbol, fqcn, Declaration.Type.CLASS, 0);
        result = Optional.of(declaration);
    }
    log.traceExit(entryMessage);
    return result;
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) Set(java.util.Set) HashSet(java.util.HashSet) EntryMessage(org.apache.logging.log4j.message.EntryMessage) ClassScope(meghanada.analyze.ClassScope)

Aggregations

EntryMessage (org.apache.logging.log4j.message.EntryMessage)64 ArrayList (java.util.ArrayList)9 CachedASMReflector (meghanada.reflect.asm.CachedASMReflector)8 Test (org.junit.jupiter.api.Test)8 ClassScope (meghanada.analyze.ClassScope)5 MethodCall (meghanada.analyze.MethodCall)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 ExecutionException (java.util.concurrent.ExecutionException)3 Variable (meghanada.analyze.Variable)3 Project (meghanada.project.Project)3 ClassIndex (meghanada.reflect.ClassIndex)3 ClassNameUtils (meghanada.utils.ClassNameUtils)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 SignatureReader (org.objectweb.asm.signature.SignatureReader)3