Search in sources :

Example 1 with MethodDescriptor

use of meghanada.reflect.MethodDescriptor in project meghanada-server by mopemope.

the class ASMReflectorTest method testReflectWithGenerics1.

@Test
public void testReflectWithGenerics1() throws Exception {
    ASMReflector asmReflector = ASMReflector.getInstance();
    Stopwatch stopwatch = Stopwatch.createUnstarted();
    {
        String fqcn = "java.util.Map";
        File jar = getRTJar();
        Map<String, ClassIndex> index = asmReflector.getClassIndexes(jar);
        final InheritanceInfo info = asmReflector.getReflectInfo(index, fqcn);
        stopwatch.start();
        List<MemberDescriptor> memberDescriptors = asmReflector.reflectAll(info);
        System.out.println(stopwatch.stop());
        // memberDescriptors.forEach(m -> System.out.println(m.getDeclaration()));
        Config config = Config.load();
        if (config.isJava8()) {
            assertEquals(34, memberDescriptors.size());
        } else {
            assertEquals(47, memberDescriptors.size());
        }
        stopwatch.reset();
        memberDescriptors.stream().filter(memberDescriptor -> memberDescriptor.getName().equals("entrySet")).forEach(memberDescriptor -> {
            if (memberDescriptor instanceof MethodDescriptor) {
                MethodDescriptor methodDescriptor = (MethodDescriptor) memberDescriptor;
                methodDescriptor.getTypeParameterMap().put("K", "String");
                methodDescriptor.getTypeParameterMap().put("V", "Long");
                System.out.println(memberDescriptor.getReturnType());
                assertEquals("java.util.Set<java.util.Map$Entry<String, Long>>", memberDescriptor.getReturnType());
            }
        });
    }
}
Also used : AfterClass(org.junit.AfterClass) MethodDescriptor(meghanada.reflect.MethodDescriptor) BeforeClass(org.junit.BeforeClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) Stopwatch(com.google.common.base.Stopwatch) ClassIndex(meghanada.reflect.ClassIndex) Config.timeIt(meghanada.config.Config.timeIt) Test(org.junit.Test) Config.debugIt(meghanada.config.Config.debugIt) File(java.io.File) MemberDescriptor(meghanada.reflect.MemberDescriptor) List(java.util.List) Map(java.util.Map) GradleTestBase(meghanada.GradleTestBase) Config(meghanada.config.Config) Assert.assertEquals(org.junit.Assert.assertEquals) Config(meghanada.config.Config) Stopwatch(com.google.common.base.Stopwatch) List(java.util.List) File(java.io.File) Map(java.util.Map) MethodDescriptor(meghanada.reflect.MethodDescriptor) Test(org.junit.Test)

Example 2 with MethodDescriptor

use of meghanada.reflect.MethodDescriptor in project meghanada-server by mopemope.

the class MethodAnalyzeVisitor method toMemberDescriptor.

