Search in sources :

Example 1 with ReferenceType

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;
}
Also used : ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) Type(japa.parser.ast.type.Type) ReferenceType(japa.parser.ast.type.ReferenceType) JClass(com.sun.codemodel.JClass) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) ReferenceType(japa.parser.ast.type.ReferenceType)

Example 2 with ReferenceType

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.");
    }
}
Also used : IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) ReferenceType(japa.parser.ast.type.ReferenceType) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) Type(org.objectweb.asm.Type) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) ReferenceType(japa.parser.ast.type.ReferenceType) PrimitiveType(japa.parser.ast.type.PrimitiveType)

Example 3 with ReferenceType

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);
    }
}
Also used : IntegerLiteralExpr(japa.parser.ast.expr.IntegerLiteralExpr) ObjectCreationExpr(japa.parser.ast.expr.ObjectCreationExpr) TypeParameter(japa.parser.ast.TypeParameter) ExplicitConstructorInvocationStmt(japa.parser.ast.stmt.ExplicitConstructorInvocationStmt) LineComment(japa.parser.ast.LineComment) ExpressionStmt(japa.parser.ast.stmt.ExpressionStmt) ReferenceType(japa.parser.ast.type.ReferenceType) VariableDeclarator(japa.parser.ast.body.VariableDeclarator) EnumConstantDeclaration(japa.parser.ast.body.EnumConstantDeclaration) QualifiedNameExpr(japa.parser.ast.expr.QualifiedNameExpr) BreakStmt(japa.parser.ast.stmt.BreakStmt) BooleanLiteralExpr(japa.parser.ast.expr.BooleanLiteralExpr) CastExpr(japa.parser.ast.expr.CastExpr) LongLiteralExpr(japa.parser.ast.expr.LongLiteralExpr) PrimitiveType(japa.parser.ast.type.PrimitiveType) NormalAnnotationExpr(japa.parser.ast.expr.NormalAnnotationExpr) PackageDeclaration(japa.parser.ast.PackageDeclaration) CompilationUnit(japa.parser.ast.CompilationUnit) BlockComment(japa.parser.ast.BlockComment) SwitchStmt(japa.parser.ast.stmt.SwitchStmt) BlockStmt(japa.parser.ast.stmt.BlockStmt) CharLiteralExpr(japa.parser.ast.expr.CharLiteralExpr) CatchClause(japa.parser.ast.stmt.CatchClause) ForeachStmt(japa.parser.ast.stmt.ForeachStmt) InstanceOfExpr(japa.parser.ast.expr.InstanceOfExpr) UnaryExpr(japa.parser.ast.expr.UnaryExpr) LinkedList(java.util.LinkedList) WildcardType(japa.parser.ast.type.WildcardType) EmptyMemberDeclaration(japa.parser.ast.body.EmptyMemberDeclaration) AnnotationDeclaration(japa.parser.ast.body.AnnotationDeclaration) ClassExpr(japa.parser.ast.expr.ClassExpr) AssertStmt(japa.parser.ast.stmt.AssertStmt) ReturnStmt(japa.parser.ast.stmt.ReturnStmt) ThrowStmt(japa.parser.ast.stmt.ThrowStmt) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) LabeledStmt(japa.parser.ast.stmt.LabeledStmt) VoidType(japa.parser.ast.type.VoidType) ClassOrInterfaceDeclaration(japa.parser.ast.body.ClassOrInterfaceDeclaration) QualifiedNameExpr(japa.parser.ast.expr.QualifiedNameExpr) NameExpr(japa.parser.ast.expr.NameExpr) StringLiteralExpr(japa.parser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) SynchronizedStmt(japa.parser.ast.stmt.SynchronizedStmt) FieldDeclaration(japa.parser.ast.body.FieldDeclaration) AssignExpr(japa.parser.ast.expr.AssignExpr) SingleMemberAnnotationExpr(japa.parser.ast.expr.SingleMemberAnnotationExpr) JavadocComment(japa.parser.ast.body.JavadocComment) VariableDeclaratorId(japa.parser.ast.body.VariableDeclaratorId) MemberValuePair(japa.parser.ast.expr.MemberValuePair) ArrayInitializerExpr(japa.parser.ast.expr.ArrayInitializerExpr) WhileStmt(japa.parser.ast.stmt.WhileStmt) ConstructorDeclaration(japa.parser.ast.body.ConstructorDeclaration) EmptyTypeDeclaration(japa.parser.ast.body.EmptyTypeDeclaration) TypeDeclarationStmt(japa.parser.ast.stmt.TypeDeclarationStmt) FieldAccessExpr(japa.parser.ast.expr.FieldAccessExpr) EmptyStmt(japa.parser.ast.stmt.EmptyStmt) ArrayCreationExpr(japa.parser.ast.expr.ArrayCreationExpr) ForStmt(japa.parser.ast.stmt.ForStmt) DoStmt(japa.parser.ast.stmt.DoStmt) ThisExpr(japa.parser.ast.expr.ThisExpr) VariableDeclarationExpr(japa.parser.ast.expr.VariableDeclarationExpr) SuperExpr(japa.parser.ast.expr.SuperExpr) IntegerLiteralMinValueExpr(japa.parser.ast.expr.IntegerLiteralMinValueExpr) ArrayAccessExpr(japa.parser.ast.expr.ArrayAccessExpr) MethodDeclaration(japa.parser.ast.body.MethodDeclaration) BinaryExpr(japa.parser.ast.expr.BinaryExpr) TryStmt(japa.parser.ast.stmt.TryStmt) MarkerAnnotationExpr(japa.parser.ast.expr.MarkerAnnotationExpr) ConditionalExpr(japa.parser.ast.expr.ConditionalExpr) EnumDeclaration(japa.parser.ast.body.EnumDeclaration) ContinueStmt(japa.parser.ast.stmt.ContinueStmt) NullLiteralExpr(japa.parser.ast.expr.NullLiteralExpr) IfStmt(japa.parser.ast.stmt.IfStmt) InitializerDeclaration(japa.parser.ast.body.InitializerDeclaration) LongLiteralMinValueExpr(japa.parser.ast.expr.LongLiteralMinValueExpr) DoubleLiteralExpr(japa.parser.ast.expr.DoubleLiteralExpr) ImportDeclaration(japa.parser.ast.ImportDeclaration) SwitchEntryStmt(japa.parser.ast.stmt.SwitchEntryStmt) Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter) ParseException(japa.parser.ParseException) EnclosedExpr(japa.parser.ast.expr.EnclosedExpr) AnnotationMemberDeclaration(japa.parser.ast.body.AnnotationMemberDeclaration) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 4 with ReferenceType

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);
    }
}
Also used : IntegerLiteralExpr(japa.parser.ast.expr.IntegerLiteralExpr) ObjectCreationExpr(japa.parser.ast.expr.ObjectCreationExpr) TypeParameter(japa.parser.ast.TypeParameter) ExplicitConstructorInvocationStmt(japa.parser.ast.stmt.ExplicitConstructorInvocationStmt) LineComment(japa.parser.ast.LineComment) ExpressionStmt(japa.parser.ast.stmt.ExpressionStmt) ReferenceType(japa.parser.ast.type.ReferenceType) VariableDeclarator(japa.parser.ast.body.VariableDeclarator) EnumConstantDeclaration(japa.parser.ast.body.EnumConstantDeclaration) QualifiedNameExpr(japa.parser.ast.expr.QualifiedNameExpr) BreakStmt(japa.parser.ast.stmt.BreakStmt) BooleanLiteralExpr(japa.parser.ast.expr.BooleanLiteralExpr) CastExpr(japa.parser.ast.expr.CastExpr) LongLiteralExpr(japa.parser.ast.expr.LongLiteralExpr) PrimitiveType(japa.parser.ast.type.PrimitiveType) NormalAnnotationExpr(japa.parser.ast.expr.NormalAnnotationExpr) PackageDeclaration(japa.parser.ast.PackageDeclaration) CompilationUnit(japa.parser.ast.CompilationUnit) BlockComment(japa.parser.ast.BlockComment) SwitchStmt(japa.parser.ast.stmt.SwitchStmt) BlockStmt(japa.parser.ast.stmt.BlockStmt) CharLiteralExpr(japa.parser.ast.expr.CharLiteralExpr) CatchClause(japa.parser.ast.stmt.CatchClause) ForeachStmt(japa.parser.ast.stmt.ForeachStmt) InstanceOfExpr(japa.parser.ast.expr.InstanceOfExpr) UnaryExpr(japa.parser.ast.expr.UnaryExpr) LinkedList(java.util.LinkedList) WildcardType(japa.parser.ast.type.WildcardType) EmptyMemberDeclaration(japa.parser.ast.body.EmptyMemberDeclaration) AnnotationDeclaration(japa.parser.ast.body.AnnotationDeclaration) ClassExpr(japa.parser.ast.expr.ClassExpr) AssertStmt(japa.parser.ast.stmt.AssertStmt) ReturnStmt(japa.parser.ast.stmt.ReturnStmt) ThrowStmt(japa.parser.ast.stmt.ThrowStmt) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) LabeledStmt(japa.parser.ast.stmt.LabeledStmt) VoidType(japa.parser.ast.type.VoidType) ClassOrInterfaceDeclaration(japa.parser.ast.body.ClassOrInterfaceDeclaration) QualifiedNameExpr(japa.parser.ast.expr.QualifiedNameExpr) NameExpr(japa.parser.ast.expr.NameExpr) StringLiteralExpr(japa.parser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) SynchronizedStmt(japa.parser.ast.stmt.SynchronizedStmt) FieldDeclaration(japa.parser.ast.body.FieldDeclaration) AssignExpr(japa.parser.ast.expr.AssignExpr) SingleMemberAnnotationExpr(japa.parser.ast.expr.SingleMemberAnnotationExpr) JavadocComment(japa.parser.ast.body.JavadocComment) VariableDeclaratorId(japa.parser.ast.body.VariableDeclaratorId) MemberValuePair(japa.parser.ast.expr.MemberValuePair) ArrayInitializerExpr(japa.parser.ast.expr.ArrayInitializerExpr) WhileStmt(japa.parser.ast.stmt.WhileStmt) ConstructorDeclaration(japa.parser.ast.body.ConstructorDeclaration) EmptyTypeDeclaration(japa.parser.ast.body.EmptyTypeDeclaration) TypeDeclarationStmt(japa.parser.ast.stmt.TypeDeclarationStmt) FieldAccessExpr(japa.parser.ast.expr.FieldAccessExpr) EmptyStmt(japa.parser.ast.stmt.EmptyStmt) ArrayCreationExpr(japa.parser.ast.expr.ArrayCreationExpr) ForStmt(japa.parser.ast.stmt.ForStmt) DoStmt(japa.parser.ast.stmt.DoStmt) ThisExpr(japa.parser.ast.expr.ThisExpr) VariableDeclarationExpr(japa.parser.ast.expr.VariableDeclarationExpr) SuperExpr(japa.parser.ast.expr.SuperExpr) IntegerLiteralMinValueExpr(japa.parser.ast.expr.IntegerLiteralMinValueExpr) ArrayAccessExpr(japa.parser.ast.expr.ArrayAccessExpr) MethodDeclaration(japa.parser.ast.body.MethodDeclaration) BinaryExpr(japa.parser.ast.expr.BinaryExpr) TryStmt(japa.parser.ast.stmt.TryStmt) MarkerAnnotationExpr(japa.parser.ast.expr.MarkerAnnotationExpr) ConditionalExpr(japa.parser.ast.expr.ConditionalExpr) EnumDeclaration(japa.parser.ast.body.EnumDeclaration) ContinueStmt(japa.parser.ast.stmt.ContinueStmt) NullLiteralExpr(japa.parser.ast.expr.NullLiteralExpr) IfStmt(japa.parser.ast.stmt.IfStmt) InitializerDeclaration(japa.parser.ast.body.InitializerDeclaration) LongLiteralMinValueExpr(japa.parser.ast.expr.LongLiteralMinValueExpr) DoubleLiteralExpr(japa.parser.ast.expr.DoubleLiteralExpr) ImportDeclaration(japa.parser.ast.ImportDeclaration) SwitchEntryStmt(japa.parser.ast.stmt.SwitchEntryStmt) Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter) ParseException(japa.parser.ParseException) EnclosedExpr(japa.parser.ast.expr.EnclosedExpr) AnnotationMemberDeclaration(japa.parser.ast.body.AnnotationMemberDeclaration) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 5 with ReferenceType

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.");
    }
}
Also used : AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) ArrayList(java.util.ArrayList) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) ReferenceType(japa.parser.ast.type.ReferenceType) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) PrimitiveType(japa.parser.ast.type.PrimitiveType)

