Search in sources :

Example 1 with Node

use of polyglot.ast.Node in project soot by Sable.

the class InitialResolver method isAnonInCCall.

public boolean isAnonInCCall(polyglot.types.ClassType anonType) {
    // System.out.println("checking type: "+anonType);
    Iterator<Node> it = cCallList.iterator();
    while (it.hasNext()) {
        polyglot.ast.ConstructorCall cCall = (polyglot.ast.ConstructorCall) it.next();
        // System.out.println("cCall params: "+cCall.arguments());
        Iterator argsIt = cCall.arguments().iterator();
        while (argsIt.hasNext()) {
            Object next = argsIt.next();
            if (next instanceof polyglot.ast.New && ((polyglot.ast.New) next).anonType() != null) {
                // System.out.println("comparing: "+((polyglot.ast.New)next).anonType());
                if (((polyglot.ast.New) next).anonType().equals(anonType))
                    return true;
            }
        }
    }
    return false;
}
Also used : New(polyglot.ast.New) Node(polyglot.ast.Node) New(polyglot.ast.New) Iterator(java.util.Iterator)

Example 2 with Node

use of polyglot.ast.Node in project soot by Sable.

the class ClassResolver method createSource.

/**
 * Source Creation
 */
protected void createSource(polyglot.ast.SourceFile source) {
    // add absolute path to sourceFileTag
    if (sootClass.hasTag("SourceFileTag")) {
        soot.tagkit.SourceFileTag t = (soot.tagkit.SourceFileTag) sootClass.getTag("SourceFileTag");
        /*
			 * System.out.println("source: "+source);
			 * System.out.println("source.source(): "+source.source());
			 * System.out.println("source path: "+source.source().path());
			 * System.out.println("source name: "+source.source().name());
			 */
        t.setAbsolutePath(source.source().path());
    } else {
        soot.tagkit.SourceFileTag t = new soot.tagkit.SourceFileTag();
        /*
			 * System.out.println("source: "+source);
			 * System.out.println("source.source(): "+source.source());
			 * System.out.println("source path: "+source.source().path());
			 * System.out.println("source name: "+source.source().name());
			 */
        t.setAbsolutePath(source.source().path());
        sootClass.addTag(t);
    }
    String simpleName = sootClass.getName();
    Iterator declsIt = source.decls().iterator();
    boolean found = false;
    // first look in top-level decls
    while (declsIt.hasNext()) {
        Object next = declsIt.next();
        if (next instanceof polyglot.ast.ClassDecl) {
            polyglot.types.ClassType nextType = ((polyglot.ast.ClassDecl) next).type();
            if (Util.getSootType(nextType).equals(sootClass.getType())) {
                createClassDecl((polyglot.ast.ClassDecl) next);
                found = true;
            }
        }
    }
    // if the class wasn't a top level then its nested, local or anon
    if (!found) {
        NestedClassListBuilder nestedClassBuilder = new NestedClassListBuilder();
        source.visit(nestedClassBuilder);
        Iterator<Node> nestedDeclsIt = nestedClassBuilder.getClassDeclsList().iterator();
        while (nestedDeclsIt.hasNext() && !found) {
            polyglot.ast.ClassDecl nextDecl = (polyglot.ast.ClassDecl) nestedDeclsIt.next();
            polyglot.types.ClassType type = nextDecl.type();
            if (type.isLocal() && !type.isAnonymous()) {
                if (InitialResolver.v().getLocalClassMap().containsVal(simpleName)) {
                    createClassDecl(((polyglot.ast.LocalClassDecl) InitialResolver.v().getLocalClassMap().getKey(simpleName)).decl());
                    found = true;
                }
            } else {
                if (Util.getSootType(type).equals(sootClass.getType())) {
                    createClassDecl(nextDecl);
                    found = true;
                }
            }
        }
        if (!found) {
            // 
            if ((InitialResolver.v().getAnonClassMap() != null) && InitialResolver.v().getAnonClassMap().containsVal(simpleName)) {
                polyglot.ast.New aNew = (polyglot.ast.New) InitialResolver.v().getAnonClassMap().getKey(simpleName);
                if (aNew == null)
                    throw new RuntimeException("Could resolve class: " + simpleName);
                createAnonClassDecl(aNew);
                findReferences(aNew.body());
                createClassBody(aNew.body());
                handleFieldInits();
            } else {
            // could be an anon class that was created out of thin air
            // for handling class lits (and asserts) in interfaces
            // this is now done on creation of this special class
            // sootClass.setSuperclass(soot.Scene.v().getSootClass("java.lang.Object"));
            }
        }
    }
}
Also used : Node(polyglot.ast.Node) Iterator(java.util.Iterator)

