草庐IT

method-reference

全部标签

java - 是否可以将方法引用转换为 MethodHandle?

是否可以将方法引用(例如SomeClass::someMethod)转换为MethodHandle实例?我想要编译时检查的好处(确保类和方法存在)以及使用MethodHandleAPI来检查方法的能力。用例:我有当且仅当请求不是由特定方法触发时才需要执行的代码(以避免无休止的递归)。我想要编译时检查以确保类/方法存在,但运行时检查以将调用者与方法进行比较。回顾一下:是否可以将方法引用转换为MethodHandle? 最佳答案 好吧,如果您能承受额外的开销和安全隐患,您可以使用Serializable函数式接口(interface)并

Java 8 方法引用和重写方法

我在Java8中使用lambda和方法引用已有一段时间了,有一件事我不明白。这是示例代码:Setfirst=Collections.singleton(1);Setsecond=Collections.singleton(2);Setthird=Collections.singleton(3);Stream.of(first,second,third).flatMap(Collection::stream).map(String::valueOf).forEach(System.out::println);Stream.of(first,second,third).flatMap(Se

java - LambdaMetaFactory 中的类型

当我调用metafactory时出现异常。它说:java.lang.invoke.LambdaConversionException:IncorrectnumberofparametersforinstancemethodinvokeVirtualmy.ExecuteTest$AProcess.step_1:()Boolean;0capturedparameters,0functionalinterfacemethodparameters,0implementationparametersLambdaMetafactory.metafactory的文档我没有完全理解。我在找出正确的参数

Java 反射 : Checking the type of the method parameter at runtime

我需要检查方法第一个参数的类型是List>或不。有人能提出比将它与字符串进行比较更好的解决方案吗?Methodm=Foo.class.getMethod("m1",List.class);if(m.getGenericParameterTypes()[0].toString().equals("java.util.List>")){...}我的意思是这样的:List.class.isAssignableFrom((Class)((ParameterizedType)m.getGenericParameterTypes()[0]).getRawType()));检查它是否是一个列表。但是

java - Spring 表达式语言和 Spring Security 3 : accessing bean reference in @PreAuthorize

我正在尝试访问@PreAuthorize注释中的bean引用,如下所示:@PreAuthorize("@testBean.getTestValue()")publicStringtestSpEL(){....}我有一个配置如下的测试bean:@Component(value="testBean")publicclassTestBean{publicbooleangetTestValue(){returntrue;}}然而,当我尝试访问testSpEL()方法时,我遇到了以下异常:Causedby:org.springframework.expression.spel.SpelEvalu

java - "cannot find symbol: method"但声明了方法

在我的驱动程序中,这一行给我cannotfindsymbol错误,我不知道为什么。该方法在SavingsAccount类中明确定义,我可以在我的驱动程序中引用所有其他方法但不是那个,我尝试将类型更改为double,等等,但仍然无法正常工作。Accountacct2=newSavingsAccount(name);acct2.calculateBalance();SavingsAccount类继承自Account类:publicclassSavingsAccountextendsAccount{privatefinalshortminBalance=0;privatedoubleover

java - ProGuard 警告 : there were 7 unresolved references to program class members

ProGuard停止并出现大量警告:Warning:therewere1221unresolvedreferencestoclassesorinterfaces.Youmayneedtoaddmissinglibraryjarsorupdatetheirversions.Ifyourcodeworksfinewithoutthemissingclasses,youcansuppressthewarningswith'-dontwarn'options.(http://proguard.sourceforge.net/manual/troubleshooting.html#unresol

java - 从 Eclipse 导出 jar 时出现 "Could not find main method from given launch configuration"错误

我使用Eclipse作为我的IDE开发了我的程序。我曾经使用File|Export|可运行JAR文件用于为客户端创建dist构建的选项。我在eclipse中使用“运行”或(经常)“调试”按钮处理项目(因此我为项目配置了有效的主要方法)。不幸的是,当我昨天创建构建时,我在尝试运行应用程序时遇到了以下错误:FailedtoloadMain-ClassmanifestattributefromMy.jar然后当我重新尝试导出时,我在导出日志中发现了以下问题:Couldnotfindmainmethodfromgivenlaunchconfiguration我不知道是什么导致了这个问题。我认为

java - 警告 : A HTTP GET method, public javax.ws.rs.core.Response... 抛出 org.codehaus.jettison.json.JSONException,不应消耗任何实体

我有以下GET方法,它无法将结果发送回客户端。/*@GETheredefines,thismethodwillprocessHTTPGETrequests.*/@GET@Path("/test/{name}/{status}")@Produces("application/json")publicResponseName(@PathParam("name,status")Stringname,Stringstatus)throwsJSONException{Stringtotal="100";...Stringresult=""+jsonObject;returnResponse.st

Java 8 : unit-testing a method that returns a Function object

我正在尝试为返回Function的方法编写Java8单元测试;像这样的东西:classMyObject{publicFunctiongetFunction(){...}}在我的单元测试中,我创建了一个示例对象并调用了getFunction()并想将其与不适用于org.junit.Assert.assertEquals的预期功能进行比较:@TestpublicvoidgetFunction_returnsFunction(){finalMyObjectobject=newMyObject(..);finalFunctionexpectedResult=...;//thisdoesnotw