├── codeontology ├── codeontology.cmd ├── .gitignore ├── run ├── scripts ├── dbpedia_tag │ ├── query.rq │ ├── tag │ └── comments.py ├── git_download_release │ └── download ├── download_dependencies │ └── download_dependencies └── get_java_repositories │ ├── quick_list.py │ └── list.py ├── .github └── workflows │ └── maven.yml ├── ontology └── catalog-v001.xml ├── src └── main │ └── java │ └── org │ └── codeontology │ ├── extraction │ ├── declaration │ │ ├── NullTypeException.java │ │ ├── EnumEntity.java │ │ ├── ConstructorEntity.java │ │ ├── AnnotationEntity.java │ │ ├── PrimitiveTypeEntity.java │ │ ├── ModifiableTagger.java │ │ ├── LambdaEntity.java │ │ ├── InterfaceEntity.java │ │ ├── ArrayEntity.java │ │ ├── LocalVariableEntity.java │ │ ├── TypeKind.java │ │ └── AnonymousClassEntity.java │ ├── support │ │ ├── FlowBreakerEntity.java │ │ ├── MemberEntity.java │ │ ├── ExpressionHolderEntity.java │ │ ├── ModifiableEntity.java │ │ ├── StatementsHolderEntity.java │ │ ├── BodyHolderEntity.java │ │ ├── TypedElementEntity.java │ │ ├── ConditionHolderEntity.java │ │ ├── VariableDeclarationEntity.java │ │ ├── GenericDeclarationEntity.java │ │ ├── DeclaringElementTagger.java │ │ ├── BodyTagger.java │ │ ├── ConditionTagger.java │ │ ├── JavaTypeTagger.java │ │ ├── TargetedLabelTagger.java │ │ ├── ExpressionTagger.java │ │ ├── LineTagger.java │ │ ├── StatementsTagger.java │ │ ├── VariableDeclarationTagger.java │ │ ├── FormalTypeParametersTagger.java │ │ └── Modifier.java │ ├── statement │ │ ├── DefaultLabelEntity.java │ │ ├── BreakEntity.java │ │ ├── ContinueEntity.java │ │ ├── LoopEntity.java │ │ ├── WhileEntity.java │ │ ├── DoWhileEntity.java │ │ ├── ClassDeclarationStatement.java │ │ ├── ThrowEntity.java │ │ ├── BlockEntity.java │ │ ├── CaseLabelEntity.java │ │ ├── ExpressionStatementEntity.java │ │ ├── ReturnEntity.java │ │ ├── FinallyEntity.java │ │ ├── LocalVariableDeclarationEntity.java │ │ ├── ForEachEntity.java │ │ ├── StatementExpressionListEntity.java │ │ ├── FieldDeclaration.java │ │ ├── SwitchLabelEntity.java │ │ ├── StatementEntity.java │ │ ├── SynchronizedEntity.java │ │ ├── AssertEntity.java │ │ ├── IfThenElseEntity.java │ │ ├── StatementKind.java │ │ ├── ForEntity.java │ │ ├── SwitchEntity.java │ │ └── CatchEntity.java │ ├── project │ │ ├── DefaultProjectEntity.java │ │ ├── GradleProjectEntity.java │ │ ├── MavenProjectEntity.java │ │ └── ProjectVisitor.java │ ├── expression │ │ ├── ClassInstanceCreationExpression.java │ │ ├── ExpressionEntity.java │ │ ├── AssignmentExpressionEntity.java │ │ ├── ActualArgumentEntity.java │ │ ├── MethodInvocationExpressionEntity.java │ │ └── AbstractInvocationExpressionEntity.java │ ├── Entity.java │ ├── SourceProcessor.java │ ├── EntityRegister.java │ ├── JarProcessor.java │ ├── RDFLogger.java │ ├── CodeElementEntity.java │ └── NamedElementEntity.java │ ├── docparser │ ├── ReturnTag.java │ ├── ParamTag.java │ ├── TagFactory.java │ ├── Tag.java │ └── DocCommentParser.java │ └── build │ ├── BuildFiles.java │ ├── DefaultLoader.java │ ├── gradle │ └── AndroidProject.java │ ├── DependenciesLoader.java │ ├── DefaultProject.java │ ├── BuildSystem.java │ ├── ProjectFactory.java │ ├── Project.java │ ├── maven │ └── MavenProject.java │ └── ClasspathLoader.java ├── Dockerfile └── README.md /codeontology: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | java -cp target/codeontology-1.0-SNAPSHOT-jar-with-dependencies.jar -d64 -Xms2G -Xmx4G org.codeontology.CodeOntology $@ 3 | -------------------------------------------------------------------------------- /codeontology.cmd: -------------------------------------------------------------------------------- 1 | set CODEONTOLOGY_LINE_ARGS=%* 2 | 3 | java -cp target/codeontology-1.0-SNAPSHOT-jar-with-dependencies.jar -d64 -Xms2G -Xmx4G org.codeontology.CodeOntology %CODEONTOLOGY_LINE_ARGS% -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Example user template template 3 | ### Example user template 4 | 5 | # IntelliJ project files 6 | .idea 7 | *.iml 8 | target 9 | input 10 | *.nt 11 | 12 | -------------------------------------------------------------------------------- /run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | repositories=$(ls $1) 4 | 5 | if ! [ -e output ]; then 6 | mkdir output 7 | fi 8 | 9 | if ! [ -e triples ]; then 10 | mkdir triples 11 | fi 12 | 13 | for repository in $repositories; do 14 | echo "Running on $repository" 15 | ./codeontology -i $1/$repository -o triples/$repository.nt -fvpestd &> output/$repository.out 16 | done 17 | -------------------------------------------------------------------------------- /scripts/dbpedia_tag/query.rq: -------------------------------------------------------------------------------- 1 | PREFIX rdf: 2 | PREFIX rdfs: 3 | PREFIX owl: 4 | PREFIX xsd: 5 | PREFIX woc: 6 | 7 | SELECT ?s ?com 8 | WHERE { 9 | ?s rdfs:comment ?com 10 | filter(?com != "") 11 | } 12 | -------------------------------------------------------------------------------- /scripts/git_download_release/download: -------------------------------------------------------------------------------- 1 | mkdir repos; 2 | 3 | for repository in `cat repositories`; do 4 | echo "Downloading $repository..." 5 | 6 | cd repos 7 | git clone "$repository" >& /dev/null 8 | name=`echo "$repository" | awk -F "/" '{print $5}'` 9 | cd $name 10 | pwd 11 | git fetch --tags >& /dev/null 12 | git describe --tags >& /dev/null 13 | 14 | if [ $? -eq 0 ]; then 15 | latestTag=$(git describe --tags `git rev-list --tags --max-count=1`); 16 | git checkout $latestTag; 17 | fi 18 | 19 | echo "Done." 20 | cd ../../ 21 | done; 22 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [ push, pull_request ] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v2 11 | - name: Set up JDK 1.8 12 | uses: actions/setup-java@v1 13 | with: 14 | java-version: 1.8 15 | - name: Build with Maven 16 | run: mvn -B package --file pom.xml 17 | - run: mkdir staging && cp target/*.jar staging 18 | - uses: actions/upload-artifact@v2 19 | with: 20 | name: Package 21 | path: staging 22 | -------------------------------------------------------------------------------- /scripts/dbpedia_tag/tag: -------------------------------------------------------------------------------- 1 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 2 | cd $DIR 3 | /opt/fuseki/bin/s-query --service=http://localhost:3030/$1/query --query="query.rq" > comments_URIs 4 | egrep "(^( {8}\"s\").+$)|(^( {8}\"com\").+$)" comments_URIs > comments 5 | rm comments_URIs 6 | sed -i -r s/"(^ {8}\"s.{31})|(\"c.{37})"// comments 7 | sed -i -r s/"^ +"// comments 8 | sed -i -r s/"\" \} ,\n"// comments 9 | sed -r -i s/"\" *\}$"// comments 10 | sed -r -i s/"\" \} ,$"/","/ comments 11 | sed -i ':a;N;$!ba;s/,\n/ /g' comments 12 | python3 comments.py comments 1 13 | cd $DIR/../../ 14 | -------------------------------------------------------------------------------- /ontology/catalog-v001.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/NullTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | public class NullTypeException extends RuntimeException { 23 | public NullTypeException() { 24 | super(); 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/docparser/ReturnTag.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.docparser; 21 | 22 | public class ReturnTag extends Tag { 23 | public static final String TAG = "@return"; 24 | 25 | public ReturnTag(String text) { 26 | super(TAG, text); 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/BuildFiles.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | import java.io.File; 23 | 24 | public class BuildFiles { 25 | public static final File MAVEN_FILE = new File("pom.xml"); 26 | public static final File GRADLE_FILE = new File("build.gradle"); 27 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/FlowBreakerEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | 24 | public interface FlowBreakerEntity extends Entity { 25 | 26 | void tagTargetedLabel(); 27 | 28 | String getTargetedLabel(); 29 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/MemberEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import spoon.reflect.declaration.CtNamedElement; 24 | 25 | public interface MemberEntity extends Entity { 26 | 27 | Entity getDeclaringElement(); 28 | 29 | void tagDeclaringElement(); 30 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/ExpressionHolderEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import org.codeontology.extraction.expression.ExpressionEntity; 24 | 25 | public interface ExpressionHolderEntity extends Entity { 26 | 27 | ExpressionEntity getExpression(); 28 | 29 | void tagExpression(); 30 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/ModifiableEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import spoon.reflect.declaration.CtModifiable; 24 | 25 | import java.util.List; 26 | 27 | public interface ModifiableEntity extends Entity { 28 | 29 | List getModifiers(); 30 | 31 | void tagModifiers(); 32 | 33 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/StatementsHolderEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import org.codeontology.extraction.statement.StatementEntity; 24 | 25 | import java.util.List; 26 | 27 | public interface StatementsHolderEntity extends Entity { 28 | 29 | List> getStatements(); 30 | 31 | void tagStatements(); 32 | 33 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/BodyHolderEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | 23 | import org.codeontology.extraction.Entity; 24 | import org.codeontology.extraction.statement.StatementEntity; 25 | import spoon.reflect.declaration.CtElement; 26 | 27 | public interface BodyHolderEntity extends Entity { 28 | 29 | StatementEntity getBody(); 30 | 31 | void tagBody(); 32 | 33 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/TypedElementEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import org.codeontology.extraction.declaration.TypeEntity; 24 | import spoon.reflect.declaration.CtTypedElement; 25 | 26 | public interface TypedElementEntity extends Entity { 27 | 28 | TypeEntity getJavaType(); 29 | 30 | void tagJavaType(); 31 | 32 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/ConditionHolderEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import org.codeontology.extraction.expression.ExpressionEntity; 24 | import spoon.reflect.declaration.CtElement; 25 | 26 | public interface ConditionHolderEntity extends Entity { 27 | 28 | ExpressionEntity getCondition(); 29 | 30 | void tagCondition(); 31 | 32 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/DefaultLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | public class DefaultLoader extends DependenciesLoader { 23 | 24 | private DefaultProject project; 25 | 26 | public DefaultLoader(DefaultProject project) { 27 | super(project); 28 | this.project = project; 29 | } 30 | 31 | @Override 32 | public void loadDependencies() { 33 | getLoader().loadAllJars(project.getRoot()); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/VariableDeclarationEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import org.codeontology.extraction.expression.ExpressionEntity; 24 | 25 | public interface VariableDeclarationEntity extends Entity { 26 | 27 | ExpressionEntity getInitializer(); 28 | 29 | void tagInitializer(); 30 | 31 | Entity getVariable(); 32 | 33 | void tagVariable(); 34 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/DefaultLabelEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import spoon.reflect.code.CtCase; 25 | 26 | public class DefaultLabelEntity extends SwitchLabelEntity { 27 | public DefaultLabelEntity(CtCase label) { 28 | super(label); 29 | } 30 | 31 | @Override 32 | protected RDFNode getType() { 33 | return Ontology.DEFAULT_ENTITY; 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/GenericDeclarationEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.extraction.Entity; 23 | import org.codeontology.extraction.declaration.TypeVariableEntity; 24 | import spoon.reflect.declaration.CtGenericElement; 25 | 26 | import java.util.List; 27 | 28 | public interface GenericDeclarationEntity extends Entity { 29 | 30 | List getFormalTypeParameters(); 31 | 32 | void tagFormalTypeParameters(); 33 | 34 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/EnumEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import spoon.reflect.reference.CtTypeReference; 25 | 26 | public class EnumEntity> extends ClassEntity { 27 | 28 | public EnumEntity(CtTypeReference reference) { 29 | super(reference); 30 | } 31 | 32 | @Override 33 | protected RDFNode getType() { 34 | return Ontology.ENUM_ENTITY; 35 | } 36 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/project/DefaultProjectEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.project; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.build.DefaultProject; 25 | 26 | public class DefaultProjectEntity extends ProjectEntity { 27 | 28 | public DefaultProjectEntity(DefaultProject project) { 29 | super(project); 30 | } 31 | 32 | @Override 33 | protected RDFNode getType() { 34 | return Ontology.PROJECT_ENTITY; 35 | } 36 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/project/GradleProjectEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.project; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.build.gradle.GradleProject; 25 | 26 | public class GradleProjectEntity extends ProjectEntity { 27 | public GradleProjectEntity(GradleProject project) { 28 | super(project); 29 | } 30 | 31 | @Override 32 | protected RDFNode getType() { 33 | return Ontology.GRADLE_PROJECT_ENTITY; 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/project/MavenProjectEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.project; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.build.maven.MavenProject; 25 | 26 | public class MavenProjectEntity extends ProjectEntity { 27 | 28 | public MavenProjectEntity(MavenProject project) { 29 | super(project); 30 | } 31 | 32 | @Override 33 | protected RDFNode getType() { 34 | return Ontology.MAVEN_PROJECT_ENTITY; 35 | } 36 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/DeclaringElementTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.RDFLogger; 24 | 25 | public class DeclaringElementTagger { 26 | 27 | private MemberEntity member; 28 | 29 | public DeclaringElementTagger(MemberEntity member) { 30 | this.member = member; 31 | } 32 | 33 | public void tagDeclaredBy() { 34 | RDFLogger.getInstance().addTriple(member, Ontology.DECLARED_BY_PROPERTY, member.getDeclaringElement()); 35 | } 36 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/expression/ClassInstanceCreationExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.expression; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import spoon.reflect.code.CtConstructorCall; 25 | 26 | public class ClassInstanceCreationExpression extends AbstractInvocationExpressionEntity> { 27 | public ClassInstanceCreationExpression(CtConstructorCall expression) { 28 | super(expression); 29 | } 30 | 31 | @Override 32 | protected RDFNode getType() { 33 | return Ontology.CLASS_INSTANCE_CREATION_EXPRESSION_ENTITY; 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/docparser/ParamTag.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.docparser; 21 | 22 | public class ParamTag extends Tag { 23 | 24 | private String parameterName; 25 | private String parameterComment; 26 | public static final String TAG = "@param"; 27 | 28 | public ParamTag(String text) { 29 | super(TAG, text); 30 | String[] values = splitText(); 31 | parameterName = values[0]; 32 | parameterComment = values[1]; 33 | } 34 | 35 | public String getParameterName() { 36 | return parameterName; 37 | } 38 | 39 | public String getParameterComment() { 40 | return parameterComment; 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/Entity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction; 21 | 22 | import com.hp.hpl.jena.rdf.model.Model; 23 | import com.hp.hpl.jena.rdf.model.Resource; 24 | 25 | public interface Entity { 26 | 27 | String SEPARATOR = "-"; 28 | 29 | E getElement(); 30 | 31 | void extract(); 32 | 33 | Model getModel(); 34 | 35 | EntityFactory getFactory(); 36 | 37 | RDFLogger getLogger(); 38 | 39 | Entity getParent(); 40 | 41 | Entity getParent(Class... classes); 42 | 43 | void setParent(Entity parent); 44 | 45 | void follow(); 46 | 47 | String getRelativeURI(); 48 | 49 | Resource getResource(); 50 | } -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | 2 | ## NOTE: this Dockerfile has not been tested 3 | 4 | 5 | ###### 6 | #IMPORTANT: 7 | #1. http://askubuntu.com/questions/755853/how-to-install-jdk-sources 8 | # 9 | # sudo apt-get install openjdk-8-source 10 | # # apt-get puts it under the relevant JDK location as src.zip: /usr/lib/jvm/java-8-openjdk-amd64/src.zip 11 | # 12 | # 13 | #2. http://stackoverflow.com/a/8693261 14 | # 15 | ## An exception is for many (if not all) of the com.sun.* classes and others that are only available under the SCSL or the JRL licenses - #which is available through a separate download from Oracle, after accepting one of these licenses. 16 | # 17 | #3. Apparentemente lo zip contiene anche classi com.sun.* ma non funziona con codeontology (crash quasi subito senza messaggio di spiegazione) 18 | # 19 | #### 20 | 21 | FROM ubuntu:16 # check if it works with FROM openjdk:8 22 | # install Oracle JDK 8 23 | RUN apt-get update && \ 24 | apt-get install -y software-properties-common && \ 25 | 26 | RUN git clone https://github.com/codeontology/openjdk8.git 27 | RUN dpkg -iR openjdk8/amd64 28 | RUN apt-get -f install && \ 29 | apt-get install -y maven gradle 30 | 31 | RUN mkdir -p /usr/src/app 32 | WORKDIR /usr/src/app 33 | 34 | 35 | ONBUILD COPY . /usr/src/app 36 | ONBUILD RUN mvn package -DskipTests 37 | 38 | CMD [ "./codeontology" ] 39 | 40 | # TIMES TO EXTRACT TRIPLES on JDK8 41 | #Triples extracted successfully in 2 h 34 min 34 s 321 ms. 42 | #real 156m21.239s 43 | 44 | -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/BodyTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.RDFLogger; 24 | import org.codeontology.extraction.statement.StatementEntity; 25 | 26 | public class BodyTagger { 27 | 28 | BodyHolderEntity entity; 29 | 30 | public BodyTagger(BodyHolderEntity entity) { 31 | this.entity = entity; 32 | } 33 | 34 | public void tagBody() { 35 | StatementEntity body = entity.getBody(); 36 | if (body != null) { 37 | RDFLogger.getInstance().addTriple(entity, Ontology.BODY_PROPERTY, body); 38 | body.extract(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/ConstructorEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import spoon.reflect.declaration.CtConstructor; 25 | import spoon.reflect.reference.CtExecutableReference; 26 | 27 | public class ConstructorEntity extends ExecutableEntity> { 28 | 29 | public ConstructorEntity(CtConstructor constructor) { 30 | super(constructor); 31 | } 32 | 33 | public ConstructorEntity(CtExecutableReference reference) { 34 | super(reference); 35 | } 36 | 37 | @Override 38 | protected RDFNode getType() { 39 | return Ontology.CONSTRUCTOR_ENTITY; 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/docparser/TagFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.docparser; 21 | 22 | 23 | public class TagFactory { 24 | private static TagFactory instance; 25 | 26 | private TagFactory() { 27 | 28 | } 29 | 30 | public static TagFactory getInstance() { 31 | if (instance == null) { 32 | instance = new TagFactory(); 33 | } 34 | 35 | return instance; 36 | } 37 | 38 | public Tag createTag(String name, String text) { 39 | switch (name) { 40 | case ParamTag.TAG: 41 | return new ParamTag(text); 42 | case ReturnTag.TAG: 43 | return new ReturnTag(text); 44 | default: 45 | return new Tag(name, text); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/ConditionTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.RDFLogger; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | 26 | public class ConditionTagger { 27 | 28 | ConditionHolderEntity entity; 29 | 30 | public ConditionTagger(ConditionHolderEntity entity) { 31 | this.entity = entity; 32 | } 33 | 34 | public void tagCondition() { 35 | ExpressionEntity condition = entity.getCondition(); 36 | if (condition != null) { 37 | RDFLogger.getInstance().addTriple(entity, Ontology.CONDITION_PROPERTY, condition); 38 | condition.extract(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/JavaTypeTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.RDFLogger; 24 | import org.codeontology.extraction.declaration.TypeEntity; 25 | 26 | public class JavaTypeTagger { 27 | 28 | private TypedElementEntity typedElement; 29 | 30 | public JavaTypeTagger(TypedElementEntity typedElement) { 31 | this.typedElement = typedElement; 32 | } 33 | 34 | public void tagJavaType() { 35 | TypeEntity type = typedElement.getJavaType(); 36 | if (type != null) { 37 | RDFLogger.getInstance().addTriple(typedElement, Ontology.JAVA_TYPE_PROPERTY, type); 38 | type.follow(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/SourceProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction; 21 | 22 | import org.codeontology.CodeOntology; 23 | import org.codeontology.extraction.declaration.PackageEntity; 24 | import spoon.processing.AbstractProcessor; 25 | import spoon.reflect.declaration.CtPackage; 26 | 27 | public class SourceProcessor extends AbstractProcessor { 28 | @Override 29 | public void process(CtPackage pack) { 30 | ReflectionFactory.getInstance().setParent(pack.getFactory()); 31 | PackageEntity packageEntity = EntityFactory.getInstance().wrap(pack); 32 | if (CodeOntology.extractProjectStructure()) { 33 | packageEntity.setParent(CodeOntology.getProject()); 34 | } 35 | packageEntity.extract(); 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/gradle/AndroidProject.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build.gradle; 21 | 22 | import org.codeontology.extraction.project.ProjectVisitor; 23 | import org.codeontology.build.DependenciesLoader; 24 | 25 | import java.io.File; 26 | 27 | public class AndroidProject extends GradleProject { 28 | 29 | private AndroidLoader loader; 30 | 31 | public AndroidProject(File project) { 32 | super(project); 33 | } 34 | 35 | @Override 36 | public DependenciesLoader getLoader() { 37 | if (loader == null) { 38 | loader = new AndroidLoader(this); 39 | } 40 | return loader; 41 | } 42 | 43 | @Override 44 | public void accept(ProjectVisitor visitor) { 45 | visitor.visit(this); 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/TargetedLabelTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import com.hp.hpl.jena.rdf.model.Literal; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.RDFLogger; 25 | 26 | public class TargetedLabelTagger { 27 | 28 | private FlowBreakerEntity entity; 29 | 30 | public TargetedLabelTagger(FlowBreakerEntity entity) { 31 | this.entity = entity; 32 | } 33 | 34 | public void tagTargetedLabel() { 35 | String labelString = entity.getTargetedLabel(); 36 | if (labelString != null) { 37 | Literal label = entity.getModel().createTypedLiteral(labelString); 38 | RDFLogger.getInstance().addTriple(entity, Ontology.TARGETED_LABEL_PROPERTY, label); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/DependenciesLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | import java.io.File; 23 | import java.util.Set; 24 | 25 | public abstract class DependenciesLoader { 26 | 27 | private T project; 28 | 29 | public DependenciesLoader(T project) { 30 | this.project = project; 31 | } 32 | 33 | private ClasspathLoader loader = ClasspathLoader.getInstance(); 34 | 35 | public abstract void loadDependencies(); 36 | 37 | public Set getJarsLoaded() { 38 | return getLoader().getJarsLoaded(); 39 | } 40 | 41 | public void loadClasspath(String classpath) { 42 | loader.loadClasspath(classpath); 43 | } 44 | 45 | public ClasspathLoader getLoader() { 46 | return loader; 47 | } 48 | 49 | public T getProject() { 50 | return project; 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/AnnotationEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import spoon.reflect.declaration.CtAnnotationType; 25 | import spoon.reflect.reference.CtTypeReference; 26 | 27 | public class AnnotationEntity extends TypeEntity> { 28 | 29 | public AnnotationEntity(CtTypeReference reference) { 30 | super(reference); 31 | } 32 | 33 | @Override 34 | protected RDFNode getType() { 35 | return Ontology.ANNOTATION_ENTITY; 36 | } 37 | 38 | @Override 39 | public void extract() { 40 | tagType(); 41 | tagName(); 42 | tagLabel(); 43 | if (isDeclarationAvailable()) { 44 | tagComment(); 45 | tagSourceCode(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /scripts/download_dependencies/download_dependencies: -------------------------------------------------------------------------------- 1 | maven_build="pom.xml" 2 | gradle_build="gradle.build" 3 | 4 | gradle_test_jars="testJar"; 5 | tast_body="(type: Jar) { classifier = 'tests' from sourceSets.test.output }"; 6 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 7 | echo "dir: $DIR" 8 | i=0 9 | 10 | for repository in `ls $DIR/../../repos/`; do 11 | echo "Downloading for $repository" 12 | echo `pwd` 13 | cd "$DIR/../../repos/$repository" 14 | 15 | if [ `ls -1 | grep $maven_build | wc -l` -ne 0 ]; then 16 | mvn package; 17 | if [ $? -eq 0 ]; then 18 | cd "$DIR/../../" 19 | python3 jpp.py "$DIR/../../repos/$repository" "$repository.nt" nd 20 | fi 21 | 22 | if [ $i -eq 5 ]; then 23 | i=0 24 | rm -rf ~/.m2 25 | mvn package -DskipTests 26 | fi 27 | else 28 | if [ `ls -1 | grep $gradle_build | wc -l` -ne 0 ]; then 29 | gradle dependencies; 30 | if [ $? -eq 0 ]; then 31 | if [ `grep $gradle_test_jar build.gradle | wc -l` -eq 0 ]; then 32 | echo "apply plugin: \'java\' \n $gradle_test_jars task testJar $task_body" >> build.gradle; 33 | fi 34 | python3 jpp.py "$DIR/../../$repository" "$repository.nt" nd 35 | 36 | if [$i -eq 5]; then 37 | i=0 38 | rm -rf ~/.gradle 39 | fi 40 | fi 41 | fi 42 | fi 43 | echo "Done." 44 | (( i=i+1 )) 45 | 46 | cd "$DIR/../../" 47 | done; 48 | -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/PrimitiveTypeEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import spoon.reflect.declaration.CtType; 25 | import spoon.reflect.reference.CtTypeReference; 26 | 27 | public class PrimitiveTypeEntity extends TypeEntity> { 28 | 29 | public PrimitiveTypeEntity(CtTypeReference reference) { 30 | super(reference); 31 | } 32 | 33 | @Override 34 | protected RDFNode getType() { 35 | return Ontology.PRIMITIVE_ENTITY; 36 | } 37 | 38 | @Override 39 | public String buildRelativeURI() { 40 | String uri = super.buildRelativeURI(); 41 | return uri.substring(0, 1).toUpperCase() + uri.substring(1); 42 | } 43 | 44 | @Override 45 | public void extract() { 46 | tagType(); 47 | tagName(); 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/ExpressionTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import com.hp.hpl.jena.rdf.model.Property; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.RDFLogger; 25 | import org.codeontology.extraction.expression.ExpressionEntity; 26 | 27 | public class ExpressionTagger { 28 | 29 | ExpressionHolderEntity entity; 30 | 31 | public ExpressionTagger(ExpressionHolderEntity entity) { 32 | this.entity = entity; 33 | } 34 | 35 | public void tagExpression() { 36 | tagExpression(Ontology.EXPRESSION_PROPERTY); 37 | } 38 | 39 | public void tagExpression(Property property) { 40 | ExpressionEntity expression = entity.getExpression(); 41 | if (expression != null) { 42 | RDFLogger.getInstance().addTriple(entity, property, expression); 43 | expression.extract(); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/DefaultProject.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | import org.codeontology.extraction.project.ProjectVisitor; 23 | 24 | import java.io.File; 25 | import java.util.ArrayList; 26 | import java.util.Collection; 27 | 28 | public class DefaultProject extends Project { 29 | 30 | public DefaultProject(File projectDirectory) { 31 | super(projectDirectory); 32 | if (projectDirectory == null) { 33 | throw new IllegalArgumentException(); 34 | } 35 | } 36 | 37 | @Override 38 | protected Collection findSubProjects() { 39 | return new ArrayList<>(); 40 | } 41 | 42 | @Override 43 | public File getBuildFile() { 44 | return null; 45 | } 46 | 47 | @Override 48 | public DependenciesLoader getLoader() { 49 | return new DefaultLoader(this); 50 | } 51 | 52 | @Override 53 | public void accept(ProjectVisitor visitor) { 54 | visitor.visit(this); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/BuildSystem.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | import org.apache.commons.io.FileUtils; 23 | import org.apache.commons.io.filefilter.FileFilterUtils; 24 | 25 | import java.io.File; 26 | 27 | public enum BuildSystem { 28 | GRADLE, 29 | MAVEN, 30 | UNKNOWN; 31 | 32 | public static BuildSystem getBuildSystem(File project) { 33 | if (!project.isDirectory()) { 34 | return UNKNOWN; 35 | } 36 | 37 | int gradleBuild = FileUtils.listFiles(project, 38 | FileFilterUtils.nameFileFilter(BuildFiles.GRADLE_FILE.getName()), 39 | null).size(); 40 | int mavenBuild = FileUtils.listFiles(project, 41 | FileFilterUtils.nameFileFilter(BuildFiles.MAVEN_FILE.getName()), 42 | null).size(); 43 | 44 | if (mavenBuild != 0) { 45 | return MAVEN; 46 | } 47 | if (gradleBuild != 0) { 48 | return GRADLE; 49 | } 50 | 51 | return UNKNOWN; 52 | } 53 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/EntityRegister.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction; 21 | 22 | import java.util.HashSet; 23 | import java.util.Set; 24 | 25 | public class EntityRegister { 26 | private static EntityRegister instance; 27 | private Set register; 28 | private int size; 29 | private static final int SIZE = 2048; 30 | private static final int LOAD = SIZE / 2; 31 | 32 | private EntityRegister() { 33 | register = new HashSet<>(SIZE); 34 | } 35 | 36 | public static EntityRegister getInstance() { 37 | if (instance == null) { 38 | instance = new EntityRegister(); 39 | } 40 | 41 | return instance; 42 | } 43 | 44 | public boolean add(Entity entity) { 45 | handleSize(); 46 | return register.add(entity.getRelativeURI()); 47 | } 48 | 49 | private void handleSize() { 50 | size++; 51 | if (size > LOAD) { 52 | size = 0; 53 | register = new HashSet<>(SIZE); 54 | } 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/BreakEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.support.FlowBreakerEntity; 25 | import org.codeontology.extraction.support.TargetedLabelTagger; 26 | import spoon.reflect.code.CtBreak; 27 | 28 | public class BreakEntity extends StatementEntity implements FlowBreakerEntity { 29 | 30 | public BreakEntity(CtBreak element) { 31 | super(element); 32 | } 33 | 34 | @Override 35 | protected RDFNode getType() { 36 | return Ontology.BREAK_ENTITY; 37 | } 38 | 39 | @Override 40 | public void extract() { 41 | super.extract(); 42 | tagTargetedLabel(); 43 | } 44 | 45 | public void tagTargetedLabel() { 46 | new TargetedLabelTagger(this).tagTargetedLabel(); 47 | } 48 | 49 | @Override 50 | public String getTargetedLabel() { 51 | return getElement().getTargetLabel(); 52 | } 53 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/ContinueEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.support.FlowBreakerEntity; 25 | import org.codeontology.extraction.support.TargetedLabelTagger; 26 | import spoon.reflect.code.CtContinue; 27 | 28 | public class ContinueEntity extends StatementEntity implements FlowBreakerEntity { 29 | 30 | public ContinueEntity(CtContinue element) { 31 | super(element); 32 | } 33 | 34 | @Override 35 | protected RDFNode getType() { 36 | return Ontology.CONTINUE_ENTITY; 37 | } 38 | 39 | @Override 40 | public void extract() { 41 | super.extract(); 42 | tagTargetedLabel(); 43 | } 44 | 45 | @Override 46 | public void tagTargetedLabel() { 47 | new TargetedLabelTagger(this).tagTargetedLabel(); 48 | } 49 | 50 | @Override 51 | public String getTargetedLabel() { 52 | return getElement().getTargetLabel(); 53 | } 54 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/LoopEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import org.codeontology.extraction.support.BodyHolderEntity; 23 | import org.codeontology.extraction.support.BodyTagger; 24 | import spoon.reflect.code.CtLoop; 25 | import spoon.reflect.code.CtStatement; 26 | 27 | public class LoopEntity extends StatementEntity implements BodyHolderEntity { 28 | 29 | public LoopEntity(T element) { 30 | super(element); 31 | } 32 | 33 | @Override 34 | public void extract() { 35 | super.extract(); 36 | tagBody(); 37 | } 38 | 39 | @Override 40 | public StatementEntity getBody() { 41 | CtStatement statement = getElement().getBody(); 42 | if (statement != null) { 43 | StatementEntity body = getFactory().wrap(statement); 44 | body.setParent(this); 45 | return body; 46 | } 47 | 48 | return null; 49 | } 50 | 51 | @Override 52 | public void tagBody() { 53 | new BodyTagger(this).tagBody(); 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/project/ProjectVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.project; 21 | 22 | import org.codeontology.extraction.EntityFactory; 23 | import org.codeontology.build.DefaultProject; 24 | import org.codeontology.build.gradle.AndroidProject; 25 | import org.codeontology.build.gradle.GradleProject; 26 | import org.codeontology.build.maven.MavenProject; 27 | 28 | public class ProjectVisitor { 29 | 30 | private ProjectEntity lastEntity; 31 | 32 | public void visit(DefaultProject project) { 33 | lastEntity = EntityFactory.getInstance().wrap(project); 34 | } 35 | 36 | public void visit(GradleProject project) { 37 | lastEntity = EntityFactory.getInstance().wrap(project); 38 | } 39 | 40 | public void visit(MavenProject project) { 41 | lastEntity = EntityFactory.getInstance().wrap(project); 42 | } 43 | 44 | public void visit(AndroidProject project) { 45 | lastEntity = EntityFactory.getInstance().wrap(project); 46 | } 47 | 48 | public ProjectEntity getLastEntity() { 49 | return lastEntity; 50 | } 51 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/ModifiableTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.RDFLogger; 24 | import org.codeontology.extraction.support.ModifiableEntity; 25 | import org.codeontology.extraction.support.Modifier; 26 | 27 | import java.util.List; 28 | 29 | public class ModifiableTagger { 30 | 31 | ModifiableEntity modifiable; 32 | 33 | public ModifiableTagger(ModifiableEntity modifiable) { 34 | this.modifiable = modifiable; 35 | } 36 | 37 | public void tagModifiers() { 38 | List modifiers = modifiable.getModifiers(); 39 | 40 | for (Modifier modifier : modifiers) { 41 | /*Property modifierProperty; 42 | if (modifier.isAccessModifier()) { 43 | modifierProperty = Ontology.VISIBILITY_PROPERTY; 44 | } else { 45 | modifierProperty = Ontology.MODIFIER_PROPERTY; 46 | }*/ 47 | 48 | RDFLogger.getInstance().addTriple(modifiable, Ontology.MODIFIER_PROPERTY, modifier.getIndividual()); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /scripts/dbpedia_tag/comments.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import urllib.request 3 | import json 4 | 5 | def tag(data): 6 | URIs = [] 7 | try: 8 | for resource in data["Resources"]: 9 | if float(resource["@similarityScore"]) > 0.085: 10 | URIs.append(resource["@URI"]) 11 | except KeyError: 12 | pass 13 | finally: 14 | return (data["@text"],URIs) 15 | 16 | 17 | def tag_comments(comments_list, start, tries): 18 | res = [] 19 | length = len(comments_list); 20 | 21 | max_tries = 10 22 | 23 | i = 0 24 | 25 | while (i < length): 26 | try: 27 | comment = comments_list[i] 28 | (URI, text) = (comment.split(" ")[0], " ".join(comment.split(" ")[1:])) 29 | print("URI: " + str(URI) + " - text: " + str(text)) 30 | encoded_comment = urllib.parse.urlencode({"text" : text}) 31 | print("requesting http://spotlight.dbpedia.org/rest/annotate?" + encoded_comment + "&confidence=0.2&support=20") 32 | req = urllib.request.Request("http://spotlight.dbpedia.org/rest/annotate?" + encoded_comment 33 | + "&confidence=0.2" 34 | + "&support=20") 35 | req.add_header('Accept', 'application/json') 36 | json_res = urllib.request.urlopen(req).read() 37 | json_data = json_res.decode('utf-8') 38 | data = json.loads(json_data) 39 | (text, URIs)= tag(data) 40 | 41 | res.append(URIs) 42 | 43 | for about_URI in URIs: 44 | open('links.nt', 'a').write("<" + URI + ">" 45 | + " " 46 | + "<" + about_URI + "> ."'\n') 47 | i = i + 1 48 | 49 | except Exception: 50 | print("Error") 51 | if tries < max_tries: 52 | print("Trying again...") 53 | tries = tries + 1 54 | else: 55 | print("Skipping...") 56 | tries = 0 57 | i = i + 1 58 | 59 | 60 | 61 | if __name__ == "__main__": 62 | comments_file = sys.argv[1] 63 | with open(comments_file) as f: 64 | comments = f.readlines() 65 | 66 | tag_comments(comments, sys.argv[2], 0) 67 | -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/LineTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import com.hp.hpl.jena.rdf.model.Literal; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.CodeElementEntity; 25 | import org.codeontology.extraction.RDFLogger; 26 | import spoon.reflect.cu.SourcePosition; 27 | 28 | public class LineTagger { 29 | private CodeElementEntity entity; 30 | private SourcePosition position; 31 | 32 | public LineTagger(CodeElementEntity entity) { 33 | this.entity = entity; 34 | position = entity.getElement().getPosition(); 35 | } 36 | 37 | public void tagLine() { 38 | if (position == null) { 39 | return; 40 | } 41 | Literal line = entity.getModel().createTypedLiteral(position.getLine()); 42 | RDFLogger.getInstance().addTriple(entity, Ontology.LINE_PROPERTY, line); 43 | } 44 | 45 | public void tagEndLine() { 46 | if (position == null) { 47 | return; 48 | } 49 | 50 | Literal endLine = entity.getModel().createTypedLiteral(position.getEndLine()); 51 | RDFLogger.getInstance().addTriple(entity, Ontology.END_LINE_PROPERTY, endLine); 52 | } 53 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/WhileEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ConditionHolderEntity; 26 | import org.codeontology.extraction.support.ConditionTagger; 27 | import spoon.reflect.code.CtWhile; 28 | 29 | public class WhileEntity extends LoopEntity implements ConditionHolderEntity { 30 | 31 | public WhileEntity(CtWhile element) { 32 | super(element); 33 | } 34 | 35 | @Override 36 | protected RDFNode getType() { 37 | return Ontology.WHILE_ENTITY; 38 | } 39 | 40 | @Override 41 | public void extract() { 42 | super.extract(); 43 | tagCondition(); 44 | } 45 | 46 | @Override 47 | public ExpressionEntity getCondition() { 48 | ExpressionEntity condition = getFactory().wrap(getElement().getLoopingExpression()); 49 | condition.setParent(this); 50 | return condition; 51 | } 52 | 53 | @Override 54 | public void tagCondition() { 55 | new ConditionTagger(this).tagCondition(); 56 | } 57 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/DoWhileEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ConditionHolderEntity; 26 | import org.codeontology.extraction.support.ConditionTagger; 27 | import spoon.reflect.code.CtDo; 28 | 29 | public class DoWhileEntity extends LoopEntity implements ConditionHolderEntity { 30 | 31 | public DoWhileEntity(CtDo doStatement) { 32 | super(doStatement); 33 | } 34 | 35 | @Override 36 | protected RDFNode getType() { 37 | return Ontology.DO_WHILE_ENTITY; 38 | } 39 | 40 | @Override 41 | public void extract() { 42 | super.extract(); 43 | tagCondition(); 44 | } 45 | 46 | @Override 47 | public ExpressionEntity getCondition() { 48 | ExpressionEntity condition = getFactory().wrap(getElement().getLoopingExpression()); 49 | condition.setParent(this); 50 | return condition; 51 | } 52 | 53 | @Override 54 | public void tagCondition() { 55 | new ConditionTagger(this).tagCondition(); 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/ProjectFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | import org.codeontology.build.gradle.AndroidProject; 23 | import org.codeontology.build.gradle.GradleProject; 24 | import org.codeontology.build.maven.MavenProject; 25 | 26 | import java.io.File; 27 | 28 | public class ProjectFactory { 29 | private static ProjectFactory instance; 30 | 31 | private ProjectFactory() { 32 | 33 | } 34 | 35 | public static ProjectFactory getInstance() { 36 | if (instance == null) { 37 | instance = new ProjectFactory(); 38 | } 39 | 40 | return instance; 41 | } 42 | 43 | public Project getProject(String path) { 44 | return getProject(new File(path)); 45 | } 46 | 47 | public Project getProject(File project) { 48 | switch (BuildSystem.getBuildSystem(project)) { 49 | case MAVEN: 50 | return new MavenProject(project); 51 | case GRADLE: 52 | if (new File(project.getPath() + "/src/main/AndroidManifest.xml").exists()) { 53 | return new AndroidProject(project); 54 | } else { 55 | return new GradleProject(project); 56 | } 57 | } 58 | 59 | return new DefaultProject(project); 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/docparser/Tag.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.docparser; 21 | 22 | import java.util.Arrays; 23 | 24 | public class Tag { 25 | private String text; 26 | private String name; 27 | 28 | public Tag(String name, String text) { 29 | setName(name); 30 | setText(text); 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public String toString() { 38 | return getName() + " " + getText(); 39 | } 40 | 41 | public String getText() { 42 | return text; 43 | } 44 | 45 | private void setText(String text) { 46 | this.text = text.trim().replaceAll("\\s+", " "); 47 | } 48 | 49 | private void setName(String name) { 50 | this.name = name.trim(); 51 | } 52 | 53 | protected String[] splitText() { 54 | return ensureSize(text.split(" ", 2), 2); 55 | } 56 | 57 | private String[] ensureSize(String[] array, int size) { 58 | String[] result; 59 | 60 | if (array.length >= size) { 61 | result = array; 62 | } else { 63 | result = new String[size]; 64 | Arrays.fill(result, ""); 65 | System.arraycopy(array, 0, result, 0, array.length); 66 | } 67 | 68 | return result; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/LambdaEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.Entity; 25 | import org.codeontology.extraction.NamedElementEntity; 26 | import spoon.reflect.code.CtLambda; 27 | 28 | public class LambdaEntity extends NamedElementEntity> { 29 | 30 | public static final String TAG = "lambda"; 31 | 32 | public LambdaEntity(CtLambda lambda) { 33 | super(lambda); 34 | } 35 | 36 | @Override 37 | public void extract() { 38 | tagType(); 39 | tagSourceCode(); 40 | tagFunctionalImplements(); 41 | } 42 | 43 | private void tagFunctionalImplements() { 44 | Entity implementedType = getFactory().wrap(getElement().getType()); 45 | implementedType.setParent(this.getParent()); 46 | getLogger().addTriple(this, Ontology.IMPLEMENTS_PROPERTY, implementedType); 47 | implementedType.follow(); 48 | } 49 | 50 | @Override 51 | public String buildRelativeURI() { 52 | return getParent().getRelativeURI() + SEPARATOR + TAG + SEPARATOR + getElement().getSimpleName(); 53 | } 54 | 55 | @Override 56 | protected RDFNode getType() { 57 | return Ontology.LAMBDA_ENTITY; 58 | } 59 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/ClassDeclarationStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.declaration.ExecutableEntity; 25 | import org.codeontology.extraction.declaration.TypeEntity; 26 | import spoon.reflect.declaration.CtClass; 27 | import spoon.reflect.declaration.CtType; 28 | 29 | public class ClassDeclarationStatement extends StatementEntity> { 30 | public ClassDeclarationStatement(CtClass element) { 31 | super(element); 32 | } 33 | 34 | @Override 35 | protected RDFNode getType() { 36 | return Ontology.CLASS_DECLARATION_ENTITY; 37 | } 38 | 39 | @Override 40 | public void extract() { 41 | super.extract(); 42 | tagDeclaredClass(); 43 | } 44 | 45 | public TypeEntity getDeclaredClass() { 46 | TypeEntity type = getFactory().wrap((CtType) getElement()); 47 | type.setParent(getParent(ExecutableEntity.class, TypeEntity.class)); 48 | return type; 49 | } 50 | 51 | private void tagDeclaredClass() { 52 | TypeEntity declaredClass = getDeclaredClass(); 53 | getLogger().addTriple(declaredClass, Ontology.DECLARATION_PROPERTY, this); 54 | declaredClass.extract(); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/ThrowEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ExpressionHolderEntity; 26 | import org.codeontology.extraction.support.ExpressionTagger; 27 | import spoon.reflect.code.CtExpression; 28 | import spoon.reflect.code.CtThrow; 29 | 30 | public class ThrowEntity extends StatementEntity implements ExpressionHolderEntity { 31 | public ThrowEntity(CtThrow statement) { 32 | super(statement); 33 | } 34 | 35 | @Override 36 | protected RDFNode getType() { 37 | return Ontology.THROW_ENTITY; 38 | } 39 | 40 | @Override 41 | public void extract() { 42 | super.extract(); 43 | tagExpression(); 44 | } 45 | 46 | @Override 47 | public ExpressionEntity getExpression() { 48 | CtExpression thrownExpression = getElement().getThrownExpression(); 49 | ExpressionEntity expression = getFactory().wrap(thrownExpression); 50 | expression.setParent(this); 51 | return expression; 52 | } 53 | 54 | @Override 55 | public void tagExpression() { 56 | new ExpressionTagger(this).tagExpression(Ontology.THROWN_EXPRESSION_PROPERTY); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/BlockEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.support.LineTagger; 25 | import org.codeontology.extraction.support.StatementsHolderEntity; 26 | import org.codeontology.extraction.support.StatementsTagger; 27 | import spoon.reflect.code.CtBlock; 28 | import spoon.reflect.code.CtStatement; 29 | 30 | import java.util.List; 31 | 32 | public class BlockEntity extends StatementEntity> implements StatementsHolderEntity> { 33 | 34 | public BlockEntity(CtBlock element) { 35 | super(element); 36 | } 37 | 38 | @Override 39 | protected RDFNode getType() { 40 | return Ontology.BLOCK_ENTITY; 41 | } 42 | 43 | @Override 44 | public void extract() { 45 | super.extract(); 46 | tagStatements(); 47 | tagEndLine(); 48 | } 49 | 50 | public void tagEndLine() { 51 | new LineTagger(this).tagEndLine(); 52 | } 53 | 54 | @Override 55 | public List> getStatements() { 56 | List statements = getElement().getStatements(); 57 | return new StatementsTagger(this).asEntities(statements); 58 | } 59 | 60 | @Override 61 | public void tagStatements() { 62 | new StatementsTagger(this).tagStatements(); 63 | } 64 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/CaseLabelEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ExpressionHolderEntity; 26 | import org.codeontology.extraction.support.ExpressionTagger; 27 | import spoon.reflect.code.CtCase; 28 | import spoon.reflect.code.CtExpression; 29 | 30 | public class CaseLabelEntity extends SwitchLabelEntity implements ExpressionHolderEntity> { 31 | 32 | public CaseLabelEntity(CtCase label) { 33 | super(label); 34 | } 35 | 36 | @Override 37 | protected RDFNode getType() { 38 | return Ontology.CASE_ENTITY; 39 | } 40 | 41 | @Override 42 | public void extract() { 43 | super.extract(); 44 | tagExpression(); 45 | } 46 | 47 | @Override 48 | public ExpressionEntity getExpression() { 49 | CtExpression caseExpression = getElement().getCaseExpression(); 50 | if (caseExpression != null) { 51 | ExpressionEntity expression = getFactory().wrap(caseExpression); 52 | expression.setParent(this); 53 | return expression; 54 | } 55 | 56 | return null; 57 | } 58 | 59 | @Override 60 | public void tagExpression() { 61 | new ExpressionTagger(this).tagExpression(); 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/ExpressionStatementEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ExpressionHolderEntity; 26 | import org.codeontology.extraction.support.ExpressionTagger; 27 | import spoon.reflect.code.CtExpression; 28 | import spoon.reflect.code.CtStatement; 29 | 30 | public class ExpressionStatementEntity extends StatementEntity implements ExpressionHolderEntity { 31 | 32 | public ExpressionStatementEntity(CtStatement element) { 33 | super(element); 34 | } 35 | 36 | @Override 37 | protected RDFNode getType() { 38 | return Ontology.EXPRESSION_STATEMENT_ENTITY; 39 | } 40 | 41 | @Override 42 | public void extract() { 43 | super.extract(); 44 | tagExpression(); 45 | } 46 | 47 | @Override 48 | public ExpressionEntity getExpression() { 49 | if (getElement() instanceof CtExpression) { 50 | ExpressionEntity expression = getFactory().wrap((CtExpression) getElement()); 51 | expression.setParent(this); 52 | return expression; 53 | } 54 | 55 | return null; 56 | } 57 | 58 | 59 | @Override 60 | public void tagExpression() { 61 | new ExpressionTagger(this).tagExpression(); 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/ReturnEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ExpressionHolderEntity; 26 | import org.codeontology.extraction.support.ExpressionTagger; 27 | import spoon.reflect.code.CtExpression; 28 | import spoon.reflect.code.CtReturn; 29 | 30 | public class ReturnEntity extends StatementEntity> implements ExpressionHolderEntity> { 31 | 32 | public ReturnEntity(CtReturn element) { 33 | super(element); 34 | } 35 | 36 | @Override 37 | protected RDFNode getType() { 38 | return Ontology.RETURN_ENTITY; 39 | } 40 | 41 | @Override 42 | public void extract() { 43 | super.extract(); 44 | tagExpression(); 45 | } 46 | 47 | @Override 48 | public ExpressionEntity getExpression() { 49 | CtExpression returnedExpression = getElement().getReturnedExpression(); 50 | if (returnedExpression != null) { 51 | ExpressionEntity expression = getFactory().wrap(returnedExpression); 52 | expression.setParent(this); 53 | return expression; 54 | } 55 | 56 | return null; 57 | } 58 | 59 | @Override 60 | public void tagExpression() { 61 | new ExpressionTagger(this).tagExpression(Ontology.RETURNED_EXPRESSION_PROPERTY); 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/FinallyEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.CodeElementEntity; 25 | import org.codeontology.extraction.support.LineTagger; 26 | import org.codeontology.extraction.support.StatementsHolderEntity; 27 | import org.codeontology.extraction.support.StatementsTagger; 28 | import spoon.reflect.code.CtBlock; 29 | 30 | import java.util.List; 31 | 32 | public class FinallyEntity extends CodeElementEntity> implements StatementsHolderEntity> { 33 | 34 | public FinallyEntity(CtBlock block) { 35 | super(block); 36 | } 37 | 38 | @Override 39 | protected RDFNode getType() { 40 | return Ontology.FINALLY_ENTITY; 41 | } 42 | 43 | @Override 44 | public void extract() { 45 | tagType(); 46 | tagStatements(); 47 | tagSourceCode(); 48 | tagLine(); 49 | tagEndLine(); 50 | } 51 | 52 | public void tagLine() { 53 | new LineTagger(this).tagLine(); 54 | } 55 | 56 | @Override 57 | public List> getStatements() { 58 | return new StatementsTagger(this).asEntities(getElement().getStatements()); 59 | } 60 | 61 | @Override 62 | public void tagStatements() { 63 | new StatementsTagger(this).tagStatements(); 64 | } 65 | 66 | public void tagEndLine() { 67 | new LineTagger(this).tagEndLine(); 68 | } 69 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/LocalVariableDeclarationEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.Entity; 25 | import org.codeontology.extraction.expression.ExpressionEntity; 26 | import org.codeontology.extraction.support.VariableDeclarationEntity; 27 | import org.codeontology.extraction.support.VariableDeclarationTagger; 28 | import spoon.reflect.code.CtLocalVariable; 29 | 30 | public class LocalVariableDeclarationEntity extends StatementEntity> 31 | implements VariableDeclarationEntity> { 32 | 33 | public LocalVariableDeclarationEntity(CtLocalVariable element) { 34 | super(element); 35 | } 36 | 37 | @Override 38 | protected RDFNode getType() { 39 | return Ontology.LOCAL_VARIABLE_DECLARATION_ENTITY; 40 | } 41 | 42 | @Override 43 | public void extract() { 44 | super.extract(); 45 | tagVariable(); 46 | tagInitializer(); 47 | } 48 | 49 | public Entity getVariable() { 50 | return VariableDeclarationTagger.declaredVariableOf(this); 51 | } 52 | 53 | @Override 54 | public void tagVariable() { 55 | new VariableDeclarationTagger(this).tagVariable(); 56 | } 57 | 58 | @Override 59 | public ExpressionEntity getInitializer() { 60 | return VariableDeclarationTagger.initializerOf(this); 61 | } 62 | 63 | public void tagInitializer() { 64 | new VariableDeclarationTagger(this).tagInitializer(); 65 | } 66 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/JarProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction; 21 | 22 | import org.codeontology.CodeOntology; 23 | import org.codeontology.build.ClasspathLoader; 24 | 25 | import java.io.File; 26 | import java.io.IOException; 27 | import java.io.OutputStream; 28 | import java.io.PrintStream; 29 | import java.util.jar.JarFile; 30 | 31 | public class JarProcessor { 32 | private JarFile jarFile; 33 | private PrintStream systemErr; 34 | 35 | public JarProcessor(String path) { 36 | try { 37 | if (new File(path).exists()) { 38 | this.jarFile = new JarFile(path); 39 | ClasspathLoader.getInstance().load(path); 40 | systemErr = System.err; 41 | } 42 | } catch (Exception | Error e) { 43 | CodeOntology.showWarning("Could not access file " + path); 44 | } 45 | } 46 | 47 | public JarProcessor(File jar) { 48 | this(jar.getPath()); 49 | } 50 | 51 | public void process() { 52 | try { 53 | try { 54 | hideMessages(); 55 | EntityFactory.getInstance().wrap(jarFile).extract(); 56 | } finally { 57 | System.setErr(systemErr); 58 | } 59 | } catch (Exception | Error e) { 60 | CodeOntology.getInstance().handleFailure(e); 61 | } 62 | } 63 | 64 | private void hideMessages() { 65 | PrintStream tmpErr = new PrintStream(new OutputStream() { 66 | @Override 67 | public void write(int i) throws IOException { 68 | 69 | } 70 | }); 71 | System.setErr(tmpErr); 72 | } 73 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/ForEachEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.declaration.LocalVariableEntity; 25 | import org.codeontology.extraction.expression.ExpressionEntity; 26 | import org.codeontology.extraction.support.ExpressionHolderEntity; 27 | import org.codeontology.extraction.support.ExpressionTagger; 28 | import spoon.reflect.code.CtForEach; 29 | 30 | public class ForEachEntity extends LoopEntity implements ExpressionHolderEntity { 31 | 32 | public ForEachEntity(CtForEach element) { 33 | super(element); 34 | } 35 | 36 | @Override 37 | protected RDFNode getType() { 38 | return Ontology.FOR_EACH_ENTITY; 39 | } 40 | 41 | @Override 42 | public void extract() { 43 | super.extract(); 44 | tagExpression(); 45 | tagVariable(); 46 | } 47 | 48 | public ExpressionEntity getExpression() { 49 | ExpressionEntity expression = getFactory().wrap(getElement().getExpression()); 50 | expression.setParent(this); 51 | return expression; 52 | } 53 | 54 | public void tagExpression() { 55 | new ExpressionTagger(this).tagExpression(); 56 | } 57 | 58 | private LocalVariableEntity getVariable() { 59 | LocalVariableEntity variable = getFactory().wrap(getElement().getVariable()); 60 | variable.setParent(this); 61 | return variable; 62 | } 63 | 64 | public void tagVariable() { 65 | LocalVariableEntity variable = getVariable(); 66 | getLogger().addTriple(this, Ontology.VARIABLE_PROPERTY, variable); 67 | variable.extract(); 68 | } 69 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/StatementExpressionListEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.AbstractEntity; 25 | import org.codeontology.extraction.Entity; 26 | 27 | import java.util.List; 28 | 29 | public class StatementExpressionListEntity extends AbstractEntity>> { 30 | 31 | private int position; 32 | 33 | private static final String TAG = "statement-expression-list"; 34 | 35 | public StatementExpressionListEntity(List> list) { 36 | super(list); 37 | for (Entity current : list) { 38 | current.setParent(this); 39 | } 40 | } 41 | 42 | @Override 43 | protected String buildRelativeURI() { 44 | return getParent().getRelativeURI() + SEPARATOR + TAG + SEPARATOR + position; 45 | } 46 | 47 | @Override 48 | protected RDFNode getType() { 49 | return Ontology.STATEMENT_EXPRESSION_LIST_ENTITY; 50 | } 51 | 52 | @Override 53 | public void extract() { 54 | tagType(); 55 | tagSourceCode(); 56 | } 57 | 58 | @Override 59 | public String getSourceCode() { 60 | List> list = getElement(); 61 | int size = list.size(); 62 | StringBuilder builder = new StringBuilder(); 63 | 64 | if (size > 0) { 65 | builder.append(list.get(0).getElement()); 66 | } 67 | 68 | for (int i = 1; i < size; i++) { 69 | builder.append(", "); 70 | builder.append(list.get(i).getElement()); 71 | } 72 | 73 | return builder.toString(); 74 | } 75 | 76 | public void setPosition(int position) { 77 | this.position = position; 78 | } 79 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/FieldDeclaration.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.CodeElementEntity; 25 | import org.codeontology.extraction.Entity; 26 | import org.codeontology.extraction.expression.ExpressionEntity; 27 | import org.codeontology.extraction.support.LineTagger; 28 | import org.codeontology.extraction.support.VariableDeclarationEntity; 29 | import org.codeontology.extraction.support.VariableDeclarationTagger; 30 | import spoon.reflect.declaration.CtField; 31 | 32 | public class FieldDeclaration extends CodeElementEntity> implements VariableDeclarationEntity> { 33 | 34 | public FieldDeclaration(CtField element) { 35 | super(element); 36 | } 37 | 38 | @Override 39 | protected RDFNode getType() { 40 | return null; 41 | } 42 | 43 | @Override 44 | public void extract() { 45 | tagType(); 46 | tagLine(); 47 | tagVariable(); 48 | tagInitializer(); 49 | tagSourceCode(); 50 | } 51 | 52 | @Override 53 | public ExpressionEntity getInitializer() { 54 | return VariableDeclarationTagger.initializerOf(this); 55 | } 56 | 57 | @Override 58 | public void tagInitializer() { 59 | new VariableDeclarationTagger(this).tagInitializer(); 60 | } 61 | 62 | @Override 63 | public Entity getVariable() { 64 | return VariableDeclarationTagger.declaredVariableOf(this); 65 | } 66 | 67 | @Override 68 | public void tagVariable() { 69 | getLogger().addTriple(getParent(), Ontology.DECLARATION_PROPERTY, this); 70 | } 71 | 72 | public void tagLine() { 73 | new LineTagger(this).tagLine(); 74 | } 75 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/expression/ExpressionEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.expression; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.CodeElementEntity; 25 | import org.codeontology.extraction.declaration.TypeEntity; 26 | import org.codeontology.extraction.support.GenericDeclarationEntity; 27 | import org.codeontology.extraction.support.JavaTypeTagger; 28 | import org.codeontology.extraction.support.LineTagger; 29 | import org.codeontology.extraction.support.TypedElementEntity; 30 | import spoon.reflect.code.CtExpression; 31 | 32 | public class ExpressionEntity> extends CodeElementEntity 33 | implements TypedElementEntity { 34 | 35 | public ExpressionEntity(E expression) { 36 | super(expression); 37 | } 38 | 39 | @Override 40 | protected RDFNode getType() { 41 | return Ontology.EXPRESSION_ENTITY; 42 | } 43 | 44 | @Override 45 | public String buildRelativeURI() { 46 | return super.buildRelativeURI("expression"); 47 | } 48 | 49 | @Override 50 | public void extract() { 51 | tagType(); 52 | tagJavaType(); 53 | tagSourceCode(); 54 | tagLine(); 55 | } 56 | 57 | public void tagLine() { 58 | new LineTagger(this).tagLine(); 59 | } 60 | 61 | @Override 62 | public TypeEntity getJavaType() { 63 | TypeEntity type = getFactory().wrap(getElement().getType()); 64 | if (type != null) { 65 | type.setParent(getParent(GenericDeclarationEntity.class)); 66 | return type; 67 | } 68 | 69 | return null; 70 | } 71 | 72 | @Override 73 | public void tagJavaType() { 74 | new JavaTypeTagger(this).tagJavaType(); 75 | } 76 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/expression/AssignmentExpressionEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.expression; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.support.ExpressionHolderEntity; 25 | import org.codeontology.extraction.support.ExpressionTagger; 26 | import spoon.reflect.code.CtAssignment; 27 | 28 | public class AssignmentExpressionEntity extends ExpressionEntity> 29 | implements ExpressionHolderEntity> { 30 | 31 | public AssignmentExpressionEntity(CtAssignment expression) { 32 | super(expression); 33 | } 34 | 35 | @Override 36 | public void extract() { 37 | super.extract(); 38 | tagLeftHandSideExpression(); 39 | tagExpression(); 40 | } 41 | 42 | public void tagLeftHandSideExpression() { 43 | ExpressionEntity expression = getLeftHandSideExpression(); 44 | getLogger().addTriple(this, Ontology.LEFT_HAND_SIDE_PROPERTY, expression); 45 | expression.extract(); 46 | } 47 | 48 | public ExpressionEntity getLeftHandSideExpression() { 49 | ExpressionEntity leftHandExpression = getFactory().wrap(getElement().getAssigned()); 50 | leftHandExpression.setParent(this); 51 | return leftHandExpression; 52 | } 53 | 54 | @Override 55 | protected RDFNode getType() { 56 | return Ontology.ASSIGNMENT_EXPRESSION_ENTITY; 57 | } 58 | 59 | @Override 60 | public ExpressionEntity getExpression() { 61 | ExpressionEntity expression = getFactory().wrap(getElement().getAssignment()); 62 | expression.setParent(this); 63 | return expression; 64 | } 65 | 66 | @Override 67 | public void tagExpression() { 68 | new ExpressionTagger(this).tagExpression(); 69 | } 70 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/expression/ActualArgumentEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.expression; 21 | 22 | import com.hp.hpl.jena.rdf.model.Literal; 23 | import com.hp.hpl.jena.rdf.model.RDFNode; 24 | import org.codeontology.Ontology; 25 | import org.codeontology.extraction.AbstractEntity; 26 | import org.codeontology.extraction.support.ExpressionHolderEntity; 27 | import org.codeontology.extraction.support.ExpressionTagger; 28 | 29 | public class ActualArgumentEntity extends AbstractEntity> 30 | implements ExpressionHolderEntity> { 31 | 32 | private int position; 33 | private static final String TAG = "argument"; 34 | 35 | public ActualArgumentEntity(ExpressionEntity expression) { 36 | super(expression); 37 | } 38 | 39 | @Override 40 | public String buildRelativeURI() { 41 | return getElement().getRelativeURI() + SEPARATOR + TAG + SEPARATOR + position; 42 | } 43 | 44 | @Override 45 | public void extract() { 46 | tagType(); 47 | tagPosition(); 48 | tagExpression(); 49 | } 50 | 51 | @Override 52 | protected RDFNode getType() { 53 | return Ontology.ACTUAL_ARGUMENT_ENTITY; 54 | } 55 | 56 | @Override 57 | public ExpressionEntity getExpression() { 58 | return getElement(); 59 | } 60 | 61 | @Override 62 | public void tagExpression() { 63 | new ExpressionTagger(this).tagExpression(); 64 | } 65 | 66 | public void tagPosition() { 67 | Literal position = getModel().createTypedLiteral(getPosition()); 68 | getLogger().addTriple(this, Ontology.POSITION_PROPERTY, position); 69 | } 70 | 71 | public int getPosition() { 72 | return position; 73 | } 74 | 75 | public void setPosition(int position) { 76 | this.position = position; 77 | } 78 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/InterfaceEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.CodeOntology; 24 | import org.codeontology.Ontology; 25 | import org.codeontology.extraction.support.FormalTypeParametersTagger; 26 | import org.codeontology.extraction.support.GenericDeclarationEntity; 27 | import spoon.reflect.declaration.CtInterface; 28 | import spoon.reflect.reference.CtTypeReference; 29 | 30 | import java.util.List; 31 | 32 | public class InterfaceEntity extends TypeEntity> implements GenericDeclarationEntity> { 33 | 34 | public InterfaceEntity(CtTypeReference reference) { 35 | super(reference); 36 | } 37 | 38 | @Override 39 | protected RDFNode getType() { 40 | return Ontology.INTERFACE_ENTITY; 41 | } 42 | 43 | @Override 44 | public void extract() { 45 | tagType(); 46 | tagName(); 47 | tagLabel(); 48 | tagSuperInterfaces(); 49 | tagModifiers(); 50 | if (isDeclarationAvailable() || CodeOntology.isJarExplorationEnabled()) { 51 | tagFields(); 52 | tagMethods(); 53 | } 54 | if (isDeclarationAvailable()) { 55 | tagAnnotations(); 56 | tagSourceCode(); 57 | tagComment(); 58 | tagFormalTypeParameters(); 59 | } 60 | } 61 | 62 | public void tagSuperInterfaces() { 63 | tagSuperInterfaces(Ontology.EXTENDS_PROPERTY); 64 | } 65 | 66 | @Override 67 | public List getFormalTypeParameters() { 68 | return FormalTypeParametersTagger.formalTypeParametersOf(this); 69 | } 70 | 71 | @Override 72 | public void tagFormalTypeParameters() { 73 | new FormalTypeParametersTagger(this).tagFormalTypeParameters(); 74 | } 75 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/SwitchLabelEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.CodeElementEntity; 24 | import org.codeontology.extraction.support.LineTagger; 25 | import org.codeontology.extraction.support.StatementsHolderEntity; 26 | import org.codeontology.extraction.support.StatementsTagger; 27 | import spoon.reflect.code.CtCase; 28 | import spoon.reflect.code.CtStatement; 29 | 30 | import java.util.List; 31 | 32 | public abstract class SwitchLabelEntity extends CodeElementEntity> 33 | implements StatementsHolderEntity> { 34 | 35 | private SwitchLabelEntity next; 36 | 37 | public SwitchLabelEntity(CtCase label) { 38 | super(label); 39 | } 40 | 41 | @Override 42 | public void extract() { 43 | tagType(); 44 | tagStatements(); 45 | tagLine(); 46 | tagEndLine(); 47 | tagNext(); 48 | } 49 | 50 | public void tagNext() { 51 | if (getNext() != null) { 52 | getLogger().addTriple(this, Ontology.NEXT_PROPERTY, next); 53 | } 54 | } 55 | 56 | @Override 57 | public List> getStatements() { 58 | List statements = getElement().getStatements(); 59 | return new StatementsTagger(this).asEntities(statements); 60 | } 61 | 62 | @Override 63 | public void tagStatements() { 64 | new StatementsTagger(this).tagStatements(); 65 | } 66 | 67 | public void tagLine() { 68 | new LineTagger(this).tagLine(); 69 | } 70 | 71 | public void tagEndLine() { 72 | new LineTagger(this).tagEndLine(); 73 | } 74 | 75 | public SwitchLabelEntity getNext() { 76 | return next; 77 | } 78 | 79 | public void setNext(SwitchLabelEntity next) { 80 | this.next = next; 81 | } 82 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/StatementEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.Literal; 23 | import com.hp.hpl.jena.rdf.model.RDFNode; 24 | import org.codeontology.Ontology; 25 | import org.codeontology.extraction.CodeElementEntity; 26 | import org.codeontology.extraction.RDFLogger; 27 | import org.codeontology.extraction.support.LineTagger; 28 | import spoon.reflect.code.CtStatement; 29 | 30 | public class StatementEntity extends CodeElementEntity { 31 | private int position; 32 | 33 | public StatementEntity(E element) { 34 | super(element); 35 | } 36 | 37 | @Override 38 | public String buildRelativeURI() { 39 | return super.buildRelativeURI("statement"); 40 | } 41 | 42 | public void setPosition(int position) { 43 | this.position = position; 44 | } 45 | 46 | public int getPosition() { 47 | return position; 48 | } 49 | 50 | @Override 51 | protected RDFNode getType() { 52 | return Ontology.STATEMENT_ENTITY; 53 | } 54 | 55 | @Override 56 | public void extract() { 57 | tagType(); 58 | tagPosition(); 59 | tagLine(); 60 | tagSourceCode(); 61 | tagLabel(); 62 | } 63 | 64 | public void tagLabel() { 65 | String labelString = getElement().getLabel(); 66 | if (labelString != null) { 67 | Literal label = getModel().createTypedLiteral(labelString); 68 | getLogger().addTriple(this, Ontology.WOC_LABEL_PROPERTY, label); 69 | } 70 | } 71 | 72 | public void tagLine() { 73 | new LineTagger(this).tagLine(); 74 | } 75 | 76 | public void tagPosition() { 77 | Literal position = getModel().createTypedLiteral(getPosition()); 78 | RDFLogger.getInstance().addTriple(this, Ontology.POSITION_PROPERTY, position); 79 | } 80 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/SynchronizedEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.BodyHolderEntity; 26 | import org.codeontology.extraction.support.BodyTagger; 27 | import org.codeontology.extraction.support.ExpressionHolderEntity; 28 | import org.codeontology.extraction.support.ExpressionTagger; 29 | import spoon.reflect.code.CtExpression; 30 | import spoon.reflect.code.CtSynchronized; 31 | 32 | public class SynchronizedEntity extends StatementEntity 33 | implements BodyHolderEntity, ExpressionHolderEntity { 34 | 35 | public SynchronizedEntity(CtSynchronized element) { 36 | super(element); 37 | } 38 | 39 | @Override 40 | protected RDFNode getType() { 41 | return Ontology.SYNCHRONIZED_ENTITY; 42 | } 43 | 44 | @Override 45 | public void extract() { 46 | super.extract(); 47 | tagBody(); 48 | tagExpression(); 49 | } 50 | 51 | @Override 52 | public StatementEntity getBody() { 53 | StatementEntity body = getFactory().wrap(getElement().getBlock()); 54 | body.setParent(this); 55 | return body; 56 | } 57 | 58 | @Override 59 | public void tagBody() { 60 | new BodyTagger(this).tagBody(); 61 | } 62 | 63 | @Override 64 | public ExpressionEntity getExpression() { 65 | CtExpression expression = getElement().getExpression(); 66 | if (expression != null) { 67 | ExpressionEntity entity = getFactory().wrap(expression); 68 | entity.setParent(this); 69 | return entity; 70 | } 71 | 72 | return null; 73 | } 74 | 75 | @Override 76 | public void tagExpression() { 77 | new ExpressionTagger(this).tagExpression(); 78 | } 79 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/AssertEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ExpressionHolderEntity; 26 | import org.codeontology.extraction.support.ExpressionTagger; 27 | import spoon.reflect.code.CtAssert; 28 | import spoon.reflect.code.CtExpression; 29 | 30 | public class AssertEntity extends StatementEntity> implements ExpressionHolderEntity> { 31 | 32 | public AssertEntity(CtAssert element) { 33 | super(element); 34 | } 35 | 36 | @Override 37 | protected RDFNode getType() { 38 | return Ontology.ASSERT_ENTITY; 39 | } 40 | 41 | @Override 42 | public void extract() { 43 | super.extract(); 44 | tagExpression(); 45 | tagAssertExpression(); 46 | } 47 | 48 | public ExpressionEntity getAssertExpression() { 49 | CtExpression expression = getElement().getAssertExpression(); 50 | ExpressionEntity entity = getFactory().wrap(expression); 51 | entity.setParent(this); 52 | return entity; 53 | } 54 | 55 | public void tagAssertExpression() { 56 | ExpressionEntity assertExpression = getAssertExpression(); 57 | getLogger().addTriple(this, Ontology.ASSERT_EXPRESSION_PROPERTY, assertExpression); 58 | assertExpression.extract(); 59 | } 60 | 61 | @Override 62 | public ExpressionEntity getExpression() { 63 | CtExpression expression = getElement().getExpression(); 64 | if (expression != null) { 65 | ExpressionEntity entity = getFactory().wrap(expression); 66 | entity.setParent(this); 67 | return entity; 68 | } 69 | 70 | return null; 71 | } 72 | 73 | @Override 74 | public void tagExpression() { 75 | new ExpressionTagger(this).tagExpression(); 76 | } 77 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/RDFLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction; 21 | 22 | import com.hp.hpl.jena.rdf.model.*; 23 | import org.codeontology.Ontology; 24 | 25 | import java.io.BufferedWriter; 26 | import java.io.FileWriter; 27 | import java.io.IOException; 28 | import java.io.PrintWriter; 29 | 30 | public class RDFLogger { 31 | private Model model; 32 | private String outputFile; 33 | private int counter; 34 | private static RDFLogger instance; 35 | 36 | public static final int MAX_SIZE = 10000; 37 | 38 | private RDFLogger() { 39 | model = Ontology.getModel(); 40 | outputFile = "triples.nt"; 41 | counter = 0; 42 | } 43 | 44 | public static RDFLogger getInstance() { 45 | if (instance == null) { 46 | instance = new RDFLogger(); 47 | } 48 | return instance; 49 | } 50 | 51 | public Model getModel() { 52 | return model; 53 | } 54 | 55 | public void setOutputFile(String path) { 56 | outputFile = path; 57 | } 58 | 59 | public void writeRDF() { 60 | try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)))) { 61 | model.write(writer, "N-TRIPLE"); 62 | } catch (IOException e) { 63 | System.out.println("Cannot write triples."); 64 | System.exit(-1); 65 | } 66 | } 67 | 68 | public void addTriple(Entity subject, Property property, Entity object) { 69 | addTriple(subject, property, object.getResource()); 70 | } 71 | 72 | public void addTriple(Entity subject, Property property, RDFNode object) { 73 | if (property != null && object != null) { 74 | Statement triple = model.createStatement(subject.getResource(), property, object); 75 | model.add(triple); 76 | counter++; 77 | if (counter > MAX_SIZE) { 78 | writeRDF(); 79 | free(); 80 | } 81 | } 82 | } 83 | 84 | private void free() { 85 | model = ModelFactory.createDefaultModel(); 86 | counter = 0; 87 | } 88 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/ArrayEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.Entity; 25 | import spoon.reflect.declaration.CtType; 26 | import spoon.reflect.reference.CtArrayTypeReference; 27 | import spoon.reflect.reference.CtTypeReference; 28 | 29 | public class ArrayEntity extends TypeEntity> { 30 | private TypeEntity componentType; 31 | 32 | public ArrayEntity(CtTypeReference reference) { 33 | super(reference); 34 | CtTypeReference componentReference = ((CtArrayTypeReference) getReference()).getArrayType(); 35 | componentType = getFactory().wrap(componentReference); 36 | } 37 | 38 | @Override 39 | public void extract() { 40 | tagType(); 41 | tagName(); 42 | tagLabel(); 43 | tagArrayOf(); 44 | tagDimensions(); 45 | } 46 | 47 | @Override 48 | protected RDFNode getType() { 49 | return Ontology.ARRAY_ENTITY; 50 | } 51 | 52 | @Override 53 | public String buildRelativeURI() { 54 | return componentType.getRelativeURI() + "[]"; 55 | } 56 | 57 | public void tagArrayOf() { 58 | getLogger().addTriple(this, Ontology.ARRAY_OF_PROPERTY, componentType); 59 | componentType.follow(); 60 | } 61 | 62 | public void tagDimensions() { 63 | int dimensions = ((CtArrayTypeReference) getReference()).getDimensionCount(); 64 | getLogger().addTriple(this, Ontology.DIMENSIONS_PROPERTY, getModel().createTypedLiteral(dimensions)); 65 | } 66 | 67 | @Override 68 | public void setParent(Entity parent) { 69 | super.setParent(parent); 70 | componentType.setParent(parent); 71 | } 72 | 73 | @Override 74 | public String getName() { 75 | String componenTypeName = componentType.buildRelativeURI(); 76 | if (componentType instanceof PrimitiveTypeEntity) { 77 | componenTypeName = componenTypeName.toLowerCase(); 78 | } 79 | return componenTypeName + "[]"; 80 | } 81 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/StatementsTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.EntityFactory; 24 | import org.codeontology.extraction.RDFLogger; 25 | import org.codeontology.extraction.statement.StatementEntity; 26 | import spoon.reflect.code.CtStatement; 27 | 28 | import java.util.ArrayList; 29 | import java.util.Iterator; 30 | import java.util.List; 31 | 32 | public class StatementsTagger { 33 | 34 | private final StatementsHolderEntity entity; 35 | 36 | public StatementsTagger(StatementsHolderEntity entity) { 37 | this.entity = entity; 38 | } 39 | 40 | public void tagStatements() { 41 | List> statements = entity.getStatements(); 42 | Iterator> iterator = statements.listIterator(); 43 | 44 | if (iterator.hasNext()) { 45 | StatementEntity previous = iterator.next(); 46 | tagStatement(previous); 47 | while (iterator.hasNext()) { 48 | StatementEntity current = iterator.next(); 49 | RDFLogger.getInstance().addTriple(previous, Ontology.NEXT_PROPERTY, current); 50 | previous = current; 51 | tagStatement(current); 52 | } 53 | } 54 | } 55 | 56 | private void tagStatement(StatementEntity statement) { 57 | RDFLogger.getInstance().addTriple(entity, Ontology.STATEMENT_PROPERTY, statement); 58 | statement.extract(); 59 | } 60 | 61 | public List> asEntities(List statements) { 62 | List> result = new ArrayList<>(); 63 | 64 | if (statements == null) { 65 | return result; 66 | } 67 | 68 | int size = statements.size(); 69 | 70 | for (int i = 0; i < size; i++) { 71 | StatementEntity statement = EntityFactory.getInstance().wrap(statements.get(i)); 72 | statement.setPosition(i); 73 | statement.setParent(entity); 74 | result.add(statement); 75 | } 76 | 77 | return result; 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/IfThenElseEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ConditionHolderEntity; 26 | import org.codeontology.extraction.support.ConditionTagger; 27 | import spoon.reflect.code.CtIf; 28 | import spoon.reflect.code.CtStatement; 29 | 30 | public class IfThenElseEntity extends StatementEntity implements ConditionHolderEntity { 31 | 32 | public IfThenElseEntity(CtIf element) { 33 | super(element); 34 | } 35 | 36 | @Override 37 | protected RDFNode getType() { 38 | return Ontology.IF_THEN_ELSE_ENTITY; 39 | } 40 | 41 | @Override 42 | public void extract() { 43 | super.extract(); 44 | tagCondition(); 45 | tagThenStatement(); 46 | tagElseStatement(); 47 | } 48 | 49 | @Override 50 | public ExpressionEntity getCondition() { 51 | ExpressionEntity condition = getFactory().wrap(getElement().getCondition()); 52 | condition.setParent(this); 53 | return condition; 54 | } 55 | 56 | @Override 57 | public void tagCondition() { 58 | new ConditionTagger(this).tagCondition(); 59 | } 60 | 61 | public void tagThenStatement() { 62 | CtStatement thenStatement = getElement().getThenStatement(); 63 | StatementEntity statement = getFactory().wrap(thenStatement); 64 | statement.setParent(this); 65 | statement.setPosition(0); 66 | getLogger().addTriple(this, Ontology.THEN_PROPERTY, statement); 67 | statement.extract(); 68 | } 69 | 70 | public void tagElseStatement() { 71 | CtStatement elseStatement = getElement().getElseStatement(); 72 | if (elseStatement != null) { 73 | StatementEntity statement = getFactory().wrap(elseStatement); 74 | statement.setParent(this); 75 | statement.setPosition(1); 76 | getLogger().addTriple(this, Ontology.ELSE_PROPERTY, statement); 77 | statement.extract(); 78 | } 79 | } 80 | 81 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/StatementKind.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import spoon.reflect.code.*; 23 | import spoon.reflect.declaration.CtClass; 24 | 25 | public enum StatementKind { 26 | BLOCK, 27 | IF_THEN_ELSE, 28 | SWITCH, 29 | WHILE, 30 | DO, 31 | FOR, 32 | FOREACH, 33 | TRY, 34 | RETURN, 35 | THROW, 36 | BREAK, 37 | CONTINUE, 38 | ASSERT, 39 | SYNCHRONIZED, 40 | EXPRESSION_STATEMENT, 41 | LOCAL_VARIABLE_DECLARATION, 42 | CLASS_DECLARATION, 43 | STATEMENT; 44 | 45 | public static StatementKind getKindOf(CtStatement statement) { 46 | if (statement instanceof CtBlock) { 47 | return BLOCK; 48 | } else if (statement instanceof CtIf) { 49 | return IF_THEN_ELSE; 50 | } else if (statement instanceof CtSwitch) { 51 | return SWITCH; 52 | } else if (statement instanceof CtWhile) { 53 | return WHILE; 54 | } else if (statement instanceof CtDo) { 55 | return DO; 56 | } else if (statement instanceof CtFor) { 57 | return FOR; 58 | } else if (statement instanceof CtForEach) { 59 | return FOREACH; 60 | } else if (statement instanceof CtTry) { 61 | return TRY; 62 | } else if (statement instanceof CtReturn) { 63 | return RETURN; 64 | } else if (statement instanceof CtThrow) { 65 | return THROW; 66 | } else if (statement instanceof CtBreak) { 67 | return BREAK; 68 | } else if (statement instanceof CtContinue) { 69 | return CONTINUE; 70 | } else if (statement instanceof CtAssert) { 71 | return ASSERT; 72 | } else if (statement instanceof CtSynchronized) { 73 | return SYNCHRONIZED; 74 | } else if (statement instanceof CtLocalVariable) { 75 | return LOCAL_VARIABLE_DECLARATION; 76 | } else if (statement instanceof CtClass) { 77 | return CLASS_DECLARATION; 78 | } else if (statement instanceof CtExpression) { 79 | return EXPRESSION_STATEMENT; 80 | } 81 | 82 | return STATEMENT; 83 | } 84 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/VariableDeclarationTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.Entity; 24 | import org.codeontology.extraction.EntityFactory; 25 | import org.codeontology.extraction.RDFLogger; 26 | import org.codeontology.extraction.expression.ExpressionEntity; 27 | import spoon.reflect.code.CtExpression; 28 | import spoon.reflect.declaration.CtVariable; 29 | 30 | public class VariableDeclarationTagger { 31 | private final VariableDeclarationEntity declaration; 32 | 33 | public VariableDeclarationTagger(VariableDeclarationEntity declaration) { 34 | this.declaration = declaration; 35 | } 36 | 37 | public void tagVariable() { 38 | Entity variable = declaration.getVariable(); 39 | if (variable != null) { 40 | RDFLogger.getInstance().addTriple(variable, Ontology.DECLARATION_PROPERTY, declaration); 41 | variable.extract(); 42 | } 43 | } 44 | 45 | public void tagInitializer() { 46 | ExpressionEntity expression = declaration.getInitializer(); 47 | if (expression != null) { 48 | RDFLogger.getInstance().addTriple(declaration, Ontology.INITIALIZER_PROPERTY, expression); 49 | expression.extract(); 50 | } 51 | } 52 | 53 | public static ExpressionEntity initializerOf(VariableDeclarationEntity> declaration) { 54 | CtExpression defaultExpression = declaration.getElement().getDefaultExpression(); 55 | if (defaultExpression != null) { 56 | ExpressionEntity initializer = EntityFactory.getInstance().wrap(defaultExpression); 57 | initializer.setParent(declaration); 58 | return initializer; 59 | } 60 | 61 | return null; 62 | } 63 | 64 | public static Entity declaredVariableOf(VariableDeclarationEntity> declaration) { 65 | Entity declaredVariable = EntityFactory.getInstance().wrap(declaration.getElement()); 66 | if (declaredVariable != null) { 67 | declaredVariable.setParent(declaration); 68 | return declaredVariable; 69 | } 70 | return null; 71 | } 72 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/expression/MethodInvocationExpressionEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.expression; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.declaration.TypeEntity; 25 | import org.codeontology.extraction.support.ExpressionHolderEntity; 26 | import org.codeontology.extraction.support.ExpressionTagger; 27 | import spoon.reflect.code.CtExpression; 28 | import spoon.reflect.code.CtInvocation; 29 | import spoon.reflect.code.CtTypeAccess; 30 | import spoon.reflect.reference.CtTypeReference; 31 | 32 | public class MethodInvocationExpressionEntity extends AbstractInvocationExpressionEntity> 33 | implements ExpressionHolderEntity> { 34 | 35 | public MethodInvocationExpressionEntity(CtInvocation expression) { 36 | super(expression); 37 | } 38 | 39 | @Override 40 | protected RDFNode getType() { 41 | return Ontology.METHOD_INVOCATION_EXPRESSION_ENTITY; 42 | } 43 | 44 | @Override 45 | public void extract() { 46 | super.extract(); 47 | tagTarget(); 48 | tagArguments(); 49 | tagExecutable(); 50 | } 51 | 52 | public void tagTarget() { 53 | CtExpression target = getElement().getTarget(); 54 | 55 | if (!(target instanceof CtTypeAccess)) { 56 | tagExpression(); 57 | return; 58 | } 59 | 60 | CtTypeReference reference = ((CtTypeAccess) target).getType(); 61 | TypeEntity type = getFactory().wrap(reference); 62 | if (type != null) { 63 | getLogger().addTriple(this, Ontology.TARGET_PROPERTY, type); 64 | type.follow(); 65 | } 66 | } 67 | 68 | @Override 69 | public ExpressionEntity getExpression() { 70 | CtExpression target = getElement().getTarget(); 71 | if (target != null) { 72 | ExpressionEntity expression = getFactory().wrap(target); 73 | expression.setParent(this); 74 | return expression; 75 | } 76 | return null; 77 | } 78 | 79 | @Override 80 | public void tagExpression() { 81 | new ExpressionTagger(this).tagExpression(Ontology.TARGET_PROPERTY); 82 | } 83 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/LocalVariableEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.Entity; 25 | import org.codeontology.extraction.NamedElementEntity; 26 | import org.codeontology.extraction.support.*; 27 | import spoon.reflect.code.CtLocalVariable; 28 | import spoon.reflect.reference.CtTypeReference; 29 | 30 | import java.util.List; 31 | 32 | public class LocalVariableEntity extends NamedElementEntity> 33 | implements MemberEntity>, TypedElementEntity>, ModifiableEntity> { 34 | 35 | public LocalVariableEntity(CtLocalVariable variable) { 36 | super(variable); 37 | } 38 | 39 | @Override 40 | public void extract() { 41 | tagType(); 42 | tagName(); 43 | tagLabel(); 44 | tagJavaType(); 45 | tagModifiers(); 46 | tagDeclaringElement(); 47 | tagSourceCode(); 48 | } 49 | 50 | @Override 51 | public List getModifiers() { 52 | return Modifier.asList(getElement().getModifiers()); 53 | } 54 | 55 | @Override 56 | public void tagModifiers() { 57 | new ModifiableTagger(this).tagModifiers(); 58 | } 59 | 60 | @Override 61 | public String buildRelativeURI() { 62 | return getParent().getRelativeURI() + SEPARATOR + getElement().getSimpleName(); 63 | } 64 | 65 | @Override 66 | protected RDFNode getType() { 67 | return Ontology.LOCAL_VARIABLE_ENTITY; 68 | } 69 | 70 | @Override 71 | public Entity getDeclaringElement() { 72 | return getParent(); 73 | } 74 | 75 | public void tagDeclaringElement() { 76 | new DeclaringElementTagger(this).tagDeclaredBy(); 77 | } 78 | 79 | @Override 80 | public TypeEntity getJavaType() { 81 | CtTypeReference type = getElement().getType(); 82 | TypeEntity entity = getFactory().wrap(type); 83 | entity.setParent(getParent(ExecutableEntity.class, TypeEntity.class)); 84 | return entity; 85 | } 86 | 87 | public void tagJavaType() { 88 | new JavaTypeTagger(this).tagJavaType(); 89 | } 90 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/FormalTypeParametersTagger.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import org.codeontology.CodeOntology; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.Entity; 25 | import org.codeontology.extraction.EntityFactory; 26 | import org.codeontology.extraction.RDFLogger; 27 | import org.codeontology.extraction.declaration.TypeVariableEntity; 28 | import spoon.reflect.reference.CtTypeReference; 29 | 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | public class FormalTypeParametersTagger { 34 | private GenericDeclarationEntity genericDeclaration; 35 | 36 | public FormalTypeParametersTagger(GenericDeclarationEntity genericDeclaration) { 37 | this.genericDeclaration = genericDeclaration; 38 | } 39 | 40 | public void tagFormalTypeParameters() { 41 | if (!CodeOntology.processGenerics()) { 42 | return; 43 | } 44 | List parameters = genericDeclaration.getFormalTypeParameters(); 45 | int size = parameters.size(); 46 | for (int i = 0; i < size; i++) { 47 | TypeVariableEntity typeVariable = parameters.get(i); 48 | typeVariable.setParent(genericDeclaration); 49 | typeVariable.setPosition(i); 50 | RDFLogger.getInstance().addTriple(genericDeclaration, Ontology.FORMAL_TYPE_PARAMETER_PROPERTY, typeVariable); 51 | typeVariable.extract(); 52 | } 53 | } 54 | 55 | public static List formalTypeParametersOf(GenericDeclarationEntity genericDeclaration) { 56 | List typeVariables = new ArrayList<>(); 57 | 58 | if (genericDeclaration.getElement() != null && CodeOntology.processGenerics()) { 59 | List> parameters = genericDeclaration.getElement().getFormalTypeParameters(); 60 | 61 | for (CtTypeReference parameter : parameters) { 62 | Entity entity = EntityFactory.getInstance().wrap(parameter); 63 | if (entity instanceof TypeVariableEntity) { 64 | typeVariables.add((TypeVariableEntity) entity); 65 | } 66 | } 67 | } 68 | 69 | return typeVariables; 70 | } 71 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/TypeKind.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import spoon.reflect.declaration.CtAnnotationType; 23 | import spoon.reflect.declaration.CtEnum; 24 | import spoon.reflect.declaration.CtInterface; 25 | import spoon.reflect.declaration.CtType; 26 | import spoon.reflect.reference.CtArrayTypeReference; 27 | import spoon.reflect.reference.CtTypeParameterReference; 28 | import spoon.reflect.reference.CtTypeReference; 29 | import spoon.support.reflect.reference.SpoonClassNotFoundException; 30 | 31 | public enum TypeKind { 32 | CLASS, 33 | INTERFACE, 34 | ANNOTATION, 35 | ENUM, 36 | PRIMITIVE, 37 | ARRAY, 38 | TYPE_VARIABLE, 39 | PARAMETERIZED_TYPE; 40 | 41 | public static TypeKind getKindOf(CtTypeReference reference) { 42 | 43 | if (reference instanceof CtArrayTypeReference) { 44 | return ARRAY; 45 | } 46 | 47 | if (reference instanceof CtTypeParameterReference) { 48 | return TYPE_VARIABLE; 49 | } 50 | 51 | if (!reference.getActualTypeArguments().isEmpty()) { 52 | return PARAMETERIZED_TYPE; 53 | } 54 | 55 | CtType type = reference.getDeclaration(); 56 | if (type != null) { 57 | return getKindOf(type); 58 | } 59 | 60 | if (reference.isPrimitive()) { 61 | return PRIMITIVE; 62 | } 63 | 64 | try { 65 | Class actualClass = reference.getActualClass(); 66 | if (actualClass.isAnnotation()) { 67 | return ANNOTATION; 68 | } else if (actualClass.isEnum()) { 69 | return ENUM; 70 | } else if (actualClass.isInterface()) { 71 | return INTERFACE; 72 | } else { 73 | return CLASS; 74 | } 75 | } catch (SpoonClassNotFoundException e) { 76 | return null; 77 | } 78 | 79 | } 80 | 81 | public static TypeKind getKindOf(CtType type) { 82 | 83 | if (type.getReference() instanceof CtArrayTypeReference) { 84 | return ARRAY; 85 | } else if (type.getReference().getActualTypeArguments().size() > 0) { 86 | return PARAMETERIZED_TYPE; 87 | } else if (type.isPrimitive()) { 88 | return PRIMITIVE; 89 | } else if (type instanceof CtAnnotationType) { 90 | return ANNOTATION; 91 | } else if (type instanceof CtEnum) { 92 | return ENUM; 93 | } else if (type instanceof CtInterface) { 94 | return INTERFACE; 95 | } else { 96 | return CLASS; 97 | } 98 | } 99 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/ForEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.Entity; 25 | import org.codeontology.extraction.expression.ExpressionEntity; 26 | import org.codeontology.extraction.support.ConditionHolderEntity; 27 | import org.codeontology.extraction.support.ConditionTagger; 28 | import spoon.reflect.code.CtExpression; 29 | import spoon.reflect.code.CtFor; 30 | 31 | import java.util.List; 32 | 33 | public class ForEntity extends LoopEntity implements ConditionHolderEntity { 34 | 35 | public ForEntity(CtFor element) { 36 | super(element); 37 | } 38 | 39 | @Override 40 | protected RDFNode getType() { 41 | return Ontology.FOR_ENTITY; 42 | } 43 | 44 | @Override 45 | public void extract() { 46 | super.extract(); 47 | tagForInit(); 48 | tagCondition(); 49 | tagForUpdate(); 50 | } 51 | 52 | @Override 53 | public ExpressionEntity getCondition() { 54 | CtExpression expression = getElement().getExpression(); 55 | if (expression != null) { 56 | ExpressionEntity condition = getFactory().wrap(expression); 57 | condition.setParent(this); 58 | return condition; 59 | } 60 | 61 | return null; 62 | } 63 | 64 | @Override 65 | public void tagCondition() { 66 | new ConditionTagger(this).tagCondition(); 67 | } 68 | 69 | private StatementExpressionListEntity getForInit() { 70 | StatementExpressionListEntity forInit = getFactory().wrap(getElement().getForInit()); 71 | forInit.setParent(this); 72 | forInit.setPosition(0); 73 | return forInit; 74 | } 75 | 76 | public void tagForInit() { 77 | List> forInit = getForInit().getElement(); 78 | for (Entity init : forInit) { 79 | getLogger().addTriple(this, Ontology.FOR_INIT_PROPERTY, init); 80 | init.extract(); 81 | } 82 | } 83 | 84 | private StatementExpressionListEntity getForUpdate() { 85 | StatementExpressionListEntity forUpdate = getFactory().wrap(getElement().getForUpdate()); 86 | forUpdate.setPosition(2); 87 | forUpdate.setParent(this); 88 | return forUpdate; 89 | } 90 | 91 | public void tagForUpdate() { 92 | List> forUpdate = getForUpdate().getElement(); 93 | for (Entity update : forUpdate) { 94 | getLogger().addTriple(this, Ontology.FOR_UPDATE_PROPERTY, update); 95 | update.extract(); 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/Project.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | import org.codeontology.extraction.project.ProjectVisitor; 23 | 24 | import java.io.File; 25 | import java.io.FileNotFoundException; 26 | import java.util.ArrayList; 27 | import java.util.Collection; 28 | import java.util.List; 29 | import java.util.Scanner; 30 | 31 | public abstract class Project { 32 | protected File projectDirectory; 33 | protected Collection subProjects; 34 | protected File root; 35 | 36 | public Project(File projectDirectory) { 37 | this.projectDirectory = projectDirectory; 38 | this.root = projectDirectory; 39 | subProjects = findSubProjects(); 40 | } 41 | 42 | protected abstract Collection findSubProjects(); 43 | 44 | public File getProjectDirectory() { 45 | return projectDirectory; 46 | } 47 | 48 | public Collection getSubProjects() { 49 | return subProjects; 50 | } 51 | 52 | public ProjectFactory getFactory() { 53 | return ProjectFactory.getInstance(); 54 | } 55 | 56 | public String getBuildFileContent() { 57 | if (getBuildFile() != null) { 58 | try (Scanner scanner = new Scanner(getBuildFile())) { 59 | scanner.useDelimiter("\\Z"); 60 | String build = ""; 61 | if (scanner.hasNext()) { 62 | build = scanner.next(); 63 | } 64 | scanner.close(); 65 | return build; 66 | } catch (FileNotFoundException e) { 67 | return ""; 68 | } 69 | } 70 | 71 | return ""; 72 | } 73 | 74 | public abstract File getBuildFile(); 75 | 76 | public String getPath() { 77 | return getProjectDirectory().getPath(); 78 | } 79 | 80 | public abstract DependenciesLoader getLoader(); 81 | 82 | public File getRoot() { 83 | return root; 84 | } 85 | 86 | public void setRoot(File root) { 87 | this.root = root; 88 | } 89 | 90 | protected Collection initSubProjects(Collection files) { 91 | List result = new ArrayList<>(); 92 | for (File file : files) { 93 | Project subProject = getFactory().getProject(file); 94 | subProject.setRoot(getRoot()); 95 | result.add(subProject); 96 | } 97 | 98 | return result; 99 | } 100 | 101 | public String getName() { 102 | return getProjectDirectory().getName(); 103 | } 104 | 105 | public abstract void accept(ProjectVisitor visitor); 106 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/SwitchEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.expression.ExpressionEntity; 25 | import org.codeontology.extraction.support.ExpressionHolderEntity; 26 | import org.codeontology.extraction.support.ExpressionTagger; 27 | import spoon.reflect.code.CtCase; 28 | import spoon.reflect.code.CtExpression; 29 | import spoon.reflect.code.CtSwitch; 30 | 31 | import java.util.ArrayList; 32 | import java.util.Iterator; 33 | import java.util.List; 34 | 35 | public class SwitchEntity extends StatementEntity> implements ExpressionHolderEntity> { 36 | 37 | public SwitchEntity(CtSwitch element) { 38 | super(element); 39 | } 40 | 41 | @Override 42 | protected RDFNode getType() { 43 | return Ontology.SWITCH_ENTITY; 44 | } 45 | 46 | @Override 47 | public void extract() { 48 | super.extract(); 49 | tagExpression(); 50 | tagSwitchLabels(); 51 | } 52 | 53 | public void tagSwitchLabels() { 54 | List labels = getSwitchLabels(); 55 | for (SwitchLabelEntity label : labels) { 56 | getLogger().addTriple(this, Ontology.SWITCH_LABEL_PROPERTY, label); 57 | label.extract(); 58 | } 59 | } 60 | 61 | public List getSwitchLabels() { 62 | List> labels = new ArrayList<>(getElement().getCases()); 63 | List result = new ArrayList<>(); 64 | 65 | Iterator> iterator = labels.iterator(); 66 | if (iterator.hasNext()) { 67 | SwitchLabelEntity previous = getFactory().wrap(iterator.next()); 68 | previous.setParent(this); 69 | result.add(previous); 70 | 71 | while (iterator.hasNext()) { 72 | SwitchLabelEntity current = getFactory().wrap(iterator.next()); 73 | current.setParent(this); 74 | result.add(current); 75 | previous.setNext(current); 76 | } 77 | } 78 | 79 | return result; 80 | } 81 | 82 | 83 | @Override 84 | public ExpressionEntity getExpression() { 85 | CtExpression selector = getElement().getSelector(); 86 | if (selector == null) { 87 | return null; 88 | } 89 | 90 | ExpressionEntity expression = getFactory().wrap(selector); 91 | expression.setParent(this); 92 | return expression; 93 | } 94 | 95 | @Override 96 | public void tagExpression() { 97 | new ExpressionTagger(this).tagExpression(); 98 | } 99 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/CodeElementEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.declaration.TypeEntity; 24 | import spoon.reflect.cu.SourcePosition; 25 | import spoon.reflect.declaration.CtAnnotation; 26 | import spoon.reflect.declaration.CtElement; 27 | import spoon.reflect.declaration.CtType; 28 | 29 | import java.util.List; 30 | 31 | public abstract class CodeElementEntity extends AbstractEntity { 32 | 33 | protected CodeElementEntity() { 34 | 35 | } 36 | 37 | protected CodeElementEntity(E element) { 38 | super(element); 39 | } 40 | 41 | @Override 42 | public String buildRelativeURI() { 43 | return buildRelativeURI(""); 44 | } 45 | 46 | protected String buildRelativeURI(String tag) { 47 | SourcePosition position = getElement().getPosition(); 48 | StringBuilder builder = new StringBuilder(); 49 | if (tag == null) { 50 | tag = ""; 51 | } 52 | 53 | tag = tag.trim(); 54 | 55 | if (position == null) { 56 | builder.append(getParent().getRelativeURI()); 57 | if (!tag.equals("")) { 58 | builder.append(SEPARATOR).append(tag); 59 | } 60 | builder.append(SEPARATOR).append("-1"); 61 | return builder.toString(); 62 | } 63 | 64 | CtType mainType = position.getCompilationUnit().getMainType(); 65 | TypeEntity mainTypeEntity = getFactory().wrap(mainType); 66 | builder.append(mainTypeEntity.getRelativeURI()); 67 | if (!tag.equals("")) { 68 | builder.append(SEPARATOR).append(tag); 69 | } 70 | builder.append(SEPARATOR) 71 | .append(position.getLine()) 72 | .append(SEPARATOR) 73 | .append(position.getColumn()) 74 | .append(SEPARATOR) 75 | .append(position.getEndColumn()); 76 | 77 | return builder.toString(); 78 | } 79 | 80 | public void tagComment() { 81 | String comment = getElement().getDocComment(); 82 | if (comment != null) { 83 | getLogger().addTriple(this, Ontology.COMMENT_PROPERTY, getModel().createLiteral(comment)); 84 | } 85 | } 86 | 87 | public void tagAnnotations() { 88 | List> annotations = getElement().getAnnotations(); 89 | for (CtAnnotation annotation : annotations) { 90 | TypeEntity annotationType = getFactory().wrap(annotation.getAnnotationType()); 91 | getLogger().addTriple(this, Ontology.ANNOTATION_PROPERTY, annotationType); 92 | annotationType.follow(); 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/expression/AbstractInvocationExpressionEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.expression; 21 | 22 | import org.codeontology.Ontology; 23 | import org.codeontology.extraction.declaration.ExecutableEntity; 24 | import org.codeontology.extraction.declaration.TypeEntity; 25 | import spoon.reflect.code.CtAbstractInvocation; 26 | import spoon.reflect.code.CtExpression; 27 | import spoon.reflect.reference.CtExecutableReference; 28 | 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | public abstract class AbstractInvocationExpressionEntity & CtExpression> 33 | extends ExpressionEntity { 34 | public AbstractInvocationExpressionEntity(T expression) { 35 | super(expression); 36 | } 37 | 38 | @Override 39 | public void extract() { 40 | super.extract(); 41 | tagExecutable(); 42 | tagArguments(); 43 | } 44 | 45 | public void tagExecutable() { 46 | ExecutableEntity executable = getExecutable(); 47 | if (executable != null) { 48 | getLogger().addTriple(this, Ontology.INVOKES_PROPERTY, executable); 49 | executable.follow(); 50 | } 51 | } 52 | 53 | public void tagArguments() { 54 | List arguments = getArguments(); 55 | for (ActualArgumentEntity argument : arguments) { 56 | getLogger().addTriple(this, Ontology.ARGUMENT_PROPERTY, argument); 57 | argument.extract(); 58 | } 59 | } 60 | 61 | public ExecutableEntity getExecutable() { 62 | CtExecutableReference reference = getElement().getExecutable(); 63 | if (reference != null) { 64 | ExecutableEntity executable = getFactory().wrap(reference); 65 | TypeEntity declaringType = getFactory().wrap(reference.getDeclaringType()); 66 | executable.setParent(declaringType); 67 | return executable; 68 | } 69 | 70 | return null; 71 | } 72 | 73 | public List getArguments() { 74 | List> expressions = getElement().getArguments(); 75 | List arguments = new ArrayList<>(); 76 | 77 | if (expressions == null) { 78 | return arguments; 79 | } 80 | 81 | int size = expressions.size(); 82 | 83 | for (int i = 0; i < size; i++) { 84 | ExpressionEntity expression = getFactory().wrap(expressions.get(i)); 85 | ActualArgumentEntity argument = new ActualArgumentEntity(expression); 86 | expression.setParent(argument); 87 | argument.setParent(this); 88 | argument.setPosition(i); 89 | arguments.add(argument); 90 | } 91 | 92 | return arguments; 93 | } 94 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/docparser/DocCommentParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.docparser; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | import java.util.Scanner; 25 | import java.util.regex.Matcher; 26 | import java.util.regex.Pattern; 27 | import java.util.stream.Collectors; 28 | 29 | public class DocCommentParser { 30 | private String comment; 31 | private List tags; 32 | private boolean parsed; 33 | public static final String REGEXP = "^\\s*(@\\w+)\\s+(.*)$"; 34 | public static final Pattern PATTERN = Pattern.compile(REGEXP, Pattern.DOTALL); 35 | 36 | public DocCommentParser(String comment) { 37 | setComment(comment); 38 | } 39 | 40 | private void setComment(String comment) { 41 | parsed = false; 42 | this.comment = comment; 43 | tags = new ArrayList<>(); 44 | } 45 | 46 | public void parse() { 47 | Scanner scanner = new Scanner("\n" + removeDescription()); 48 | scanner.useDelimiter("\\n\\s*@"); 49 | while (scanner.hasNext()) { 50 | String current = "@" + scanner.next(); 51 | Matcher matcher = getMatcher(current); 52 | if (matcher.matches()) { 53 | String name = matcher.group(1); 54 | String text = matcher.group(2); 55 | Tag tag = TagFactory.getInstance().createTag(name, text); 56 | tags.add(tag); 57 | } 58 | } 59 | scanner.close(); 60 | parsed = true; 61 | } 62 | 63 | private String removeDescription() { 64 | Scanner scanner = new Scanner(comment); 65 | while (scanner.hasNextLine()) { 66 | String current = scanner.nextLine(); 67 | if (getMatcher(current).matches()) { 68 | scanner.useDelimiter("\\Z"); 69 | if (scanner.hasNext()) { 70 | current += "\n" + scanner.next(); 71 | } 72 | scanner.close(); 73 | return current; 74 | } 75 | } 76 | scanner.close(); 77 | return ""; 78 | } 79 | 80 | public List getParamTags() { 81 | return getTags(ParamTag.TAG); 82 | } 83 | 84 | public List getReturnTags() { 85 | return getTags(ReturnTag.TAG); 86 | } 87 | 88 | public List getTags(String name) { 89 | if (!parsed) { 90 | parse(); 91 | } 92 | 93 | return tags.stream() 94 | .filter(tag -> tag.getName().equals(name)) 95 | .collect(Collectors.toCollection(ArrayList::new)); 96 | 97 | } 98 | 99 | private Matcher getMatcher(String current) { 100 | return PATTERN.matcher(current); 101 | } 102 | 103 | 104 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/NamedElementEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction; 21 | 22 | import com.hp.hpl.jena.rdf.model.Literal; 23 | import org.codeontology.CodeOntology; 24 | import org.codeontology.Ontology; 25 | import spoon.reflect.declaration.CtNamedElement; 26 | import spoon.reflect.reference.CtReference; 27 | 28 | public abstract class NamedElementEntity extends CodeElementEntity { 29 | 30 | private CtReference reference; 31 | 32 | protected NamedElementEntity(E element) { 33 | setElement(element); 34 | } 35 | 36 | protected NamedElementEntity(CtReference reference) { 37 | setReference(reference); 38 | } 39 | 40 | @SuppressWarnings("unchecked") 41 | private void setReference(CtReference reference) { 42 | if (reference == null) { 43 | throw new IllegalArgumentException(); 44 | } 45 | 46 | this.reference = reference; 47 | if (reference.getDeclaration() != null && getElement() == null) { 48 | setElement((E) reference.getDeclaration()); 49 | } 50 | } 51 | 52 | @Override 53 | protected void setElement(E element) { 54 | if (element == null) { 55 | throw new IllegalArgumentException(); 56 | } 57 | super.setElement(element); 58 | if (reference == null) { 59 | try { 60 | this.reference = element.getReference(); 61 | } catch (ClassCastException e) { 62 | // leave reference null 63 | } 64 | } 65 | } 66 | 67 | public CtReference getReference() { 68 | return reference; 69 | } 70 | 71 | public String getName() { 72 | return getReference().getSimpleName(); 73 | } 74 | 75 | public void tagName() { 76 | Literal name = getModel().createTypedLiteral(getName()); 77 | getLogger().addTriple(this, Ontology.NAME_PROPERTY, name); 78 | } 79 | 80 | public void tagLabel() { 81 | String labelString = splitCamelCase(getName()); 82 | Literal label = getModel().createTypedLiteral(labelString); 83 | getLogger().addTriple(this, Ontology.RDFS_LABEL_PROPERTY, label); 84 | } 85 | 86 | public String splitCamelCase(String s) { 87 | return s.replaceAll( 88 | String.format("%s|%s|%s", 89 | "(?<=[A-Z])(?=[A-Z][a-z])", 90 | "(?<=[^A-Z])(?=[A-Z])", 91 | "(?<=[A-Za-z])(?=[^A-Za-z])" 92 | ), " " 93 | ); 94 | } 95 | 96 | @Override 97 | public void follow() { 98 | if (!isDeclarationAvailable() && !CodeOntology.isJarExplorationEnabled() 99 | && EntityRegister.getInstance().add(this)) { 100 | extract(); 101 | } 102 | } 103 | 104 | public boolean isDeclarationAvailable() { 105 | return getElement() != null; 106 | } 107 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/declaration/AnonymousClassEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.declaration; 21 | 22 | import com.hp.hpl.jena.rdf.model.Property; 23 | import com.hp.hpl.jena.rdf.model.RDFNode; 24 | import org.codeontology.Ontology; 25 | import org.codeontology.extraction.Entity; 26 | import spoon.reflect.declaration.CtClass; 27 | import spoon.reflect.reference.CtTypeReference; 28 | 29 | import java.util.HashSet; 30 | import java.util.List; 31 | import java.util.Set; 32 | 33 | public class AnonymousClassEntity extends ClassEntity { 34 | 35 | public static final String TAG = "anonymous-class"; 36 | private Set> requestedResources; 37 | 38 | public AnonymousClassEntity(CtClass anonymousClass) { 39 | super(anonymousClass); 40 | requestedResources = new HashSet<>(); 41 | } 42 | 43 | @Override 44 | public String buildRelativeURI() { 45 | return getParent().getRelativeURI() + SEPARATOR + TAG + SEPARATOR + getElement().getSimpleName(); 46 | } 47 | 48 | @Override 49 | public RDFNode getType() { 50 | return Ontology.ANONYMOUS_CLASS_ENTITY; 51 | } 52 | 53 | @Override 54 | public void extract() { 55 | tagType(); 56 | tagSuperType(); 57 | tagComment(); 58 | tagFields(); 59 | tagMethods(); 60 | tagSourceCode(); 61 | tagNestedTypes(); 62 | } 63 | 64 | public void tagSuperType() { 65 | Set> references = getReference().getSuperInterfaces(); 66 | CtTypeReference superTypeReference; 67 | Property property; 68 | if (references.isEmpty()) { 69 | superTypeReference = getReference().getSuperclass(); 70 | property = Ontology.EXTENDS_PROPERTY; 71 | } else { 72 | superTypeReference = (CtTypeReference) references.toArray()[0]; 73 | property = Ontology.IMPLEMENTS_PROPERTY; 74 | } 75 | TypeEntity superType = getFactory().wrap(superTypeReference); 76 | superType.setParent(getParent()); 77 | getLogger().addTriple(this, property, superType); 78 | requestedResources.add(superType); 79 | superType.follow(); 80 | } 81 | 82 | public Set> getRequestedResources() { 83 | return requestedResources; 84 | } 85 | 86 | @Override 87 | public void tagMethods() { 88 | List methods = getMethods(); 89 | for (MethodEntity method : methods) { 90 | method.extract(); 91 | requestedResources.addAll(method.getRequestedResources()); 92 | } 93 | } 94 | 95 | @Override 96 | public void tagFields() { 97 | List fields = getFields(); 98 | for (FieldEntity field : fields) { 99 | field.extract(); 100 | requestedResources.add(field.getJavaType()); 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /scripts/get_java_repositories/quick_list.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | import json 3 | 4 | auth = "https://api.github.com/?access_token=" 5 | tokens_ptr = 0 6 | tokens = {0: "2cec86610286f8ed43ca08cace5b3b14374af183", 7 | 1: "85567176f1d77ebe7792b85c36650ee30ef4f29e"} 8 | client_id = "e24bed6b410a7bcc89a5" 9 | client_secret = "80b111079f9e708da6ad463ccc8749152259d139" 10 | 11 | 12 | def is_maven(contents_url): 13 | """ Check if given repository url is a gradle one. 14 | 15 | @contents_url The REST url for the repository contents. 16 | @return True iff it is a maven repository. 17 | """ 18 | req = urllib.request.Request(contents_url) 19 | req.add_header('Accept', 'application/json') 20 | json_res = urllib.request.urlopen(req).read() 21 | 22 | json_data = json_res.decode('utf-8') 23 | files = json.loads(json_data) 24 | 25 | for remote_file in files: 26 | if remote_file["name"] == "pom.xml": 27 | return True 28 | 29 | return False 30 | 31 | 32 | def is_gradle(contents_url): 33 | """ Check if given repository url is a gradle one. 34 | 35 | @contents_url The REST url for the repository contents. 36 | @return True iff it is a gradle repository. 37 | """ 38 | req = urllib.request.Request(contents_url) 39 | req.add_header('Accept', 'application/json') 40 | json_response = urllib.request.urlopen(req).read() 41 | 42 | json_data = json_response.decode('utf-8') 43 | files = json.loads(json_data) 44 | 45 | for remote_file in files: 46 | if remote_file["name"] == "build.gradle": 47 | return True 48 | 49 | return False 50 | 51 | 52 | def list_repositories(last): 53 | """ List repositories, starting from @last. 54 | 55 | @last: The last listed repository id. 56 | 57 | @return Nothing. 58 | """ 59 | global tokens_ptr 60 | global client_id 61 | global client_secret 62 | 63 | try: 64 | for i in range(1, 11): 65 | print("Requesting https://api.github.com/search/repositories?q=language:java&per_page=100&page=" + str(i)) 66 | req = urllib.request.Request("https://api.github.com/search/repositories?q=language:java&per_page=100&page=" + str(i)) 67 | req.add_header('Accept', 'application/json') 68 | json_res = urllib.request.urlopen(req).read() 69 | json_data = json_res.decode('utf-8') 70 | data = json.loads(json_data) 71 | 72 | for repository in data["items"]: 73 | repository_id = repository["id"] 74 | git_url = repository["html_url"] 75 | github_page = git_url 76 | contents_url_tmp = str(repository["contents_url"]) + "?client_id=" + client_id + "&client_secret=" + client_secret 77 | contents_url = contents_url_tmp.replace("/{+path}", "") 78 | 79 | req = urllib.request.Request(github_page) 80 | github_page_response = urllib.request.urlopen(req).read() 81 | github_page_html = github_page_response.decode("utf-8") 82 | 83 | if ((is_maven(contents_url) or is_gradle(contents_url))): 84 | print("Adding " + str(git_url)) 85 | open("quick-repositories", "a").write(git_url + "\n") 86 | 87 | last = repository_id 88 | 89 | except urllib.error.HTTPError as e: 90 | if str(e).startswith("HTTP Error 403"): 91 | print("Switching to new token") 92 | tokens_ptr = tokens_ptr + 1 93 | 94 | req = urllib.request.Request(auth + tokens[tokens_ptr]) 95 | urllib.request.urlopen(req).read() 96 | 97 | list_repositories(int(repository_id) - 1) 98 | else: 99 | print(str(e)) 100 | list_repositories(int(repository_id) - 1) 101 | except UnicodeDecodeError: 102 | print("Decode error on " + str(git_url)) 103 | list_repositories(int(last) + 1) 104 | 105 | if __name__ == "__main__": 106 | req = urllib.request.Request(auth + tokens[tokens_ptr]) 107 | urllib.request.urlopen(req).read() 108 | 109 | list_repositories(0) 110 | -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/statement/CatchEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.statement; 21 | 22 | import com.hp.hpl.jena.rdf.model.RDFNode; 23 | import org.codeontology.Ontology; 24 | import org.codeontology.extraction.CodeElementEntity; 25 | import org.codeontology.extraction.declaration.ExecutableEntity; 26 | import org.codeontology.extraction.declaration.TypeEntity; 27 | import org.codeontology.extraction.support.LineTagger; 28 | import org.codeontology.extraction.support.StatementsHolderEntity; 29 | import org.codeontology.extraction.support.StatementsTagger; 30 | import spoon.reflect.code.CtCatch; 31 | import spoon.reflect.reference.CtTypeReference; 32 | 33 | import java.util.ArrayList; 34 | import java.util.List; 35 | 36 | public class CatchEntity extends CodeElementEntity implements StatementsHolderEntity { 37 | 38 | private int position; 39 | 40 | public CatchEntity(CtCatch catcher) { 41 | super(catcher); 42 | } 43 | 44 | @Override 45 | protected RDFNode getType() { 46 | return Ontology.CATCH_ENTITY; 47 | } 48 | 49 | @Override 50 | public void extract() { 51 | tagType(); 52 | tagSourceCode(); 53 | tagLine(); 54 | tagStatements(); 55 | tagEndLine(); 56 | tagCatchFormalParameters(); 57 | } 58 | 59 | public void tagCatchFormalParameters() { 60 | List> formalParameters = getCatchFormalParameters(); 61 | for (TypeEntity catchFormalParameter : formalParameters) { 62 | getLogger().addTriple(this, Ontology.CATCH_FORMAL_PARAMETER_PROPERTY, catchFormalParameter); 63 | catchFormalParameter.follow(); 64 | } 65 | } 66 | 67 | public List> getCatchFormalParameters() { 68 | List> references = getElement().getParameter().getMultiTypes(); 69 | List> parameters = new ArrayList<>(); 70 | 71 | for (CtTypeReference reference : references) { 72 | TypeEntity parameter = getFactory().wrap(reference); 73 | parameter.setParent(getParent(ExecutableEntity.class)); 74 | parameters.add(parameter); 75 | } 76 | 77 | return parameters; 78 | } 79 | 80 | public int getPosition() { 81 | return position; 82 | } 83 | 84 | public void setPosition(int position) { 85 | this.position = position; 86 | } 87 | 88 | public void tagLine() { 89 | new LineTagger(this).tagLine(); 90 | } 91 | 92 | @Override 93 | public List> getStatements() { 94 | return new StatementsTagger(this).asEntities(getElement().getBody().getStatements()); 95 | } 96 | 97 | @Override 98 | public void tagStatements() { 99 | new StatementsTagger(this).tagStatements(); 100 | } 101 | 102 | public void tagEndLine() { 103 | new LineTagger(this).tagEndLine(); 104 | } 105 | 106 | 107 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/maven/MavenProject.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build.maven; 21 | 22 | import org.apache.commons.lang3.StringUtils; 23 | import org.codeontology.build.DependenciesLoader; 24 | import org.codeontology.build.Project; 25 | import org.codeontology.extraction.project.ProjectVisitor; 26 | 27 | import java.io.File; 28 | import java.io.FileNotFoundException; 29 | import java.util.Collection; 30 | import java.util.HashSet; 31 | import java.util.Scanner; 32 | import java.util.Set; 33 | 34 | public class MavenProject extends Project { 35 | 36 | private org.apache.maven.project.MavenProject mavenProject; 37 | private File buildFile; 38 | private MavenLoader loader; 39 | private boolean setUp; 40 | 41 | public MavenProject(File project) { 42 | super(project); 43 | setUp(); 44 | } 45 | 46 | private void setUp() { 47 | if (!setUp) { 48 | mavenProject = new org.apache.maven.project.MavenProject(); 49 | buildFile = new File(getPath() + "/pom.xml"); 50 | mavenProject.setFile(buildFile); 51 | loader = new MavenLoader(this); 52 | setUp = true; 53 | } 54 | } 55 | 56 | @Override 57 | protected Collection findSubProjects() { 58 | setUp(); 59 | try { 60 | Set modules = new HashSet<>(); 61 | File pom = new File(mavenProject.getBasedir() + "/pom.xml"); 62 | Scanner scanner = new Scanner(pom); 63 | 64 | while (scanner.hasNextLine()) { 65 | String line = scanner.nextLine(); 66 | String match = StringUtils.substringBetween(line, "", ""); 67 | 68 | if (!(match == null) && !match.equals("")) { 69 | modules.add(new File(mavenProject.getBasedir() + "/" + match)); 70 | System.out.println("Module: " + mavenProject.getBasedir() + "/" + match); 71 | } 72 | } 73 | 74 | mavenProject.getModules().forEach(module -> { 75 | System.out.println("module: " + mavenProject.getBasedir() + "/" + module); 76 | modules.add(new File(mavenProject.getBasedir() + "/" + module)); 77 | } 78 | ); 79 | 80 | return initSubProjects(modules); 81 | } catch (FileNotFoundException e) { 82 | throw new RuntimeException(e); 83 | } 84 | } 85 | 86 | @Override 87 | public DependenciesLoader getLoader() { 88 | return loader; 89 | } 90 | 91 | @Override 92 | public void accept(ProjectVisitor visitor) { 93 | visitor.visit(this); 94 | } 95 | 96 | @Override 97 | public File getBuildFile() { 98 | return buildFile; 99 | } 100 | 101 | @Override 102 | public File getProjectDirectory() { 103 | if (!setUp) { 104 | return super.getProjectDirectory(); 105 | } 106 | return mavenProject.getBasedir(); 107 | } 108 | } -------------------------------------------------------------------------------- /src/main/java/org/codeontology/build/ClasspathLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.build; 21 | 22 | import org.apache.commons.io.FileUtils; 23 | import org.apache.commons.io.filefilter.FileFilterUtils; 24 | import org.apache.commons.io.filefilter.TrueFileFilter; 25 | 26 | import java.io.File; 27 | import java.lang.reflect.Method; 28 | import java.net.MalformedURLException; 29 | import java.net.URL; 30 | import java.net.URLClassLoader; 31 | import java.util.HashSet; 32 | import java.util.Set; 33 | 34 | public class ClasspathLoader { 35 | 36 | private static ClasspathLoader instance; 37 | private Set classpath; 38 | private boolean locked; 39 | 40 | private ClasspathLoader() { 41 | classpath = new HashSet<>(); 42 | locked = false; 43 | } 44 | 45 | public static ClasspathLoader getInstance() { 46 | if (instance == null) { 47 | instance = new ClasspathLoader(); 48 | } 49 | return instance; 50 | } 51 | 52 | public void load(String path) { 53 | load(new File(path)); 54 | } 55 | 56 | public void load(File file) { 57 | if (file.isDirectory()) { 58 | loadAllJars(file); 59 | return; 60 | } 61 | 62 | if (file.getPath().endsWith(".jar") && !locked) { 63 | classpath.add(file); 64 | } 65 | 66 | try { 67 | load(file.toURI().toURL()); 68 | } catch (MalformedURLException e) { 69 | throw new RuntimeException(e); 70 | } 71 | } 72 | 73 | private void load(URL url) { 74 | URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader(); 75 | Class clazz = URLClassLoader.class; 76 | final Class[] PARAMETERS = new Class[]{URL.class}; 77 | 78 | try { 79 | Method method = clazz.getDeclaredMethod("addURL", PARAMETERS); 80 | method.setAccessible(true); 81 | method.invoke(loader, url); 82 | } catch (Throwable t) { 83 | System.err.println("Error loading " + url.getPath()); 84 | } 85 | } 86 | 87 | public void loadAllJars(File root) { 88 | if (root.isDirectory()) { 89 | Set jars = new HashSet<>(); 90 | 91 | jars.addAll(FileUtils.listFiles(root, 92 | FileFilterUtils.suffixFileFilter(".jar"), 93 | TrueFileFilter.INSTANCE)); 94 | 95 | jars.forEach(this::load); 96 | } 97 | } 98 | 99 | public void loadAllJars(String path) { 100 | loadAllJars(new File(path)); 101 | } 102 | 103 | public void loadClasspath(String classpath) { 104 | String[] paths = classpath.split(":"); 105 | for (String path : paths) { 106 | load(path); 107 | } 108 | } 109 | 110 | public Set getJarsLoaded() { 111 | return classpath; 112 | } 113 | 114 | public void lock() { 115 | locked = true; 116 | } 117 | 118 | public void release() { 119 | locked = false; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeOntology 2 | 3 | ### RDF-ization of source code 4 | CodeOntology is an extraction tool that parses Java source code to generate RDF triples. It supports both maven and gradle projects. For more details see [codeontology.org](http://codeontology.org/). 5 | 6 | ### Set up 7 | First, check dependencies in the Dockerfile. 8 | 9 | To set up codeontology, you have to clone the repository and build the tool: 10 | ```bash 11 | $ git clone https://github.com/codeontology/parser 12 | $ cd codeontology 13 | $ mvn package -DskipTests 14 | ``` 15 | 16 | Now, you can run the tool on any java project: 17 | ```bash 18 | $ ./codeontology -i -o 19 | ``` 20 | 21 | For a complete list of all command line options, just type: 22 | ```bash 23 | $ ./codeontology --help 24 | ``` 25 | 26 | ### Use cases 27 | #### JDK 28 | Let's use the tool to extract RDF triples from the OpenJDK 8 source code. 29 | 30 | First, you need the OpenJDK 8 source code. It is available on github: 31 | ```bash 32 | $ git clone https://github.com/codeontology/openjdk8.git 33 | ``` 34 | 35 | Now, you have to install OpenJDK 8: 36 | ```bash 37 | $ sudo dpkg -iR openjdk8/amd64 38 | ``` 39 | 40 | The above command should install OpenJDK 8. If you get dependecy errors, just type: 41 | ```bash 42 | $ sudo apt-get -f install 43 | ``` 44 | 45 | Set the newly installed version of Java as the default version: 46 | ```bash 47 | $ sudo update-java-alternatives -s java-1.8.0-openjdk-amd64 48 | ``` 49 | 50 | If you get the following error, just ignore it: 51 | ```bash 52 | update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so 53 | ``` 54 | 55 | To verify that everything has worked, check that your java version is correct: 56 | ```bash 57 | $ java -version 58 | openjdk version "1.8.0_121" 59 | OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-4-b13) 60 | OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) 61 | ``` 62 | 63 | Finally, you are ready to serialize the OpenJDK source code into RDF triples. Just type: 64 | ```bash 65 | $ ./codeontology -i openjdk8/ -o openjdk8.nt 66 | ``` 67 | 68 | This command will run the tool on the openjdk8 directory and save the extracted RDF triples to the file `openjdk8.nt`. 69 | Be aware that this may take 2 hour and a half! 70 | 71 | To annotate source code comments, see [CommentLinker](https://github.com/codeontology/commentlinker). 72 | 73 | #### Maven Repository 74 | Let's suppose you want to use the tool to extract RDF triples from a generic repository. 75 | Here the spoon maven repository is used as an example to show how it works. 76 | 77 | First, you have to clone the repository: 78 | 79 | ```bash 80 | $ git clone https://github.com/INRIA/spoon 81 | $ git checkout spoon-core-5.1.0 82 | ``` 83 | 84 | The repository contains tests that cause some troubles when building the abstract syntax tree. The -f switch is added to solve this issue and get rid of the tests. Moreover, the --dependencies switch is here used to parse all of the dependencies of the repository. The -v switch tells CodeOntology to verbosely print out all files processed. 85 | 86 | ```bash 87 | $ ./codeontology -i spoon -o spoon.nt -vf --dependencies 88 | ``` 89 | 90 | Another interesting repository that can be used as example is Apache [Commons Math](https://github.com/apache/commons-math) (it will take less than 2 minutes to build the triples). 91 | 92 | #### Jar files 93 | CodeOntology can also process jar files: 94 | 95 | ```bash 96 | $ ./codeontology --jar 97 | ``` 98 | 99 | In the following example, a jar file is downloaded to show how it works. 100 | 101 | ```bash 102 | $ wget -O weka.zip http://downloads.sourceforge.net/project/weka/weka-3-8/3.8.0/weka-3-8-0.zip?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Fweka%2F&ts=1463402758&use_mirror=kent 103 | $ unzip -j weka.zip "weka-3-8-0/weka.jar" -d . 104 | $ ./codeontology --jar weka.jar -v 105 | ``` 106 | -------------------------------------------------------------------------------- /scripts/get_java_repositories/list.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | import json 3 | 4 | auth = "https://api.github.com/?access_token=" 5 | tokens_ptr = 0 6 | tokens = {0: "2cec86610286f8ed43ca08cace5b3b14374af183", 7 | 1: "85567176f1d77ebe7792b85c36650ee30ef4f29e"} 8 | client_id = "e24bed6b410a7bcc89a5" 9 | client_secret = "80b111079f9e708da6ad463ccc8749152259d139" 10 | 11 | 12 | def is_maven(contents_url): 13 | """ Check if given repository url is a gradle one. 14 | 15 | @contents_url The REST url for the repository contents. 16 | @return True iff it is a maven repository. 17 | """ 18 | req = urllib.request.Request(contents_url) 19 | req.add_header('Accept', 'application/json') 20 | json_res = urllib.request.urlopen(req).read() 21 | 22 | json_data = json_res.decode('utf-8') 23 | files = json.loads(json_data) 24 | 25 | for remote_file in files: 26 | if remote_file["name"] == "pom.xml": 27 | return True 28 | 29 | return False 30 | 31 | 32 | def is_gradle(contents_url): 33 | """ Check if given repository url is a gradle one. 34 | 35 | @contents_url The REST url for the repository contents. 36 | @return True iff it is a gradle repository. 37 | """ 38 | req = urllib.request.Request(contents_url) 39 | req.add_header('Accept', 'application/json') 40 | json_response = urllib.request.urlopen(req).read() 41 | 42 | json_data = json_response.decode('utf-8') 43 | files = json.loads(json_data) 44 | 45 | for remote_file in files: 46 | if remote_file["name"] == "build.gradle": 47 | return True 48 | 49 | return False 50 | 51 | 52 | def list_repositories(last): 53 | """ List repositories, starting from @last. 54 | 55 | @last: The last listed repository id. 56 | 57 | @return Nothing. 58 | """ 59 | global tokens_ptr 60 | global client_id 61 | global client_secret 62 | java_hardcoded = "class=\"language-color\" aria-label=\"Java " 63 | 64 | try: 65 | print("Requesting https://api.github.com/repositories" 66 | + "?since=" + str(last) 67 | + "&client_id=" + client_id 68 | + "&client_secret=" + client_secret) 69 | req = urllib.request.Request("https://api.github.com/repositories" 70 | + "?since=" + str(last) 71 | + "&client_id=" + client_id 72 | + "&client_secret=" + client_secret) 73 | req.add_header('Accept', 'application/json') 74 | json_res = urllib.request.urlopen(req).read() 75 | json_data = json_res.decode('utf-8') 76 | data = json.loads(json_data) 77 | 78 | for repository in data: 79 | repository_id = repository["id"] 80 | git_url = repository["html_url"] 81 | github_page = git_url 82 | contents_url_tmp = str(repository["contents_url"]) + "?client_id=" + client_id + "&client_secret=" + client_secret 83 | contents_url = contents_url_tmp.replace("/{+path}", "") 84 | 85 | print("Repository: " + git_url) 86 | req = urllib.request.Request(github_page) 87 | github_page_response = urllib.request.urlopen(req).read() 88 | github_page_html = github_page_response.decode("utf-8") 89 | 90 | if java_hardcoded in github_page_html and ((is_maven(contents_url) or is_gradle(contents_url))): 91 | print("Adding " + str(git_url)) 92 | open("repositories", "a").write(git_url + "\n") 93 | 94 | last = repository_id 95 | 96 | list_repositories(last) 97 | 98 | except urllib.error.HTTPError as e: 99 | if str(e).startswith("HTTP Error 403"): 100 | print("Switching to new token") 101 | tokens_ptr = tokens_ptr + 1 102 | 103 | req = urllib.request.Request(auth + tokens[tokens_ptr]) 104 | urllib.request.urlopen(req).read() 105 | 106 | list_repositories(int(repository_id) - 1) 107 | else: 108 | print(str(e)) 109 | list_repositories(int(repository_id) - 1) 110 | except UnicodeDecodeError: 111 | print("Decode error on " + str(git_url)) 112 | list_repositories(int(last) + 1) 113 | 114 | if __name__ == "__main__": 115 | req = urllib.request.Request(auth + tokens[tokens_ptr]) 116 | urllib.request.urlopen(req).read() 117 | 118 | list_repositories(0) 119 | -------------------------------------------------------------------------------- /src/main/java/org/codeontology/extraction/support/Modifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017 Mattia Atzeni, Maurizio Atzori 3 | 4 | This file is part of CodeOntology. 5 | 6 | CodeOntology is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | CodeOntology is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with CodeOntology. If not, see 18 | */ 19 | 20 | package org.codeontology.extraction.support; 21 | 22 | import com.hp.hpl.jena.rdf.model.Resource; 23 | import org.codeontology.Ontology; 24 | import spoon.reflect.declaration.ModifierKind; 25 | 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | import java.util.Set; 29 | 30 | public enum Modifier { 31 | PUBLIC (Ontology.PUBLIC_INDIVIDUAL), 32 | PRIVATE (Ontology.PRIVATE_INDIVIDUAL), 33 | PROTECTED (Ontology.PROTECTED_INDIVIDUAL), 34 | DEFAULT (Ontology.DEFAULT_INDIVIDUAL), 35 | ABSTRACT (Ontology.ABSTRACT_INDIVIDUAL), 36 | FINAL (Ontology.FINAL_INDIVIDUAL), 37 | STATIC (Ontology.STATIC_INDIVIDUAL), 38 | SYNCHRONIZED (Ontology.SYNCHRONIZED_INDIVIDUAL), 39 | VOLATILE (Ontology.VOLATILE_INDIVIDUAL); 40 | 41 | private Resource individual; 42 | 43 | Modifier(Resource individual) { 44 | this.individual = individual; 45 | } 46 | 47 | public Resource getIndividual() { 48 | return individual; 49 | } 50 | 51 | public static List asList(Set set) { 52 | List list = new ArrayList<>(); 53 | for (ModifierKind current : set) { 54 | Modifier modifier = valueOf(current); 55 | if (modifier != null) { 56 | list.add(modifier); 57 | } 58 | } 59 | return list; 60 | } 61 | 62 | public static List asList(int code) { 63 | List list = new ArrayList<>(); 64 | if (java.lang.reflect.Modifier.isPublic(code)) { 65 | list.add(PUBLIC); 66 | } else if (java.lang.reflect.Modifier.isPrivate(code)) { 67 | list.add(PRIVATE); 68 | } else if (java.lang.reflect.Modifier.isProtected(code)) { 69 | list.add(PROTECTED); 70 | } else { 71 | list.add(DEFAULT); 72 | } 73 | 74 | if (java.lang.reflect.Modifier.isAbstract(code)) { 75 | list.add(ABSTRACT); 76 | } 77 | 78 | if (java.lang.reflect.Modifier.isFinal(code)) { 79 | list.add(FINAL); 80 | } 81 | 82 | if (java.lang.reflect.Modifier.isStatic(code)) { 83 | list.add(STATIC); 84 | } 85 | 86 | if (java.lang.reflect.Modifier.isSynchronized(code)) { 87 | list.add(SYNCHRONIZED); 88 | } 89 | 90 | if (java.lang.reflect.Modifier.isVolatile(code)) { 91 | list.add(VOLATILE); 92 | } 93 | 94 | return list; 95 | } 96 | 97 | public static Modifier valueOf(ModifierKind modifier) { 98 | if (modifier != null) { 99 | switch (modifier) { 100 | case PUBLIC: 101 | return PUBLIC; 102 | case PRIVATE: 103 | return PRIVATE; 104 | case PROTECTED: 105 | return PROTECTED; 106 | case ABSTRACT: 107 | return ABSTRACT; 108 | case FINAL: 109 | return FINAL; 110 | case STATIC: 111 | return STATIC; 112 | case SYNCHRONIZED: 113 | return SYNCHRONIZED; 114 | case VOLATILE: 115 | return VOLATILE; 116 | } 117 | } 118 | 119 | return null; 120 | } 121 | } --------------------------------------------------------------------------------