Example 3 with Node

use of polyglot.ast.Node in project soot by Sable.

the class JavaClassSource method resolve.

public Dependencies resolve(SootClass sc) {
    if (Options.v().verbose())
        logger.debug("resolving [from .java]: " + className);
    IInitialResolver resolver;
    if (Options.v().polyglot())
        resolver = InitialResolver.v();
    else
        resolver = JastAddInitialResolver.v();
    if (fullPath != null) {
        resolver.formAst(fullPath.getPath(), SourceLocator.v().sourcePath(), className);
    }
    // System.out.println("about to call initial resolver in j2j: "+sc.getName());
    Dependencies references = resolver.resolveFromJavaFile(sc);
    /*
         * 1st March 2006
         * Nomair
         * This seems to be a good place to calculate all the
         * AST Metrics needed from Java's AST
         */
    if (Options.v().ast_metrics()) {
        // System.out.println("CALLING COMPUTEASTMETRICS!!!!!!!");
        Node ast = InitialResolver.v().getAst();
        if (ast == null) {
            logger.debug("No compatible AST available for AST metrics. Skipping. Try -polyglot option.");
        } else {
            ComputeASTMetrics metrics = new ComputeASTMetrics(ast);
            metrics.apply();
        }
    }
    return references;
}
Also used : IInitialResolver(soot.javaToJimple.IInitialResolver) ComputeASTMetrics(soot.toolkits.astmetrics.ComputeASTMetrics) Node(polyglot.ast.Node) Dependencies(soot.javaToJimple.IInitialResolver.Dependencies)

Example 4 with Node

use of polyglot.ast.Node in project soot by Sable.

the class ClassResolver method handleClassLiteral.

private void handleClassLiteral(polyglot.ast.ClassBody cBody) {
    // check for class lits whose type is not primitive
    ClassLiteralChecker classLitChecker = new ClassLiteralChecker();
    cBody.visit(classLitChecker);
    ArrayList<Node> classLitList = classLitChecker.getList();
    if (!classLitList.isEmpty()) {
        soot.SootClass addToClass = sootClass;
        if (addToClass.isInterface()) {
            addToClass = getSpecialInterfaceAnonClass(addToClass);
        }
        // add class$ meth
        String methodName = "class$";
        soot.Type methodRetType = soot.RefType.v("java.lang.Class");
        ArrayList paramTypes = new ArrayList();
        paramTypes.add(soot.RefType.v("java.lang.String"));
        soot.SootMethod sootMethod = Scene.v().makeSootMethod(methodName, paramTypes, methodRetType, soot.Modifier.STATIC);
        ClassLiteralMethodSource mSrc = new ClassLiteralMethodSource();
        sootMethod.setSource(mSrc);
        if (!addToClass.declaresMethod(methodName, paramTypes, methodRetType)) {
            addToClass.addMethod(sootMethod);
            sootMethod.addTag(new soot.tagkit.SyntheticTag());
        }
        // add fields for all non prim class lits
        Iterator<Node> classLitIt = classLitList.iterator();
        while (classLitIt.hasNext()) {
            polyglot.ast.ClassLit classLit = (polyglot.ast.ClassLit) classLitIt.next();
            // field
            String fieldName = Util.getFieldNameForClassLit(classLit.typeNode().type());
            soot.Type fieldType = soot.RefType.v("java.lang.Class");
            soot.SootField sootField = Scene.v().makeSootField(fieldName, fieldType, soot.Modifier.STATIC);
            if (!addToClass.declaresField(fieldName, fieldType)) {
                addToClass.addField(sootField);
                sootField.addTag(new soot.tagkit.SyntheticTag());
            }
        }
    }
}
Also used : SootField(soot.SootField) Node(polyglot.ast.Node) ArrayList(java.util.ArrayList) SootClass(soot.SootClass) SootMethod(soot.SootMethod)

Example 5 with Node

use of polyglot.ast.Node in project soot by Sable.

the class ClassResolver method addFinalLocals.