private void toMemberDescriptor() {
    String modifier = ASMReflector.toModifier(this.access, this.hasDefault);
    if (this.interfaceMethod) {
        modifier = ClassNameUtils.replace(modifier, "abstract", "").trim();
    }
    final CandidateUnit.MemberType memberType = name.equals("<init>") ? CandidateUnit.MemberType.CONSTRUCTOR : CandidateUnit.MemberType.METHOD;
    final String methodName = memberType == CandidateUnit.MemberType.CONSTRUCTOR ? this.classAnalyzeVisitor.className : this.name;
    final EntryMessage message = log.traceEntry("className={} memberType={} methodName={} returnType={}", this.classAnalyzeVisitor.className, methodName, memberType, this.returnType);
    if (methodName.startsWith("lambda$")) {
        // skip
        log.traceExit(message);
        return;
    }
    if (memberType == CandidateUnit.MemberType.CONSTRUCTOR && methodName.equals(ClassNameUtils.OBJECT_CLASS)) {
        // skip
        log.traceExit(message);
        return;
    }
    final String returnFQCN = memberType == CandidateUnit.MemberType.CONSTRUCTOR ? methodName : this.returnType.getFQCN();
    final List<MethodParameter> methodParameters = this.parameterTypes.stream().map(typeInfo -> new MethodParameter(typeInfo.getFQCN(), typeInfo.paramName)).collect(Collectors.toList());
    final MethodDescriptor descriptor = new MethodDescriptor(this.classAnalyzeVisitor.className, methodName, modifier, methodParameters, this.exceptions, returnFQCN, this.hasDefault, memberType);
    descriptor.setTypeParameters(this.typeParameters);
    log.trace("formalType={}", this.formalType);
    if (this.formalType != null) {
        descriptor.formalType = this.formalType.toString();
    }
    this.classAnalyzeVisitor.members.add(descriptor);
    log.traceExit(message);
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor) Label(org.objectweb.asm.Label) Type(org.objectweb.asm.Type) ParameterName(meghanada.reflect.names.ParameterName) ArrayList(java.util.ArrayList) Serializer(meghanada.store.Serializer) Map(java.util.Map) CandidateUnit(meghanada.reflect.CandidateUnit) Attribute(org.objectweb.asm.Attribute) Opcodes(org.objectweb.asm.Opcodes) MethodDescriptor(meghanada.reflect.MethodDescriptor) MoreObjects(com.google.common.base.MoreObjects) Set(java.util.Set) SignatureReader(org.objectweb.asm.signature.SignatureReader) Collectors(java.util.stream.Collectors) EntryMessage(org.apache.logging.log4j.message.EntryMessage) MethodParameter(meghanada.reflect.MethodParameter) List(java.util.List) Logger(org.apache.logging.log4j.Logger) TypePath(org.objectweb.asm.TypePath) ClassNameUtils(meghanada.utils.ClassNameUtils) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) MethodParameterNames(meghanada.reflect.names.MethodParameterNames) CandidateUnit(meghanada.reflect.CandidateUnit) MethodParameter(meghanada.reflect.MethodParameter) EntryMessage(org.apache.logging.log4j.message.EntryMessage) MethodDescriptor(meghanada.reflect.MethodDescriptor)

Example 3 with MethodDescriptor

use of meghanada.reflect.MethodDescriptor in project meghanada-server by mopemope.

the class ReferenceSearcher method searchMethodCallCondition.

private static Optional<SearchCondition> searchMethodCallCondition(Source source, int line, int col, String symbol) {
    EntryMessage msg = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    Optional<MethodCall> mcResult = source.getMethodCall(line, col, true);
    Optional<SearchCondition> result = mcResult.map(mc -> {
        String methodName = mc.name;
        List<String> arguments = mc.getArguments();
        String declaringClass = mc.declaringClass;
        if (isNull(declaringClass)) {
            return null;
        }
        MemberDescriptor method = searchMatchMethod(declaringClass, methodName, arguments).orElseGet(() -> searchMatchConstructor(declaringClass, arguments).orElse(null));
        if (isNull(method)) {
            // not match
            return null;
        }
        MethodDescriptor md = (MethodDescriptor) method;
        if (md.getMemberType().equals(CandidateUnit.MemberType.CONSTRUCTOR)) {
            return new SearchCondition(mc.declaringClass, mc.name, SearchCondition.Type.CONSTRUCTOR, mc.getArguments());
        }
        return new SearchCondition(mc.declaringClass, mc.name, SearchCondition.Type.METHOD, mc.getArguments());
    });
    log.traceExit(msg);
    return result;
}
Also used : MemberDescriptor(meghanada.reflect.MemberDescriptor) EntryMessage(org.apache.logging.log4j.message.EntryMessage) MethodCall(meghanada.analyze.MethodCall) MethodDescriptor(meghanada.reflect.MethodDescriptor)

Aggregations

MethodDescriptor (meghanada.reflect.MethodDescriptor)3 List (java.util.List)2 Map (java.util.Map)2 MemberDescriptor (meghanada.reflect.MemberDescriptor)2 EntryMessage (org.apache.logging.log4j.message.EntryMessage)2 MoreObjects (com.google.common.base.MoreObjects)1 Stopwatch (com.google.common.base.Stopwatch)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 GradleTestBase (meghanada.GradleTestBase)1 MethodCall (meghanada.analyze.MethodCall)1 Config (meghanada.config.Config)1 Config.debugIt (meghanada.config.Config.debugIt)1 Config.timeIt (meghanada.config.Config.timeIt)1 CandidateUnit (meghanada.reflect.CandidateUnit)1 ClassIndex (meghanada.reflect.ClassIndex)1 MethodParameter (meghanada.reflect.MethodParameter)1