use of japa.parser.ast.type.ReferenceType in project jsonschema2pojo by joelittlejohn.
the class TypeUtil method buildClass.
private static JClass buildClass(JClassContainer _package, ClassOrInterfaceType c, int arrayCount) {
final String packagePrefix = (c.getScope() != null) ? c.getScope().toString() + "." : "";
JClass _class;
try {
_class = _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(packagePrefix + c.getName()));
} catch (ClassNotFoundException e) {
_class = _package.owner().ref(packagePrefix + c.getName());
}
for (int i = 0; i < arrayCount; i++) {
_class = _class.array();
}
List<Type> typeArgs = c.getTypeArgs();
if (typeArgs != null && typeArgs.size() > 0) {
JClass[] genericArgumentClasses = new JClass[typeArgs.size()];
for (int i = 0; i < typeArgs.size(); i++) {
genericArgumentClasses[i] = buildClass(_package, (ClassOrInterfaceType) ((ReferenceType) typeArgs.get(i)).getType(), ((ReferenceType) typeArgs.get(i)).getArrayCount());
}
_class = _class.narrow(genericArgumentClasses);
}
return _class;
}
use of japa.parser.ast.type.ReferenceType in project enumerable by hraberg.
the class ExpressionInterpreter method newOperation.
public Value newOperation(final AbstractInsnNode insn) throws AnalyzerException {
switch(insn.getOpcode()) {
case ACONST_NULL:
return new ExpressionValue(createClassOrInterfaceType(Object.class.getName()), new NullLiteralExpr());
case ICONST_M1:
return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(new IntegerLiteralExpr("1"), UnaryExpr.Operator.negative));
case ICONST_0:
return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("0"));
case ICONST_1:
return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("1"));
case ICONST_2:
return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("2"));
case ICONST_3:
return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("3"));
case ICONST_4:
return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("4"));
case ICONST_5:
return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("5"));
case LCONST_0:
return new ExpressionValue(PRIMITIVE_LONG, new LongLiteralExpr("0L"));
case LCONST_1:
return new ExpressionValue(PRIMITIVE_LONG, new LongLiteralExpr("1L"));
case FCONST_0:
return new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr("0.0f"));
case FCONST_1:
return new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr("1.0f"));
case FCONST_2:
return new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr("2.0f"));
case DCONST_0:
return new ExpressionValue(PRIMITIVE_DOUBLE, new DoubleLiteralExpr("0.0"));
case DCONST_1:
return new ExpressionValue(PRIMITIVE_DOUBLE, new DoubleLiteralExpr("1.0"));
case BIPUSH:
case SIPUSH:
int operand = ((IntInsnNode) insn).operand;
if (operand < 0)
return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(new IntegerLiteralExpr("" + Math.abs(operand)), UnaryExpr.Operator.negative));
return new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr("" + operand));
case LDC:
Object cst = ((LdcInsnNode) insn).cst;
if (cst instanceof Number) {
ExpressionValue value = null;
if (cst instanceof Integer) {
value = new ExpressionValue(PRIMITIVE_INT, new IntegerLiteralExpr(cst.toString()));
} else if (cst instanceof Float) {
value = new ExpressionValue(PRIMITIVE_FLOAT, new DoubleLiteralExpr(cst.toString() + "f"));
} else if (cst instanceof Long) {
value = new ExpressionValue(PRIMITIVE_LONG, new LongLiteralExpr(cst.toString() + "L"));
} else if (cst instanceof Double) {
value = new ExpressionValue(PRIMITIVE_DOUBLE, new DoubleLiteralExpr(cst.toString()));
}
if (((Number) cst).intValue() < 0) {
StringLiteralExpr expr = (StringLiteralExpr) value.expression;
expr.setValue(expr.getValue().substring("-".length()));
value.expression = new UnaryExpr(expr, UnaryExpr.Operator.negative);
}
return value;
} else if (cst instanceof Type) {
ClassExpr classExpr = new ClassExpr(new ReferenceType(createClassOrInterfaceType(((Type) cst).getClassName())));
return new ExpressionValue(createClassOrInterfaceType(Class.class.getName()), classExpr);
} else {
return new ExpressionValue(createClassOrInterfaceType(String.class.getName()), new StringLiteralExpr(cst.toString()));
}
case JSR:
throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
case GETSTATIC:
FieldInsnNode fieldNode = (FieldInsnNode) insn;
ExpressionValue getField = (ExpressionValue) newValue(getType(fieldNode.desc));
getField.expression = new FieldAccessExpr(new NameExpr(removeJavaLang(getObjectType(fieldNode.owner).getClassName())), fieldNode.name);
return getField;
case NEW:
return newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
default:
throw new Error("Internal error.");
}
}
use of japa.parser.ast.type.ReferenceType in project Japid by branaway.
the class JavaSyntaxTool method parseCode.
public static List<CodeNode> parseCode(String code) {
try {
final List<CodeNode> nodes = new LinkedList();
CompilationUnit cu = parse(code);
VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
int nested = 0;
@Override
public void visit(AnnotationDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AnnotationMemberDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayAccessExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayCreationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayInitializerExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AssertStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AssignExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BinaryExpr n, Object arg) {
if (n.getOperator() == Operator.binOr) {
nodes.add(new CodeNode(nested++, new BinaryOrExpr(n)));
} else {
nodes.add(new CodeNode(nested++, n));
}
super.visit(n, arg);
nested--;
}
@Override
public void visit(BlockComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BlockStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BooleanLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BreakStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CastExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CatchClause n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CharLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassOrInterfaceType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CompilationUnit n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ConditionalExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ConstructorDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ContinueStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(DoStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(DoubleLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyMemberDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyTypeDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnclosedExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnumConstantDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnumDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ExplicitConstructorInvocationStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ExpressionStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(FieldAccessExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(FieldDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ForeachStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ForStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IfStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ImportDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(InitializerDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(InstanceOfExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IntegerLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IntegerLiteralMinValueExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(JavadocComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LabeledStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LineComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LongLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LongLiteralMinValueExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MarkerAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MemberValuePair n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MethodCallExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MethodDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NameExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NormalAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NullLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ObjectCreationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(PackageDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(Parameter n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(PrimitiveType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(QualifiedNameExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ReferenceType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ReturnStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SingleMemberAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(StringLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SuperExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SwitchEntryStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SwitchStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SynchronizedStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ThisExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ThrowStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TryStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TypeDeclarationStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TypeParameter n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(UnaryExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclarationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclarator n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclaratorId n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VoidType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(WhileStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(WildcardType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
};
cu.accept(visitor, null);
return nodes;
} catch (ParseException e) {
throw new RuntimeException("invalid Java code: " + code + ". " + e);
}
}
use of japa.parser.ast.type.ReferenceType in project japid42 by branaway.
the class JavaSyntaxTool method parseCode.
public static List<CodeNode> parseCode(String code) {
try {
final List<CodeNode> nodes = new LinkedList();
CompilationUnit cu = parse(code);
VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
int nested = 0;
@Override
public void visit(AnnotationDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AnnotationMemberDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayAccessExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayCreationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ArrayInitializerExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AssertStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(AssignExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BinaryExpr n, Object arg) {
if (n.getOperator() == Operator.binOr) {
nodes.add(new CodeNode(nested++, new BinaryOrExpr(n)));
} else {
nodes.add(new CodeNode(nested++, n));
}
super.visit(n, arg);
nested--;
}
@Override
public void visit(BlockComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BlockStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BooleanLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(BreakStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CastExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CatchClause n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CharLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ClassOrInterfaceType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(CompilationUnit n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ConditionalExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ConstructorDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ContinueStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(DoStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(DoubleLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyMemberDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EmptyTypeDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnclosedExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnumConstantDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(EnumDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ExplicitConstructorInvocationStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ExpressionStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(FieldAccessExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(FieldDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ForeachStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ForStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IfStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ImportDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(InitializerDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(InstanceOfExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IntegerLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(IntegerLiteralMinValueExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(JavadocComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LabeledStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LineComment n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LongLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(LongLiteralMinValueExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MarkerAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MemberValuePair n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MethodCallExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(MethodDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NameExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NormalAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(NullLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ObjectCreationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(PackageDeclaration n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(Parameter n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(PrimitiveType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(QualifiedNameExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ReferenceType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ReturnStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SingleMemberAnnotationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(StringLiteralExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SuperExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SwitchEntryStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SwitchStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(SynchronizedStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ThisExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(ThrowStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TryStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TypeDeclarationStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(TypeParameter n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(UnaryExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclarationExpr n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclarator n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VariableDeclaratorId n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(VoidType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(WhileStmt n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
@Override
public void visit(WildcardType n, Object arg) {
nodes.add(new CodeNode(nested++, n));
super.visit(n, arg);
nested--;
}
};
cu.accept(visitor, null);
return nodes;
} catch (ParseException e) {
throw new RuntimeException("invalid Java code: " + code + ". " + e);
}
}
use of japa.parser.ast.type.ReferenceType in project enumerable by hraberg.
the class ExpressionInterpreter method unaryOperation.
public Value unaryOperation(final AbstractInsnNode insn, final Value value) throws AnalyzerException {
ExpressionValue expressionValue = (ExpressionValue) value;
switch(insn.getOpcode()) {
case INEG:
return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
case IINC:
IincInsnNode node = (IincInsnNode) insn;
NameExpr nameExpr = new NameExpr(getLocalVariable(node.var).name);
if (node.incr == 1)
iinc = new UnaryExpr(nameExpr, UnaryExpr.Operator.posIncrement);
if (node.incr == -1)
iinc = new UnaryExpr(nameExpr, UnaryExpr.Operator.posDecrement);
if (node.incr > 1)
iincAssign = new AssignExpr(nameExpr, new IntegerLiteralExpr(node.incr + ""), AssignExpr.Operator.plus);
if (node.incr < -1)
iincAssign = new AssignExpr(nameExpr, new IntegerLiteralExpr(-node.incr + ""), AssignExpr.Operator.minus);
return value;
case L2I:
case F2I:
case D2I:
return new ExpressionValue(PRIMITIVE_INT, new CastExpr(PRIMITIVE_INT, expressionValue.expression));
case I2B:
return new ExpressionValue(PRIMITIVE_BYTE, new CastExpr(PRIMITIVE_BYTE, expressionValue.expression));
case I2C:
return new ExpressionValue(PRIMITIVE_CHAR, new CastExpr(PRIMITIVE_CHAR, expressionValue.expression));
case I2S:
return new ExpressionValue(PRIMITIVE_SHORT, new CastExpr(PRIMITIVE_SHORT, expressionValue.expression));
case FNEG:
return new ExpressionValue(PRIMITIVE_FLOAT, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
case I2F:
case L2F:
case D2F:
return new ExpressionValue(PRIMITIVE_FLOAT, new CastExpr(PRIMITIVE_FLOAT, expressionValue.expression));
case LNEG:
return new ExpressionValue(PRIMITIVE_LONG, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
case I2L:
case F2L:
case D2L:
return new ExpressionValue(PRIMITIVE_LONG, new CastExpr(PRIMITIVE_LONG, expressionValue.expression));
case DNEG:
return new ExpressionValue(PRIMITIVE_DOUBLE, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
case I2D:
case L2D:
case F2D:
return new ExpressionValue(PRIMITIVE_DOUBLE, new CastExpr(PRIMITIVE_DOUBLE, expressionValue.expression));
case IFEQ:
if (conditional != null) {
if (conditional.getCondition() instanceof BinaryExpr && cmpConditional) {
((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.notEquals);
cmpConditional = false;
} else {
handleNestedConditional(expressionValue.expression);
}
} else {
conditional = new ConditionalExpr(expressionValue.expression, null, null);
}
return null;
case IFNE:
if (conditional != null) {
if (conditional.getCondition() instanceof BinaryExpr && cmpConditional) {
((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.equals);
cmpConditional = false;
} else {
handleNestedConditional(new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.not));
}
} else {
conditional = new ConditionalExpr(new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.not), null, null);
}
return null;
case IFGT:
((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.lessEquals);
cmpConditional = false;
return null;
case IFLE:
((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.greater);
cmpConditional = false;
return null;
case IFLT:
((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.greaterEquals);
cmpConditional = false;
return null;
case IFGE:
((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.less);
cmpConditional = false;
return null;
case TABLESWITCH:
case LOOKUPSWITCH:
throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
case IRETURN:
case LRETURN:
case FRETURN:
case DRETURN:
case ARETURN:
return null;
case PUTSTATIC:
FieldInsnNode fieldNode = (FieldInsnNode) insn;
ExpressionValue putField = (ExpressionValue) newValue(getType(fieldNode.desc));
putField.expression = new AssignExpr(new FieldAccessExpr(new NameExpr(removeJavaLang(getObjectType(fieldNode.owner).getClassName())), fieldNode.name), expressionValue.expression, AssignExpr.Operator.assign);
assign = putField;
return null;
case GETFIELD:
fieldNode = (FieldInsnNode) insn;
ExpressionValue getField = (ExpressionValue) newValue(Type.getType(fieldNode.desc));
getField.expression = new FieldAccessExpr(expressionValue.expression, fieldNode.name);
return getField;
case NEWARRAY:
PrimitiveType type;
switch(((IntInsnNode) insn).operand) {
case T_BOOLEAN:
type = PRIMITIVE_BOOLEAN;
break;
case T_CHAR:
type = PRIMITIVE_CHAR;
break;
case T_BYTE:
type = PRIMITIVE_BYTE;
break;
case T_SHORT:
type = PRIMITIVE_SHORT;
break;
case T_INT:
type = PRIMITIVE_INT;
break;
case T_FLOAT:
type = PRIMITIVE_FLOAT;
break;
case T_DOUBLE:
type = PRIMITIVE_DOUBLE;
break;
case T_LONG:
type = PRIMITIVE_LONG;
break;
default:
throw new AnalyzerException(insn, "Invalid array type");
}
ArrayList<Expression> dimensions = new ArrayList<Expression>();
dimensions.add(expressionValue.expression);
return new ExpressionValue(new ReferenceType(type, 1), new ArrayCreationExpr(type, dimensions, 0));
case ANEWARRAY:
ExpressionValue newArray = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
dimensions = new ArrayList<Expression>();
dimensions.add(expressionValue.expression);
newArray.expression = new ArrayCreationExpr(newArray.type, dimensions, 0);
return newArray;
case ARRAYLENGTH:
return new ExpressionValue(PRIMITIVE_INT, new FieldAccessExpr(expressionValue.expression, "length"));
case ATHROW:
throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
case CHECKCAST:
ExpressionValue cast = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
cast.expression = new CastExpr(new ReferenceType(cast.type), expressionValue.expression);
return cast;
case INSTANCEOF:
ExpressionValue instanceOf = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
instanceOf.expression = new InstanceOfExpr(expressionValue.expression, new ReferenceType(instanceOf.type));
return instanceOf;
case MONITORENTER:
case MONITOREXIT:
throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
case IFNULL:
handleNestedConditional(new BinaryExpr(expressionValue.expression, new NullLiteralExpr(), BinaryExpr.Operator.notEquals));
return null;
case IFNONNULL:
handleNestedConditional(new BinaryExpr(expressionValue.expression, new NullLiteralExpr(), BinaryExpr.Operator.equals));
return null;
default:
throw new Error("Internal error.");
}
}
Aggregations