private ArrayList<SootField> addFinalLocals(polyglot.ast.ClassBody cBody, ArrayList<IdentityKey> finalLocalsAvail, polyglot.types.ClassType nodeKeyType, AnonLocalClassInfo info) {
    ArrayList<SootField> finalFields = new ArrayList<SootField>();
    LocalUsesChecker luc = new LocalUsesChecker();
    cBody.visit(luc);
    /* Iterator localsNeededIt = luc.getLocals().iterator(); */
    ArrayList<IdentityKey> localsUsed = new ArrayList<IdentityKey>();
    /*
		 * while (localsNeededIt.hasNext()){ polyglot.types.LocalInstance li =
		 * (polyglot.types.LocalInstance)((polyglot.util.IdentityKey)
		 * localsNeededIt.next()).object(); //if
		 * (luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li))){
		 * //} //else { //} if (finalLocalsAvail.contains(new
		 * polyglot.util.IdentityKey(li)) && !luc.getLocalDecls().contains(new
		 * polyglot.util.IdentityKey(li))){
		 * 
		 * addFinals(li,finalFields);
		 * 
		 * localsUsed.add(new polyglot.util.IdentityKey(li)); } }
		 */
    Iterator<IdentityKey> fieldsNeededIt = finalLocalsAvail.iterator();
    while (fieldsNeededIt.hasNext()) {
        polyglot.types.LocalInstance li = (polyglot.types.LocalInstance) fieldsNeededIt.next().object();
        if (!luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li))) {
            localsUsed.add(new polyglot.util.IdentityKey(li));
            addFinals(li, finalFields);
        }
    }
    // this part is broken it adds all final locals available for the new
    // not just the ones used (which is a problem)
    Iterator<Node> newsIt = luc.getNews().iterator();
    while (newsIt.hasNext()) {
        polyglot.ast.New tempNew = (polyglot.ast.New) newsIt.next();
        polyglot.types.ClassType tempNewType = (polyglot.types.ClassType) tempNew.objectType().type();
        if (InitialResolver.v().finalLocalInfo().containsKey(new polyglot.util.IdentityKey(tempNewType))) {
            AnonLocalClassInfo lInfo = InitialResolver.v().finalLocalInfo().get(new polyglot.util.IdentityKey(tempNewType));
            Iterator<IdentityKey> it = lInfo.finalLocalsAvail().iterator();
            while (it.hasNext()) {
                polyglot.types.LocalInstance li2 = (polyglot.types.LocalInstance) it.next().object();
                if (!sootClass.declaresField("val$" + li2.name(), Util.getSootType(li2.type()))) {
                    if (!luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li2))) {
                        addFinals(li2, finalFields);
                        localsUsed.add(new polyglot.util.IdentityKey(li2));
                    }
                }
            }
        }
    }
    // also need to add them if any super class all the way up needs one
    // because the super() will be made in init and it will require
    // possibly eventually to send in the finals
    polyglot.types.ClassType superType = (polyglot.types.ClassType) nodeKeyType.superType();
    while (!Util.getSootType(superType).equals(soot.Scene.v().getSootClass("java.lang.Object").getType())) {
        if (InitialResolver.v().finalLocalInfo().containsKey(new polyglot.util.IdentityKey(superType))) {
            AnonLocalClassInfo lInfo = InitialResolver.v().finalLocalInfo().get(new polyglot.util.IdentityKey(superType));
            Iterator<IdentityKey> it = lInfo.finalLocalsAvail().iterator();
            while (it.hasNext()) {
                polyglot.types.LocalInstance li2 = (polyglot.types.LocalInstance) it.next().object();
                if (!sootClass.declaresField("val$" + li2.name(), Util.getSootType(li2.type()))) {
                    if (!luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li2))) {
                        addFinals(li2, finalFields);
                        localsUsed.add(new polyglot.util.IdentityKey(li2));
                    }
                }
            }
        }
        superType = (polyglot.types.ClassType) superType.superType();
    }
    info.finalLocalsUsed(localsUsed);
    InitialResolver.v().finalLocalInfo().put(new polyglot.util.IdentityKey(nodeKeyType), info);
    return finalFields;
}
Also used : IdentityKey(polyglot.util.IdentityKey) Node(polyglot.ast.Node) ArrayList(java.util.ArrayList) IdentityKey(polyglot.util.IdentityKey) SootField(soot.SootField)

Aggregations

Node (polyglot.ast.Node)6 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 IdentityKey (polyglot.util.IdentityKey)2 SootField (soot.SootField)2 New (polyglot.ast.New)1 SootClass (soot.SootClass)1 SootMethod (soot.SootMethod)1 IInitialResolver (soot.javaToJimple.IInitialResolver)1 Dependencies (soot.javaToJimple.IInitialResolver.Dependencies)1 ComputeASTMetrics (soot.toolkits.astmetrics.ComputeASTMetrics)1