Skip to content

Commit

Permalink
create elements via reflection without anonymous classes
Browse files Browse the repository at this point in the history
  • Loading branch information
VISTALL committed Dec 2, 2017
1 parent b3220e7 commit 82c2d6b
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 1,894 deletions.
36 changes: 34 additions & 2 deletions src/com/perl5/lang/perl/parser/elementTypes/PerlElementTypeEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,49 @@
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;

/**
* Created by hurricup on 19.01.2016.
*/
public class PerlElementTypeEx extends PerlElementType implements PsiElementProvider {
private final Function<ASTNode, PsiElement> myInstanceFactory;

public PerlElementTypeEx(@NotNull @NonNls String debugName) {
this(debugName, PerlCompositeElementImpl.class);
}

public PerlElementTypeEx(@NotNull @NonNls String debugName, Class<? extends PsiElement> clazz) {
super(debugName);
myInstanceFactory = createInstanceFactory(clazz);
}

@NotNull
static Function<ASTNode, PsiElement> createInstanceFactory(Class<? extends PsiElement> clazz) {
Constructor<? extends PsiElement> constructor;
try {
constructor = clazz.getDeclaredConstructor(ASTNode.class);
constructor.setAccessible(true);
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}

return p1 -> {
try {
return constructor.newInstance(p1);
}
catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
};
}

@NotNull
@Override
public PsiElement getPsiElement(@NotNull ASTNode node) {
return new PerlCompositeElementImpl(node);
return myInstanceFactory.apply(node);
}
}
}
Loading

0 comments on commit 82c2d6b

Please sign in to comment.