universal-ctags / ctags

A maintained ctags implementation
https://ctags.io
GNU General Public License v2.0
6.48k stars 620 forks source link

Parsing Java annotations #2069

Open miranska opened 5 years ago

miranska commented 5 years ago

Looking at the forums, it seems that ctags is capable of detecting some annotations. However, for example, in this file it cannot detect any. I provide the details in the template below.

Could you please tell me, is it a bug or a feature and if you have any "best practices" advice on detecting such annotations?

The name of the parser: Java

The command line you used to run ctags:

$ ctags --options=NONE

The content of input file:

/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.actuate.autoconfigure.cache;

import java.util.Map;

import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnExposedEndpoint;
import org.springframework.boot.actuate.cache.CachesEndpoint;
import org.springframework.boot.actuate.cache.CachesEndpointWebExtension;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * {@link EnableAutoConfiguration Auto-configuration} for {@link CachesEndpoint}.
 *
 * @author Johannes Edmeier
 * @author Stephane Nicoll
 * @since 2.1.0
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(CacheManager.class)
@ConditionalOnEnabledEndpoint(endpoint = CachesEndpoint.class)
@ConditionalOnExposedEndpoint(endpoint = CachesEndpoint.class)
@AutoConfigureAfter(CacheAutoConfiguration.class)
public class CachesEndpointAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public CachesEndpoint cachesEndpoint(Map<String, CacheManager> cacheManagers) {
        return new CachesEndpoint(cacheManagers);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(CachesEndpoint.class)
    public CachesEndpointWebExtension cachesEndpointWebExtension(
            CachesEndpoint cachesEndpoint) {
        return new CachesEndpointWebExtension(cachesEndpoint);
    }

}

The tags output you are not satisfied with:

CachesEndpointAutoConfiguration a.java  /^public class CachesEndpointAutoConfiguration {$/;"    c
cachesEndpoint  a.java  /^  public CachesEndpoint cachesEndpoint(Map<String, CacheManager> cacheManagers) {$/;" m   class:CachesEndpointAutoConfiguration
cachesEndpointWebExtension  a.java  /^  public CachesEndpointWebExtension cachesEndpointWebExtension($/;"   m   class:CachesEndpointAutoConfiguration
org.springframework.boot.actuate.autoconfigure.cache    a.java  /^package org.springframework.boot.actuate.autoconfigure.cache;$/;" p

The tags output you expect: something similar to annotation for 1-line objects(e.g., global variables)

The version of ctags:

$ ctags --version
Universal Ctags 0.0.0(59c2a327), Copyright (C) 2015 Universal Ctags Team
Universal Ctags is derived from Exuberant Ctags.
Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Oct 26 2018, 04:41:19
  URL: https://ctags.io/
  Optional compiled features: +wildcards, +regex, +iconv, +option-directory, +xpath, +json, +interactive, +yaml

How do you get ctags binary: https://github.com/universal-ctags/ctags-docker

masatake commented 5 years ago

Thank you. I know Java parser is broken. I need a perfect one in my daily job. I will work on after merging TypeScript parser.

masatake commented 5 years ago

Which forum? Unlike Python parser, the Java parser cannot record annotations.

miranska commented 5 years ago

Thank you @masatake, will be waiting! As for the forums, I mean old posts, like these two (where specific patterns / RegEx were used):

  1. https://sourceforge.net/p/ctags/bugs/240/
  2. https://github.com/universal-ctags/ctags/issues/219
masatake commented 5 years ago
  1. 219

What I wrote in #219 is still true. Even with the current Java parser implementation, we may be able to gather annotation information if we add code to parseJavaAnnotation.

e.g.

cachesEndpoint  a.java  /^  public CachesEndpoint cachesEndpoint(Map<String, CacheManager> cacheManagers) {$/;" m   annotations:@Bean,@ConditionalOnMissingBean class:CachesEndpointAutoConfiguration

Above output is enough for you? I cannot promise that I add the code gathering annotations and attaching them to a tag as "annotations:" field. But I would like to know what you want to do with such information. I want to reflect your idea to new Java parser (and partially current Java parser if possible.).

I'm very interested in making ctags support frameworks and DSL. See https://github.com/universal-ctags/ctags/issues/2067. The issue is about Perl. However, we can apply the concept of subparser to Java and JavaEE (or Spring).

miranska commented 5 years ago

Nice! We are doing some code analysis/parsing for research purposes, so the annotations info may be helpful. However, it is not a showstopper for our needs.