Eclipse使用的常见问题及解决

记录些开发时常遇到的问题,所有的问题都是发生并解决了才会记录,不过由于问题发生的原因多种多样,因此只能给出一般的解决办法。

本文请善用搜索(CTRL + F)功能,每个问题记录的格式为:关键信息,现象描述,解决办法,参考。

  • 在debug项目时,出现source not found

动态Web工程,之前用debug功能都是好好的,今天改了些代码,单步执行走到改的代码附近单步走不下去,一执行就会提示找不到源码。点击关联源码也会一闪而过。

首先应该确定项目确实关联了源码,我是以debug方式启动的项目,并且确实通过Edit Source Lookup Path添加了项目。然后依然提示我找不到源码,查找后发现是由于编译的class不是最新的,通过project -> clean重新编译项目,并且重新发布,我在clean后点发布,发现右下角并没有发布的进度条出现,于是把tomcat也clean了,tomcat清理后要重新添加项目,重新发布,再启动就好了。

http://www.cnblogs.com/chenpi/p/6247343.html
https://stackoverflow.com/questions/6174550/eclipse-java-debugging-source-not-found

  • 在格式化代码时,指定部分代码不要格式化

有些情况会有很长的链式代码,这种代码通常又一个方法一行,虽然没有达到行宽限制,但是我们也不希望它换行。比如这样的:

@Override
public void configure(HttpSecurity http) throws Exception {
    http
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
        .requestMatchers().antMatchers("/photos/**", "/oauth/users/**", "/oauth/clients/**","/me")
    .and()
        .authorizeRequests()
            .antMatchers("/me").access("#oauth2.hasScope('read')")                  
            .antMatchers("/photos").access("#oauth2.hasScope('read') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")                                        
            .antMatchers("/photos/trusted/**").access("#oauth2.hasScope('trust')")
            .antMatchers("/photos/user/**").access("#oauth2.hasScope('trust')")                 
            .antMatchers("/photos/**").access("#oauth2.hasScope('read') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")
            .regexMatchers(HttpMethod.DELETE, "/oauth/users/([^/].*?)/tokens/.*").access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('write')")
            .regexMatchers(HttpMethod.GET, "/oauth/clients/([^/].*?)/users/.*").access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('read')")
            .regexMatchers(HttpMethod.GET, "/oauth/clients/.*").access("#oauth2.clientHasRole('ROLE_CLIENT') and #oauth2.isClient() and #oauth2.hasScope('read')");
}

一旦格式化就变成这样了:

http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().requestMatchers()
                    .antMatchers("/photos/**", "/oauth/users/**", "/oauth/clients/**", "/me").and().authorizeRequests()
                    .antMatchers("/me").access("#oauth2.hasScope('read')").antMatchers("/photos")
                    .access("#oauth2.hasScope('read') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")
                    .antMatchers("/photos/trusted/**").access("#oauth2.hasScope('trust')")
                    .antMatchers("/photos/user/**").access("#oauth2.hasScope('trust')").antMatchers("/photos/**")
                    .access("#oauth2.hasScope('read') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")
                    .regexMatchers(HttpMethod.DELETE, "/oauth/users/([^/].*?)/tokens/.*")
                    .access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('write')")
                    .regexMatchers(HttpMethod.GET, "/oauth/clients/([^/].*?)/users/.*")
                    .access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('read')")
                    .regexMatchers(HttpMethod.GET, "/oauth/clients/.*")
                    .access("#oauth2.clientHasRole('ROLE_CLIENT') and #oauth2.isClient() and #oauth2.hasScope('read')");

所以这种情况我们是不希望它帮我们自动换行的。

解决办法是:
Windows -> Prefences -> Java -> Code Style -> Formatter -> Edit...(这一步需要创建自己的格式化模板文件,因为你不能修改Eclipse内置的默认模板) -> Off/On Tags。将Enable Off/On tags勾选上就行了,然后就能使用相应的标记。
Snipaste_2019-07-29_18-03-43.png

使用方式如上面的英文描述,Off/On tag可以在任何注释中使用以开关代码的格式化功能。在任何文件的开始,默认允许格式化,每次发现 off tag,就会禁止格式化后面的代码,每次发现on tag,就会开启格式化后面的代码。所以通常都是Off和On连用以禁止格式化部分代码,以如上代码为例,单行注释的方法如下:

@Override
        public void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            http
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
                .requestMatchers().antMatchers("/photos/**", "/oauth/users/**", "/oauth/clients/**","/me")
            .and()
                .authorizeRequests()
                    .antMatchers("/me").access("#oauth2.hasScope('read')")                  
                    .antMatchers("/photos").access("#oauth2.hasScope('read') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")                                        
                    .antMatchers("/photos/trusted/**").access("#oauth2.hasScope('trust')")
                    .antMatchers("/photos/user/**").access("#oauth2.hasScope('trust')")                 
                    .antMatchers("/photos/**").access("#oauth2.hasScope('read') or (!#oauth2.isOAuth() and hasRole('ROLE_USER'))")
                    .regexMatchers(HttpMethod.DELETE, "/oauth/users/([^/].*?)/tokens/.*").access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('write')")
                    .regexMatchers(HttpMethod.GET, "/oauth/clients/([^/].*?)/users/.*").access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('read')")
                    .regexMatchers(HttpMethod.GET, "/oauth/clients/.*").access("#oauth2.clientHasRole('ROLE_CLIENT') and #oauth2.isClient() and #oauth2.hasScope('read')");
            // @formatter:on
        }

将这段代码加上开关就不会再格式化这段代码了。

https://stackoverflow.com/questions/1820908/how-to-turn-off-the-eclipse-code-formatter-for-certain-sections-of-java-code

标签: none

添加新评论

ali-01.gifali-58.gifali-09.gifali-23.gifali-04.gifali-46.gifali-57.gifali-22.gifali-38.gifali-13.gifali-10.gifali-34.gifali-06.gifali-37.gifali-42.gifali-35.gifali-12.gifali-30.gifali-16.gifali-54.gifali-55.gifali-59.gif

加载中……