Aggregations

ReferenceType (japa.parser.ast.type.ReferenceType)5 PrimitiveType (japa.parser.ast.type.PrimitiveType)4 ClassOrInterfaceType (japa.parser.ast.type.ClassOrInterfaceType)3 ParseException (japa.parser.ParseException)2 BlockComment (japa.parser.ast.BlockComment)2 CompilationUnit (japa.parser.ast.CompilationUnit)2 ImportDeclaration (japa.parser.ast.ImportDeclaration)2 LineComment (japa.parser.ast.LineComment)2 PackageDeclaration (japa.parser.ast.PackageDeclaration)2 TypeParameter (japa.parser.ast.TypeParameter)2 AnnotationDeclaration (japa.parser.ast.body.AnnotationDeclaration)2 AnnotationMemberDeclaration (japa.parser.ast.body.AnnotationMemberDeclaration)2 ClassOrInterfaceDeclaration (japa.parser.ast.body.ClassOrInterfaceDeclaration)2 ConstructorDeclaration (japa.parser.ast.body.ConstructorDeclaration)2 EmptyMemberDeclaration (japa.parser.ast.body.EmptyMemberDeclaration)2 EmptyTypeDeclaration (japa.parser.ast.body.EmptyTypeDeclaration)2 EnumConstantDeclaration (japa.parser.ast.body.EnumConstantDeclaration)2 EnumDeclaration (japa.parser.ast.body.EnumDeclaration)2 FieldDeclaration (japa.parser.ast.body.FieldDeclaration)2 InitializerDeclaration (japa.parser.ast.body.InitializerDeclaration)2