获取java路径的5种方法解析

package my;


 import java.io.File;
 import java.io.IOException;
 import java.net.URL;

 public class MyUrlDemo {
      public static void main(String[] args) {
         MyUrlDemo muDemo = new MyUrlDemo();
         try {
             muDemo.showURL();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }

     public void showURL() throws IOException {

         // 第一种:获取类加载的根路径   D:\git\daotie\daotie\target\classes
         File f = new File(this.getClass().getResource("/").getPath());
         System.out.println(f);

         // 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录  D:\git\daotie\daotie\target\classes\my
         File f2 = new File(this.getClass().getResource("").getPath());
         System.out.println(f2);

         // 第二种:获取项目路径    D:\git\daotie\daotie
         File directory = new File("");// 参数为空
         String courseFile = directory.getCanonicalPath();
         System.out.println(courseFile);

 
         // 第三种:  file:/D:/git/daotie/daotie/target/classes/
         URL xmlpath = this.getClass().getClassLoader().getResource("");
         System.out.println(xmlpath);

 
         // 第四种: D:\git\daotie\daotie
         System.out.println(System.getProperty("user.dir"));
         /*
          * 结果: C:\Documents and Settings\Administrator\workspace\projectName
          * 获取当前工程路径
          */

         // 第五种:  获取所有的类路径 包括jar包的路径
         System.out.println(System.getProperty("java.class.path"));

     }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
解析SVN路径下的.java文件并获取改动方法,您可以使用JavaSVN库和JavaParser库。 JavaSVN库是一个Java库,用于访问Subversion存储库。您可以使用这个库来检索文件历史记录和版本信息。 JavaParser库是一个Java库,用于解析和分析Java源代码。您可以使用这个库来解析.java文件并获取文件中的方法。 以下是一个简单的示例代码,展示如何使用这两个库来实现这个任务: ```java import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.IOUtils; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.diff.Edit; import org.eclipse.jgit.diff.EditList; import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.AbstractTreeIterator; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.TreeWalk; import com.github.javaparser.JavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.stmt.Statement; public class SVNJavaParser { public static void main(String[] args) throws Exception { String repoUrl = "svn://example.com/svn/myproject"; String username = "myusername"; String password = "mypassword"; String filePath = "/src/main/java/com/example/MyClass.java"; String commitId = "12345"; Repository repository = SVNUtils.getRepository(repoUrl, username, password); RevWalk revWalk = new RevWalk(repository); ObjectId objectId = repository.resolve(commitId); RevCommit commit = revWalk.parseCommit(objectId); AbstractTreeIterator oldTreeParser = SVNUtils.prepareTreeParser(repository, commit.getParent(0)); AbstractTreeIterator newTreeParser = SVNUtils.prepareTreeParser(repository, commit); List<DiffEntry> diffs = new Git(repository).diff() .setOldTree(oldTreeParser) .setNewTree(newTreeParser) .call(); for (DiffEntry entry : diffs) { if (entry.getNewPath().equals(filePath)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(outputStream); formatter.setRepository(repository); formatter.format(entry); String diffString = outputStream.toString(); RawText oldRawText = new RawText(diffString.split("@@")[0]); RawText newRawText = new RawText(diffString.split("@@")[2]); EditList editList = new EditList(); editList.addAll(new EditList(oldRawText, newRawText).getEdits()); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(commit.getTree()); treeWalk.setRecursive(true); while (treeWalk.next()) { if (filePath.equals(treeWalk.getPathString())) { CanonicalTreeParser oldTreeParser2 = new CanonicalTreeParser(); ObjectId oldTree = commit.getParent(0).getTree(); try (ObjectReader reader = repository.newObjectReader()) { oldTreeParser2.reset(reader, oldTree); } String oldContent = new String(repository.open(treeWalk.getObjectId(0)).getBytes()); CompilationUnit oldCompilationUnit = JavaParser.parse(oldContent); CompilationUnit newCompilationUnit = null; for (Edit edit : editList) { int beginLine = edit.getBeginA(); int endLine = edit.getEndA(); if (beginLine < 0 || endLine < 0) continue; List<String> oldLines = new ArrayList<>(); for (int i = beginLine; i < endLine; i++) { oldLines.add(oldRawText.getString(i)); } String oldMethodContent = String.join("\n", oldLines); MethodDeclaration oldMethodDeclaration = getMethodDeclaration(oldCompilationUnit, oldMethodContent); if (oldMethodDeclaration == null) continue; int newBeginLine = edit.getBeginB(); int newEndLine = edit.getEndB(); if (newBeginLine < 0 || newEndLine < 0) continue; List<String> newLines = new ArrayList<>(); for (int i = newBeginLine; i < newEndLine; i++) { newLines.add(newRawText.getString(i)); } String newMethodContent = String.join("\n", newLines); if (newCompilationUnit == null) { String newContent = new String(repository.open(treeWalk.getObjectId(0)).getBytes()); newCompilationUnit = JavaParser.parse(newContent); } MethodDeclaration newMethodDeclaration = getMethodDeclaration(newCompilationUnit, newMethodContent); if (newMethodDeclaration == null) continue; System.out.println("Old method: " + oldMethodDeclaration.getNameAsString()); System.out.println("New method: " + newMethodDeclaration.getNameAsString()); } } } } } } private static MethodDeclaration getMethodDeclaration(CompilationUnit compilationUnit, String methodContent) { List<MethodDeclaration> methods = compilationUnit.findAll(MethodDeclaration.class); for (MethodDeclaration method : methods) { String methodString = method.toString(); if (methodString.equals(methodContent)) { return method; } } return null; } } ``` 在这个示例代码中,我们首先使用JavaSVN库来检索指定提交的特定文件的差异。然后,我们使用JavaParser库来解析旧文件中的方法和新文件中的方法,并将它们进行比较,以获取改动方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sun897827804

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值