Разработка intellij idea: впечатления новичка. Тагир Валеев,...

29
Разработка IntelliJ IDEA впечатления новичка Тагир Валеев

Upload: jetbrains-russia

Post on 12-Jan-2017

52 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Разработка IntelliJ IDEA—впечатления новичка

Тагир Валеев

Page 2: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Java 8 Stream API—• StreamEx library: enhancing Java 8 Streams

https://github.com/amaembo/streamex

• StackOverflow: java-stream Gold badge (один из трёх!)http://stackoverflow.com/help/badges/5670/java-stream

• Патчи в OpenJDK Stream API (~15 штук)http://hg.openjdk.java.net/jdk9/dev/jdk/log?rev=valeev&revcount=2000

Page 3: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Статический анализ кода—• FindBugs contributor

https://github.com/findbugsproject/findbugs• Поиск методов без побочных эффектов

• Анализ целочисленных диапазонов

• Десятки других детекторов

• HuntBugs projecthttps://github.com/amaembo/huntbugs

• Полностью новый проект, > 200 предупреждений (~50% функциональности FindBugs).

Page 4: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA
Page 5: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA
Page 6: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA
Page 7: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Что мне удалось сделать?—• Оптимизация цепочек вызовов Stream API

• Использование новых методов для работы с коллекциями

• Превращение циклов for в вызовы Stream API

• Много мелочей (всего ~50 закрытых задач)

Page 8: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Оптимизации вызовов Stream API—

long count = collection.stream().flatMap(List::stream).count();

long count = collection.stream().mapToLong(List::size).sum();

Page 9: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Упрощение вызовов Stream API—

int totalLength = strings.stream().collect(

Collectors.mapping(String::trim, Collectors.summingInt(String::length)));

int totalLength = strings.stream().map(String::trim).collect(

Collectors.summingInt(String::length));

Page 10: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Упрощение вызовов Stream API—

int totalLength = strings.stream().collect(

Collectors.mapping(String::trim, Collectors.summingInt(String::length)));

int totalLength = strings.stream().map(String::trim).collect(

Collectors.summingInt(String::length));

int totalLength = strings.stream().map(String::trim).mapToInt(String::length).sum();

Page 11: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Упрощение вызовов Stream API—

int totalLength = strings.stream().collect(

Collectors.mapping(String::trim, Collectors.summingInt(String::length)));

int totalLength = strings.stream().map(String::trim).collect(

Collectors.summingInt(String::length));

int totalLength = strings.stream().map(String::trim).mapToInt(String::length).sum();

int totalLength = strings.stream().mapToInt((s) -> s.trim().length()).sum();

(2017.1)

Page 12: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Использование новых методов для коллекций—Iterator<String> it =

collection.iterator();

while(it.hasNext()) {

if(it.next().contains("bad")) {

it.remove();

}

}

collection.removeIf(

s -> s.contains("bad"));

List<String> list = map.get(key);

if(list == null) {

list = new ArrayList<>();

map.put(key, list);

}

list.add(value);

List<String> list =

map.computeIfAbsent(key,

k -> new ArrayList<>());

list.add(value);

Page 13: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Преобразование циклов в Stream API—

long countNonEmpty(List<List<String>> list) {

long count = 0;

for(List<String> subList : list) {

if(subList != null) {

for(String item : subList) {

if(item != null &&

!item.isEmpty()) {

count++;

}

}

}

}

return count;

}

long count = list.stream()

.filter(Objects::nonNull)

.flatMap(Collection::stream)

.filter(item -> item != null &&

!item.isEmpty())

.count();

return count;

Page 14: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA
Page 15: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA
Page 16: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA
Page 17: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

List<String> list = this.set.stream().collect(Collectors.toList());

Page 18: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

List<String> list = this.set.stream().collect(Collectors.toList());

List<String> list = this.new ArrayList<>(set);

Page 19: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

public class CompressedRefs {

...

@NotNull

public Stream<VcsRef> stream() {

return Stream.concat(streamBranches(), streamTags());

}

@NotNull

public Collection<VcsRef> getRefs() {

return new AbstractCollection<VcsRef>() {

private final Supplier<Collection<VcsRef>> myLoadedRefs =

Suppliers.memoize(() -> CompressedRefs.this.stream()

.collect(Collectors.toList()));

...

}

...

}

...

}

Page 20: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

PsiMethod m = methodCall.resolveMethod();

if(m != null && m.getName().equals("toList") &&

m.getParameterList().getParametersCount() == 0) {

PsiClass c = m.getContainingClass();

if(c.getQualifiedName().equals("java.util.stream.Collectors")) {

// Наш коллектор!

}

}

Page 21: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

list.stream().collect(Collectors.toList());

list.stream().collect(java.util.stream.Collectors.toList());

import static java.util.stream.Collectors.toList;

list.stream().collect(toList());

PsiMethod m = methodCall.resolveMethod();

if(m != null && m.getName().equals("toList") &&

m.getParameterList().getParametersCount() == 0) {

PsiClass c = m.getContainingClass();

if(c.getQualifiedName().equals("java.util.stream.Collectors")) {

// Наш коллектор!

}

}

Page 22: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

import com.example.Collectors;

list.stream().collect(Collectors.toList());

import static com.example.MyCollectors.toList;

list.stream().collect(toList());

PsiMethod m = methodCall.resolveMethod();

if(m != null && m.getName().equals("toList") &&

m.getParameterList().getParametersCount() == 0) {

PsiClass c = m.getContainingClass();

if(c.getQualifiedName().equals("java.util.stream.Collectors")) {

// Наш коллектор!

}

}

Page 23: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

set.stream().collect(Collectors.toList());

new ArrayList<>(set);

Page 24: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

PsiExpression replacement = factory

.createExpressionFromText("new java.util.ArrayList<>(" +

collectionExpression.getText() + ")", collectCall);

PsiElement result = collectCall.replace(replacement);

JavaCodeStyleManager.getInstance(project).shortenClassReferences(result);

Page 25: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

public Iterator<Object> copyAndGetIterator(List<String> input) {

return input.stream().collect(Collectors.<Object>toList()).iterator();

}

Page 26: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

public Iterator<Object> copyAndGetIterator(List<String> input) {

return input.stream().collect(Collectors.<Object>toList()).iterator();

}

public Iterator<Object> copyAndGetIterator(List<String> input) {

return new ArrayList<>(input).iterator();

}

public Iterator<Object> copyAndGetIterator(List<String> input) {

return new ArrayList<Object>(input).iterator();

}

Page 27: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

if (PsiDiamondTypeUtil.canCollapseToDiamond(newExpr, newExpr, null)) {

PsiDiamondTypeUtil.replaceExplicitWithDiamond(classRef.getParameterList());

}

Page 28: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

set.stream().collect(Collectors.toSet());

new HashSet<>(set);

set.stream().collect(Collectors.toCollection(LinkedHashSet::new));

new LinkedHashSet<>(set);

Page 29: Разработка IntelliJ IDEA: впечатления новичка. Тагир Валеев, Senior Software Developer, IDEA

Спасибо за внимание

jetbrains.com