فهرست منبع

tatalny shit i workaround na multiline strings (przynajmniej na potrzeby UnitTestów może się przydać...)

Ranides Atterwim 13 سال پیش
والد
کامیت
498092a82c
1فایلهای تغییر یافته به همراه51 افزوده شده و 0 حذف شده
  1. 51 0
      src/main/java/net/ranides/assira/text/SelfReader.java

+ 51 - 0
src/main/java/net/ranides/assira/text/SelfReader.java

@@ -0,0 +1,51 @@
+/*
+ * I-BS.pl PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ * Copyright (c) 2012, I-BS.pl and/or its affiliates. All rights reserved.
+ */
+package net.ranides.assira.text;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import net.ranides.assira.trace.StackInspector;
+
+/**
+ *
+ * @author ranides
+ */
+public final class SelfReader {
+    
+    private SelfReader() {
+        // utility class
+    }
+
+    public static String S() {
+        StackTraceElement frame = StackInspector.frame(StackInspector.CALLER);
+        String file = frame.getClassName().replace('.', '/') + ".java";
+        String source = load(file);
+        int offset = 0;
+        for (int i = 1, n = frame.getLineNumber(); i < n; i++) {
+            offset = source.indexOf('\n', offset) + 1;
+        }
+        return source.substring(source.indexOf("/*", offset) + 2, source.indexOf("*/", offset));
+    }
+    
+    private static String load(String file) {
+        InputStream istream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
+        if(istream != null) {
+            return StringLoader.fromStream(istream);
+        }
+        if(new File(file).exists() ) {
+            return StringLoader.fromFile(file);
+        }
+        if(new File("src/test/java/" + file).exists() ) {
+            return StringLoader.fromFile("src/test/java/" + file);
+        }
+        if(new File("src/test/main/" + file).exists() ) {
+            return StringLoader.fromFile("src/test/main/" + file);
+        }
+        throw new RuntimeException(new IOException("unknown location of file: " + file));
+    }
+
+    
+}