/** * Sets context information in case an {@link InputStream} is parsed using {@link Python3FileAnalyzer#getConstructs(InputStream)}. * In this case, package and module information cannot be obtained from the file and file system. * The method is called by {@link PythonArchiveAnalyzer}. * * @param _module * @param _pack */ public void setContext(PythonId _module, PythonId _pack) { this.constructs = new TreeMap<ConstructId, Construct>(); if(_pack!=null){ this.context.push(_pack); this.constructs.put(_pack, new Construct(_pack, "")); } this.context.push(_module); this.module = _module; this.constructs.put(module, new Construct(module, "")); }
/** * Sets context information in case an {@link InputStream} is parsed using {@link Python3FileAnalyzer#getConstructs(InputStream)}. * In this case, package and module information cannot be obtained from the file and file system. * The method is called by {@link PythonArchiveAnalyzer}. * * @param _module * @param _pack */ public void setContext(PythonId _module, PythonId _pack) { this.constructs = new TreeMap<ConstructId, Construct>(); if(_pack!=null){ this.context.push(_pack); this.constructs.put(_pack, new Construct(_pack, "")); } this.context.push(_module); this.module = _module; this.constructs.put(module, new Construct(module, "")); }
@Override public Map<ConstructId, Construct> getConstructs() throws FileAnalysisException { if(this.constructBodies==null) { this.constructBodies = new TreeMap<ConstructId,Construct>(); for(ConstructId c: this.getConstructIds()) { this.constructBodies.put(c, new Construct(c, "")); } } return this.constructBodies; }
/** * Creates and adds a new {@link Construct} to the set of constructs found in the analyzed file. * This method is called during the various visitor methods inherited from {@link JavaBaseListener}. * @param _id * @param _body */ private void saveConstruct(ConstructId _id, String _body) { try { final Construct c = new Construct(_id, _body); this.constructs.put(_id, c); JavaFileAnalyzer2.log.debug("Added " + c.getId()); } catch (IllegalArgumentException e) { JavaFileAnalyzer2.log.error(e); } }
this.constructs.put(c, new Construct(c, ""));
@Override public void enterClassdef(Python3Parser.ClassdefContext ctx) { // Happens if class name is 'async', due to the Python grammar's problem with the ASYNC keyword, cf. testPythonFileWithAsync if (ctx.NAME()==null) throw new IllegalStateException("Parser error: Class without name in context " + this.context + ", line [" + ctx.getStart().getLine() + "]"); final String name = ctx.NAME().toString(); String parent_classes = ""; if(ctx.arglist()!=null) parent_classes = ctx.arglist().getText().toString(); // Create construct and add to context final PythonId id = new PythonId(this.context.peek(), PythonId.Type.CLASS, name + "(" + parent_classes + ")"); final Construct c = new Construct(id, ctx.getText()); this.constructs.put(id, c); this.context.push(id); }
@Override public void enterClassdef(Python335Parser.ClassdefContext ctx) { // Happens if class name is 'async', due to the Python grammar's problem with the ASYNC keyword, cf. testPythonFileWithAsync if (ctx.NAME()==null) throw new IllegalStateException("Parser error: Class without name in context " + this.context + ", line [" + ctx.getStart().getLine() + "]"); final String name = ctx.NAME().toString(); String parent_classes = ""; if(ctx.arglist()!=null) parent_classes = ctx.arglist().getText().toString(); // Create construct and add to context final PythonId id = new PythonId(this.context.peek(), PythonId.Type.CLASS, name + "(" + parent_classes + ")"); final Construct c = new Construct(id, ctx.getText()); this.constructs.put(id, c); this.context.push(id); }
@Override public void enterFuncdef(Python3Parser.FuncdefContext ctx) { // Happens if method or function name is 'async', due to the Python grammar's problem with the ASYNC keyword, cf. testPythonFileWithAsync if(ctx.NAME()==null) throw new IllegalStateException("Parser error: Construct without name in context " + this.context + ", line [" + ctx.getStart().getLine() + "]"); PythonId id; String name = ctx.NAME().toString(); String args = ctx.parameters().getText(); // Identical construct names can be used within a single context (e.g., the same function in a module) name = this.getNameForCurrentContext(name); // New type depends on context type final PythonId.Type ctx_type = this.context.peek().getType(); if(ctx_type==PythonId.Type.CLASS && name.equals("__init__")) id = new PythonId(this.context.peek(), PythonId.Type.CONSTRUCTOR, name + args); else if(ctx_type==PythonId.Type.CLASS) id = new PythonId(this.context.peek(), PythonId.Type.METHOD, name + args); else if(ctx_type==PythonId.Type.MODULE || ctx_type==PythonId.Type.FUNCTION || ctx_type==PythonId.Type.METHOD || ctx_type==PythonId.Type.CONSTRUCTOR) id = new PythonId(this.context.peek(), PythonId.Type.FUNCTION, name + args); else { log.error("Cannot create method, constructor or class due to wrong context: " + this.context + ", line [" + ctx.getStart().getLine() + "]"); throw new IllegalStateException("Error when parsing [" + this.file + "]: Cannot create method or class due to wrong context: " + this.context); } final Construct c = new Construct(id, ctx.getText()); this.constructs.put(id, c); this.context.push(id); }
@Override public void enterFuncdef(Python335Parser.FuncdefContext ctx) { // Happens if method or function name is 'async', due to the Python grammar's problem with the ASYNC keyword, cf. testPythonFileWithAsync if(ctx.NAME()==null) throw new IllegalStateException("Parser error: Construct without name in context " + this.context + ", line [" + ctx.getStart().getLine() + "]"); PythonId id; String name = ctx.NAME().toString(); String args = ctx.parameters().getText(); // Identical construct names can be used within a single context (e.g., the same function in a module) name = this.getNameForCurrentContext(name); // New type depends on context type final PythonId.Type ctx_type = this.context.peek().getType(); if(ctx_type==PythonId.Type.CLASS && name.equals("__init__")) id = new PythonId(this.context.peek(), PythonId.Type.CONSTRUCTOR, name + args); else if(ctx_type==PythonId.Type.CLASS) id = new PythonId(this.context.peek(), PythonId.Type.METHOD, name + args); else if(ctx_type==PythonId.Type.MODULE || ctx_type==PythonId.Type.FUNCTION || ctx_type==PythonId.Type.METHOD || ctx_type==PythonId.Type.CONSTRUCTOR) id = new PythonId(this.context.peek(), PythonId.Type.FUNCTION, name + args); else { log.error("Cannot create method, constructor or class due to wrong context: " + this.context + ", line [" + ctx.getStart().getLine() + "]"); throw new IllegalStateException("Error when parsing [" + this.file + "]: Cannot create method or class due to wrong context: " + this.context); } final Construct c = new Construct(id, ctx.getText()); this.constructs.put(id, c); this.context.push(id); }