public ASTConstructBodySignature(Construct _construct){ super(JavaEntityType.METHOD, _construct.getId().getSimpleName()); sMethodBodyConverter = sInjector.getInstance(JavaMethodBodyConverter.class); this._construct = _construct; fCompilation = CompilationUtils.compileSource(getSourceCodeWithSnippets(_construct.getContent())); }
/** * 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); } }
public Construct(ConstructId _id, String _content) { if(_id == null || _content==null) throw new IllegalArgumentException("Id and content must be provided"); this.id = _id; this.setContent(_content); } public String getDigest() {
/** * 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 Signature createSignature(Construct _construct) { if(_construct!=null && _construct.getContent()!=null) return new PythonConstructDigest(_construct.getContent(), DigestAlgorithm.SHA1); else return null; }
for (ConstructId key : this.defAnalyzer.getConstructs().keySet()) { if(this.fixAnalyzer!=null && this.fixAnalyzer.containsConstruct(key)) { if(!this.defAnalyzer.getConstruct(key).equals(this.fixAnalyzer.getConstruct(key))) { for (ConstructId key : this.fixAnalyzer.getConstructs().keySet()) { if(this.defAnalyzer!=null && this.defAnalyzer.containsConstruct(key)) { if(!this.fixAnalyzer.getConstruct(key).equals(this.defAnalyzer.getConstruct(key))) {
/** * Returns true if construct id and revision are equal. */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final ConstructChange other = (ConstructChange)obj; final ConstructId tc = (this.def!=null ? com.sap.psr.vulas.ConstructId.toSharedType(this.def.getId()) : com.sap.psr.vulas.ConstructId.toSharedType(this.fix.getId())); final ConstructId oc = (other.def!=null ? com.sap.psr.vulas.ConstructId.toSharedType(other.def.getId()) : com.sap.psr.vulas.ConstructId.toSharedType(other.fix.getId())); return (tc.equals(oc) && this.commit.equals(other.commit)); }
/** * 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, "")); }
final String source_code = jfa.getConstruct(jid).getContent(); response.getWriter().print(source_code); response.flushBuffer();
public ConstructChange(String _repo, String _repo_path, Construct _def, Construct _fix, String _rev, String _time_stamp) { if(_def==null && _fix==null) throw new IllegalArgumentException("At least one construct must be provided (defective, fixed or both)"); this.repo = _repo; this.repoPath = _repo_path; this.def = _def; this.fix = _fix; this.commit = (_rev!=null&&_rev.contains(":"))?_rev.substring(0,_rev.indexOf(":")-1):_rev; this.committedAt = _time_stamp; // Signatures of the defective and fixed constructs (if any, can be null) and the so-called signature change final SignatureFactory factory = CoreConfiguration.getSignatureFactory( com.sap.psr.vulas.ConstructId.toSharedType( (_def!=null ? _def.getId() : _fix.getId()) )); if(factory!=null) { defSignature = factory.createSignature(def); fixSignature = factory.createSignature(fix); signatureChange = factory.computeChange(def, fix); } }
@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; }
final String source_code = jfa.getConstruct(jid).getContent(); response.getWriter().print(source_code); response.flushBuffer();
public Map<ConstructId, Construct> getConstructs(InputStream m) throws FileAnalysisException, IOException, RecognitionException { final ANTLRInputStream input = new ANTLRInputStream(m); final Python3Lexer lexer = new Python3Lexer(input); final CommonTokenStream tokens = new CommonTokenStream(lexer); final Python3Parser parser = new Python3Parser(tokens); final ParseTree root = parser.file_input(); final ParseTreeWalker walker = new ParseTreeWalker(); try { walker.walk(this, root); } catch(IllegalStateException ise) { throw new FileAnalysisException(ise.getMessage(), ise); } // Update module body after the parsing of the entire file if(this.stmts!=null && this.stmts.size()>0) { final StringBuffer b = new StringBuffer(); for(String stmt: this.stmts) if(!stmt.trim().equals("")) b.append(stmt); this.constructs.get(this.module).setContent(b.toString()); } return this.constructs; }
/** * Creates the construct signature on the basis of the source code provided by {@link Construct#getContent()}. * @param _c the construct for which the siganture shall be created * @param _src * @return */ @Override public Signature createSignature(Construct _c) { if(_c==null) return null; ASTConstructBodySignature ast_signature = null; try { ast_signature = new ASTConstructBodySignature(_c); final String construct_name = ((JavaId)_c.getId()).getSimpleName(); //this.extractConstructName(_c.getId()); if(construct_name!=null && !construct_name.equals("")) //!=" ") ast_signature.convertConstructBody(construct_name); } catch(Exception e){ JavaSignatureFactory.log.error(e.getMessage().toString()); } return ast_signature; }
@Override public Signature createSignature(ConstructId _cid, File _file) { if(!_cid.getLang().equals(ProgrammingLanguage.PY)) throw new IllegalArgumentException("Programming language [" + _cid.getLang() + "] not supported"); Signature signature = null; // Compute signature from the entire file if(_cid.getType().equals(ConstructType.MODU)) { signature = new PythonConstructDigest(_file.toPath(), DigestAlgorithm.SHA1); } // For all others, parse the file, and compute the digest for the construct body else { try { final Python3FileAnalyzer fa = (Python3FileAnalyzer)FileAnalyzerFactory.buildFileAnalyzer(_file, new String[] { "py" }); final Construct c = fa.getConstruct(_cid); if(c==null) { throw new IllegalStateException("Construct [" + _cid +"] cannot be found in file [" + _file + "]"); } else { signature = new PythonConstructDigest(c.getContent(), DigestAlgorithm.SHA1); } } catch (IllegalArgumentException e) { log.error(e); } catch (FileAnalysisException e) { log.error(e); } } return signature; }
public Map<ConstructId, Construct> getConstructs(InputStream m) throws FileAnalysisException, IOException, RecognitionException { final ANTLRInputStream input = new ANTLRInputStream(m); final Python335Lexer lexer = new Python335Lexer(input); final CommonTokenStream tokens = new CommonTokenStream(lexer); final Python335Parser parser = new Python335Parser(tokens); final ParseTree root = parser.file_input(); final ParseTreeWalker walker = new ParseTreeWalker(); try { walker.walk(this, root); } catch(IllegalStateException ise) { throw new FileAnalysisException("Parser error", ise); } // Update module body after the parsing of the entire file if(this.stmts!=null && this.stmts.size()>0) { final StringBuffer b = new StringBuffer(); for(String stmt: this.stmts) if(!stmt.trim().equals("")) b.append(stmt); this.constructs.get(this.module).setContent(b.toString()); } return this.constructs; }
public String toJSON() { final StringBuilder b = new StringBuilder(); // Key information b.append("{"); b.append(" \"repo\" : \"").append(this.repo).append("\","); if(this.repoPath!=null) b.append(" \"repoPath\" : \"").append(this.repoPath).append("\","); b.append(" \"constructId\" : ").append(this.getConstruct().getId().toJSON()).append(","); if(this.commit!=null) b.append(" \"commit\" : \"").append(this.commit).append("\","); // Other information b.append(" \"constructChangeType\":\"").append(this.getType().toString()).append("\","); if(this.committedAt!=null) b.append(" \"committedAt\" : \"").append(StringUtil.formatDate(Long.parseLong(this.getCommittedAt()))).append("\""); // Buggy method body, fixed one and applied changes if(this.getType() == ConstructChangeType.MOD ){ if(this.fixSignature != null) //b.append(" \"description\" : ").append(JsonBuilder.escape(this.bugDescription)).append(", "); b.append(",\"fixedBody\":").append(JsonBuilder.escape(this.fixSignature.toJson().toString())); //b.append(getFixConstructASTSignature().toJson().toString()).append(" ,"); if(this.defSignature != null) b.append(",\"buggyBody\":").append(JsonBuilder.escape(this.defSignature.toJson().toString())); //b.append(getDefConstructASTSignature().toJson().toString()).append(" ,"); if(signatureChange != null) //b.append(" \"signatureChange\" : ").append(astDiff.toJSON().toString()).append(" ,"); //b.append("\"sigchg \" :[").append(JsonBuilder.escape(astDiff.toJSON().toString())).append("]").append(" ,"); b.append(",\"bodyChange\":").append(JsonBuilder.escape(signatureChange.toJSON().toString())); //b.append(astDiff.toJSON().toString()).append(" ,"); } b.append("}"); return b.toString(); }