由于Java 8接口可以有默认方法.我知道如何从实现方法中显式调用该方法,即(请参阅在Java中显式调用默认方法)
但是,如何在代理上使用反射显式调用默认方法?
例:
interface ExampleMixin {
String getText();
default void printInfo(){
System.out.println(getText());
}
}
class Example {
public static void main(String... args) throws Exception {
Object target = new Object();
Map<String, BiFunction<Object, Object[], Object>> behavior = new HashMap<>();
ExampleMixin dynamic =
(ExampleMixin) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{ExampleMixin.class}, (Object proxy, Method method, Object[] arguments) -> {
//custom mixin behavior
if(behavior.containsKey(method.getName())) {
return behavior.get(method.getName()).apply(target, arguments);
//default mixin behavior
} else if (method.isDefault()) {
//this block throws java.lang.IllegalAccessException: no …
Run Code Online (Sandbox Code Playgroud) 编写Java类时,使用IDE生成方法并不罕见
toString()
equals()
hashCode()
但是一旦使用IDE生成它们,它们就会成为代码库的一部分(在SCM中),因此所有质量测量方法都适用.
特别是equals和hashcode方法包含许多条件.如果我不编写单元测试,代码覆盖率(line-,condition-,mutation-)的得分相当低,特别是如果被测试的类不那么大.
一些覆盖工具支持过滤(即cobertura),其他(即jacoco)则不支持.但覆盖工具只露出一个症状 - 未经测试的代码 - 因此我不是在问,是否要抑制/忽略症状,而是如何处理根本原因.
问题是:我应该为这些方法编写单元测试吗?
我不是要求一般生成的类,比如JAXB pojos,WS-Clients等,它们可以很容易地自动生成并从覆盖率分析中排除.
我有一组从共享类型(即继承域对象的GroupRecord extends Record
,RequestRecord extends Record
).亚型具有特定属性(即GroupRecord::getCumulativeTime
,RequestRecord::getResponseTime
).
此外,由于解析日志文件,我有一个混合子类型的记录列表.
List<Record> records = parseLog(...);
Run Code Online (Sandbox Code Playgroud)
为了计算日志记录的统计数据,我想仅在与特定子类型匹配的记录子集上应用数学函数,即仅在GroupRecord
s上.因此,我希望有一个特定子类型的过滤流.我知道我可以使用一个filter
和map
一个子类型
records.stream()
.filter(GroupRecord.class::isInstance)
.map(GroupRecord.class::cast)
.collect(...
Run Code Online (Sandbox Code Playgroud)
在流上多次应用此过滤器和强制转换(特别是对于不同的计算多次执行相同的子类型时)不仅繁琐,而且会产生大量重复.
我目前的做法是使用a TypeFilter
class TypeFilter<T>{
private final Class<T> type;
public TypeFilter(final Class<T> type) {
this.type = type;
}
public Stream<T> filter(Stream<?> inStream) {
return inStream.filter(type::isInstance).map(type::cast);
}
}
Run Code Online (Sandbox Code Playgroud)
要应用于流:
TypeFilter<GroupRecord> groupFilter = new TypeFilter(GroupRecord.class);
SomeStatsResult stats1 = groupFilter.filter(records.stream())
.collect(...)
SomeStatsResult stats2 = groupFilter.filter(records.stream())
.collect(...)
Run Code Online (Sandbox Code Playgroud)
它有效,但我发现这种方法对于这么简单的任务来说有点多了.因此我想知道,使用流和函数以简洁和可读的方式使这种行为可重用是否有更好或更好的方法是什么?
我的目标是编写一个框架,用于测量方法执行或事务时间以及处理测量,即存储,分析等.事务可能包括调用外部系统,同步或异步等待结果.
关于这个话题已经存在一些问题,比如
所有的答案归结为三种方法来花时间
System.currentTimeMillis()
System.nanoTime()
Instant.now()
和Duration
(自Java 8以来)我知道,所有这些都有一些影响
该方法的结果取决于平台.在Linux上你获得1ms的分辨率,在Windows中你得到10ms(单核)~15ms(多核).因此,可以测量大型运行操作或短期运行操作的多次执行.
你得到一个高分辨率的时间测量,具有纳秒精度(但不一定是纳秒精度),并且在292年之后你会得到溢出(我可以忍受它).
从Java 8开始,就有了新的时间API.瞬间有第二个和第二个纳米字段,因此它在对象引用的顶部使用两个长值(相同Duration
).您还可以获得纳秒精度,具体取决于基础时钟(请参阅" 具有纳秒分辨率的Java 8 Instant.now()? ").实例化是通过调用Instant.now()
哪些映射到System.currentTimeMillis()
正常的系统时钟来完成的.
鉴于事实,很明显,最佳精确度只能通过实现System.nanoTime()
,但我的问题更多地针对处理一般措施的最佳做法,这不仅包括采取措施,还包括措施处理.
Instant和Duration提供最佳API支持(计算,比较等),但在标准情况下具有os-dependend精度,内存和创建度量的更多开销(对象构造,更深的callstack)
System.nanoTime()和System.currentTimeMillis()具有不同的精度级别,但只有基本的"api"支持(数学操作为long),但更快和更小以保持在内存中.
那么最好的方法是什么?有没有我没想过的含义?还有其他选择吗?
在编写 javadoc 时,我偶尔会有列表,即
/**
* <ul>
* <li>item1</li>
* <li>item2</li>
* </ul>
*/
Run Code Online (Sandbox Code Playgroud)
当我重新格式化代码 ( Ctrl+Alt+L
) 时,缩进被删除:
/**
* <ul>
* <li>item1</li>
* <li>item2</li>
* </ul>
*/
Run Code Online (Sandbox Code Playgroud)
更糟糕的是,一旦我将列表放在@param
or@throws
声明下(即列出选项或抛出异常时的情况)并且我点击重新格式化代码,整个列表就会变成单行
/**
* @throws Exception
* <ul> <li>item1</li> <li>item2</li> </ul>
*/
Run Code Online (Sandbox Code Playgroud)
这对于阅读和维护来说确实很糟糕。
那么我该如何配置 IntelliJ
我有一张地图.
Map<Integer,String> map = ...
Run Code Online (Sandbox Code Playgroud)
这个地图有n个元素(这个例子就是9个)
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
map.put(4,"four");
map.put(5,"five");
map.put(6,"six");
map.put(7,"seven");
map.put(8,"eigth");
map.put(9,"nine");
Run Code Online (Sandbox Code Playgroud)
现在我想迭代这个映射,并使用迭代器删除第n个元素.
private void remove(int num, final Map<Integer, String> map) {
Iterator<Map.Entry<Integer,String>> it = map.entrySet().iterator();
Map.Entry<Integer,String> entry;
while(it.hasNext()){
entry = it.next();
if(Integer.valueOf(num).equals(entry.getKey())){
it.remove();
System.out.println(entry.getValue());
// vs
// System.out.println(entry.getValue());
// it.remove();
}
}
}
Run Code Online (Sandbox Code Playgroud)
从javadoc,我假设,删除的语义很明确.
但是根据地图的实现 - 即HashMap vs TreeMap,无论it.remove()
是在之前还是之后 都有所不同entry.getValue()
.
对于HashMaps map = new HashMap<>()
,行为是
...
remove(4, map); //output: four
//or
remove(5, map); //output: five
Run Code Online (Sandbox Code Playgroud)
对于TreeMap map = …
我正在使用Java9模块系统(在openjdk11上运行)
我有
此类的单元测试尝试加载两个测试服务实现
测试实现列在 META-INF/services 文件中
src/main/example/Service.java
public interface Service {
public static List<Service> loadServices(){
return StreamSupport.stream(ServiceLoader.load(Service.class).spliterator(),false)
.collect(Collectors.toList());
}
}
Run Code Online (Sandbox Code Playgroud)
和一个
src/main/module-info.java
module example {
uses example.Service;
exports example;
}
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的单元测试
src/main/example/ServiceTest.java
public ServiceTest {
@Test
void loadServices_should_find_TestServices{
List<Service> services = Service.loadServices();
assertEquals(2, services.size());
}
}
Run Code Online (Sandbox Code Playgroud)
我在测试源中有两个测试服务:
src/main/example/TestService1.java
public TestService1 implements Service {}
Run Code Online (Sandbox Code Playgroud)
src/main/example/TestService2.java
public TestService2 implements Service {}
Run Code Online (Sandbox Code Playgroud)
src/test/resources/META-INF/services/example.Service
example.TestService1
example.TestService2
Run Code Online (Sandbox Code Playgroud)
我使用 maven-surefire-plugin 3.0.0-M3 没有任何特定配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
Run Code Online (Sandbox Code Playgroud)
它正确地修补了示例模块(来自surefireargs文件)
--patch-module example="D:\\git\\example\\target\\test-classes"
Run Code Online (Sandbox Code Playgroud)
当我直接在IntelliJ中执行测试时,测试运行成功,找到了两个服务。但是,当我在maven中构建模块并通过surefire执行测试时,它找不到服务并且测试失败。 …
当我在集合上创建索引时,结果文档的一个属性是createCollectionAutomatically:false
.
db.myCollection.createIndex({"address":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
Run Code Online (Sandbox Code Playgroud)
这是什么意思,什么时候是真的?
我想显示两个 UILabel
,但是它们UILabel
的文本长度是可变的。根据textsize UIlabel
的宽度需要使用自动布局来增加。
为此,我在下面为这两个UIlabel
的自动布局
第一个标签:
1)leading Space
2)Top space
3)Width
4)height
5)Horizontal spacing
Run Code Online (Sandbox Code Playgroud)
第二标签:
1)Trailing space
2)Top space
3)Width
4)height
Run Code Online (Sandbox Code Playgroud)
我们应该怎么做 ?
请帮我。
textLabel1.numberOfLines = 0
textLabel1 .sizeToFit()
textLabel1.text = "asdfdsfdghjgjhkhkjlhjkhjk"
textLabel2.numberOfLines = 0
textLabel2 .sizeToFit()
textLabel2.text = "asdfdsfdghjgjhkhkjlhjkhjk"
Run Code Online (Sandbox Code Playgroud) java ×7
java-8 ×3
unit-testing ×2
formatting ×1
ios ×1
iterator ×1
java-9 ×1
java-stream ×1
javadoc ×1
module-info ×1
mongodb ×1
performance ×1
qa ×1
reflection ×1
time ×1
timing ×1
uilabel ×1