.class工具类测试

master
Qin Dong Ming 2024-09-04 10:12:28 +08:00
parent a907cbf5b3
commit 0a4b120f1b
14 changed files with 611 additions and 4 deletions

BIN
HelloWorld.class 100644

Binary file not shown.

1
HelloWorld.java 100644
View File

@ -0,0 +1 @@
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }

View File

@ -26,6 +26,12 @@
<groupId>com.muyu</groupId> <groupId>com.muyu</groupId>
<artifactId>cloud-common-security</artifactId> <artifactId>cloud-common-security</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.17.4</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>--> <!-- <groupId>org.springframework.boot</groupId>-->

View File

@ -0,0 +1,56 @@
package com.muyu.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.muyu.common.core.domain.Result;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
public class OssUpload {
private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
private static final String bucketName = "011811";
public static Result<Object> uploadFiles(String content, String fileName) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String objectName = "build/rule/version/" + fileName + ".java";
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
ossClient.putObject(putObjectRequest);
return Result.success(null, "文件" + fileName + ".java" + "存储成功");
} catch (Exception e) {
e.printStackTrace();
return Result.error(null, "文件" + fileName + ".java" + "存储失败");
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}

View File

@ -0,0 +1,164 @@
//package com.muyu.utils;
//
//import lombok.extern.log4j.Log4j2;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.*;
//import java.time.LocalDateTime;
//import java.util.UUID;
//
///**
// * Oss服务调用
// */
//@Log4j2
//public class OssUtil {
//
// /**
// * Endpoint 存储对象概述 阿里云主账号AccessKeyaccessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述
// */
// private static String endPoint = "oss-cn-beijing.aliyuncs.com";
// private static String accessKeyId = " LTAI5tFNfBpzEbLkntksCgwC";
// private static String accessKeySecret = "XmYwUEea6BpgssdLKXMlpdzCI42Pk7";
// private static String accessPre = "https://qdm123.oss-cn-beijing.aliyuncs.com/";
//
// /**
// * bucket名称
// *
// * @return
// */
// private static String bucketName = "qdm123";
//
// private static Oss ossClient;
//
// static {
// ossClient = new OSSClientBuilder().build(
// endPoint,
// accessKeyId,
// accessKeySecret);
// log.info("oss服务连接成功");
// }
//
// /**
// * 默认路径上传本地文件
// *
// * @param filePath
// */
// public static String uploadFile(String filePath) {
// return uploadFileForBucket(bucketName, getOssFilePath(filePath), filePath);
// }
//
// /**
// * 默认路径上传multipartFile文件
// *
// * @param multipartFile
// */
// public static String uploadMultipartFile(MultipartFile multipartFile) {
// return uploadMultipartFile(bucketName, getOssFilePath(multipartFile.getOriginalFilename()), multipartFile);
// }
//
// /**
// * 上传 multipartFile 类型文件
// *
// * @param bucketName
// * @param ossPath
// * @param multipartFile
// */
// public static String uploadMultipartFile(String bucketName, String ossPath, MultipartFile multipartFile) {
// InputStream inputStream = null;
// try {
// inputStream = multipartFile.getInputStream();
// } catch (IOException e) {
// e.printStackTrace();
// }
// uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
// return accessPre + ossPath;
// }
//
// /**
// * 使用File上传PutObject上传文件 ** 程序默认使用次方法上传
// *
// * @param bucketName 实例名称
// * @param ossPath oss存储路径
// * @param filePath 本地文件路径
// */
// public static String uploadFileForBucket(String bucketName, String ossPath, String filePath) {
// // 创建PutObjectRequest对象。
// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath));
//
// // 上传
// ossClient.putObject(putObjectRequest);
// return accessPre + ossPath;
// }
//
// /**
// * 使用文件流上传到指定的bucket实例
// *
// * @param bucketName 实例名称
// * @param ossPath oss存储路径
// * @param filePath 本地文件路径
// */
// public static String uploadFileInputStreamForBucket(String bucketName, String ossPath, String filePath) {
//
// // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
// InputStream inputStream = null;
// try {
// inputStream = new FileInputStream(filePath);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
// uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
// return accessPre + ossPath;
// }
//
// public static void uploadFileInputStreamForBucket(String bucketName, String ossPath, InputStream inputStream) {
// ossClient.putObject(bucketName, ossPath, inputStream);
// }
//
// /**
// * 下载
// *
// * @param ossFilePath
// * @param filePath
// */
// public static void downloadFile(String ossFilePath, String filePath) {
// downloadFileForBucket(bucketName, ossFilePath, filePath);
// }
//
// /**
// * 下载
// *
// * @param bucketName 实例名称
// * @param ossFilePath oss存储路径
// * @param filePath 本地文件路径
// */
// public static void downloadFileForBucket(String bucketName, String ossFilePath, String filePath) {
// ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath));
// }
//
// /**
// * @return
// */
// public static String getOssDefaultPath() {
// LocalDateTime now = LocalDateTime.now();
// String url =
// now.getYear() + "/" +
// now.getMonth() + "/" +
// now.getDayOfMonth() + "/" +
// now.getHour() + "/" +
// now.getMinute() + "/";
// return url;
// }
//
// public static String getOssFilePath(String filePath) {
// String fileSuf = filePath.substring(filePath.indexOf(".") + 1);
// return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
// }
//
//}
//
//
//
//
//
//

View File

@ -44,6 +44,31 @@
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<!-- //创建测试工程,引入依赖-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.17.4</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
<!-- Mysql Connector --> <!-- Mysql Connector -->
<dependency> <dependency>
<groupId>com.mysql</groupId> <groupId>com.mysql</groupId>
@ -90,6 +115,7 @@
</dependency> </dependency>
<!-- &lt;!&ndash; XllJob定时任务 &ndash;&gt;--> <!-- &lt;!&ndash; XllJob定时任务 &ndash;&gt;-->
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>com.muyu</groupId>--> <!-- <groupId>com.muyu</groupId>-->

View File

@ -210,8 +210,7 @@ public class EngIneController extends BaseController {
*/ */
@PostMapping("/generate") @PostMapping("/generate")
public EngineVersion generate(@RequestBody EngineVersion engineVersion) { public EngineVersion generate(@RequestBody EngineVersion engineVersion) {
EngineVersion byId1 = engineVersionService.getById(engineVersion.getId()); EngineMaintenance byId = engIneService.selectById(engineVersion.getId());
EngineMaintenance byId = engIneService.selectById(byId1.getId());
byId.setName("generate" + "_" + byId.getEngineCode() + "_" + engineVersion.getVersionCode()); byId.setName("generate" + "_" + byId.getEngineCode() + "_" + engineVersion.getVersionCode());
engineVersion.setRuleContent(GenerateConstant.generateConstant(byId, engineVersion)); engineVersion.setRuleContent(GenerateConstant.generateConstant(byId, engineVersion));
return engineVersion; return engineVersion;

View File

@ -0,0 +1,55 @@
package com.muyu.input;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.FileInputStream;
import java.io.InputStream;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以华东1杭州为例其它Region请按实际情况填写。
String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填写Bucket名称例如examplebucket。
String bucketName = "011811";
// 填写Object完整路径完整路径中不能包含Bucket名称例如exampledir/exampleobject.txt。
String objectName = "localpath\\examplefile.txt";
// 填写本地文件的完整路径例如D:\\localpath\\examplefile.txt。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
String filePath= "C:\\Users\\qdm\\Desktop\\Project\\CLOUD-ETL-UI-ENGINE\\cloud-etl-engine\\cloud-etl-server\\target\\";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
InputStream inputStream = new FileInputStream(filePath);
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
// 创建PutObject请求。
PutObjectResult result = ossClient.putObject(putObjectRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}

View File

@ -0,0 +1,53 @@
package com.muyu.javacomplier;
import lombok.extern.log4j.Log4j2;
import javax.tools.*;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
@Log4j2
public class JavaCompilerDemo {
public static void main(String[] args) {
String sourceCode = "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello, world!\"); } }";
String fileName = "HelloWorld.java";
try {
// 将源代码写入文件
writeToFile(fileName, sourceCode);
// 获取Java编译器实例
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// 设置编译参数
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(fileName));
// 执行编译
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);
log.info("数据是:{}"+task);
boolean success = task.call();
// 处理编译结果
if (success) {
System.out.println("编译成功");
} else {
System.out.println("编译失败");
diagnostics.getDiagnostics().forEach(d -> System.out.format("Line %d, Column %d: %s%n", d.getLineNumber(), d.getColumnNumber(), d.getMessage(null)));
}
// 释放资源
fileManager.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void writeToFile(String fileName, String sourceCode) throws IOException {
try (FileWriter writer = new FileWriter(fileName)) {
writer.write(sourceCode);
}
}
}

View File

@ -0,0 +1,70 @@
package com.itranswarp.compiler;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
/**
* In-memory compile Java source code as String.
*
* @author michael
*/
public class JavaStringCompiler {
JavaCompiler compiler;
StandardJavaFileManager stdManager;
public JavaStringCompiler() {
this.compiler = ToolProvider.getSystemJavaCompiler();
this.stdManager = compiler.getStandardFileManager(null, null, null);
}
/**
* Compile a Java source file in memory.
*
* @param fileName
* Java file name, e.g. "Test.java"
* @param source
* The source code as String.
* @return The compiled results as Map that contains class name as key,
* class binary as value.
* @throws IOException
* If compile error.
*/
public Map<String, byte[]> compile(String fileName, String source) throws IOException {
try (MemoryJavaFileManager manager = new MemoryJavaFileManager(stdManager)) {
JavaFileObject javaFileObject = manager.makeStringSource(fileName, source);
CompilationTask task = compiler.getTask(null, manager, null, null, null, Arrays.asList(javaFileObject));
Boolean result = task.call();
if (result == null || !result.booleanValue()) {
throw new RuntimeException("Compilation failed.");
}
return manager.getClassBytes();
}
}
/**
* Load class from compiled classes.
*
* @param name
* Full class name.
* @param classBytes
* Compiled results as a Map.
* @return The Class instance.
* @throws ClassNotFoundException
* If class not found.
* @throws IOException
* If load error.
*/
public Class<?> loadClass(String name, Map<String, byte[]> classBytes) throws ClassNotFoundException, IOException {
try (MemoryClassLoader classLoader = new MemoryClassLoader(classBytes)) {
return classLoader.loadClass(name);
}
}
}

View File

@ -0,0 +1,33 @@
package com.muyu.javacomplier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;
/**
* Load class from byte[] which is compiled in memory.
*
* @author michael
*/
class MemoryClassLoader extends URLClassLoader {
// class name to class bytes:
Map<String, byte[]> classBytes = new HashMap<String, byte[]>();
public MemoryClassLoader(Map<String, byte[]> classBytes) {
super(new URL[0], MemoryClassLoader.class.getClassLoader());
this.classBytes.putAll(classBytes);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] buf = classBytes.get(name);
if (buf == null) {
return super.findClass(name);
}
classBytes.remove(name);
return defineClass(name, buf, 0, buf.length);
}
}

View File

@ -0,0 +1,96 @@
package com.itranswarp.compiler;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
/**
* In-memory java file manager.
*
* @author michael
*/
class MemoryJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
// compiled classes in bytes:
final Map<String, byte[]> classBytes = new HashMap<String, byte[]>();
MemoryJavaFileManager(JavaFileManager fileManager) {
super(fileManager);
}
public Map<String, byte[]> getClassBytes() {
return new HashMap<String, byte[]>(this.classBytes);
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
classBytes.clear();
}
@Override
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, Kind kind,
FileObject sibling) throws IOException {
if (kind == Kind.CLASS) {
return new MemoryOutputJavaFileObject(className);
} else {
return super.getJavaFileForOutput(location, className, kind, sibling);
}
}
JavaFileObject makeStringSource(String name, String code) {
return new MemoryInputJavaFileObject(name, code);
}
static class MemoryInputJavaFileObject extends SimpleJavaFileObject {
final String code;
MemoryInputJavaFileObject(String name, String code) {
super(URI.create("string:///" + name), Kind.SOURCE);
this.code = code;
}
@Override
public CharBuffer getCharContent(boolean ignoreEncodingErrors) {
return CharBuffer.wrap(code);
}
}
class MemoryOutputJavaFileObject extends SimpleJavaFileObject {
final String name;
MemoryOutputJavaFileObject(String name) {
super(URI.create("string:///" + name), Kind.CLASS);
this.name = name;
}
@Override
public OutputStream openOutputStream() {
return new FilterOutputStream(new ByteArrayOutputStream()) {
@Override
public void close() throws IOException {
out.close();
ByteArrayOutputStream bos = (ByteArrayOutputStream) out;
classBytes.put(name, bos.toByteArray());
}
};
}
}
}

View File

@ -0,0 +1,48 @@
package com.muyu.cloud.rule.utils.upload;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.muyu.common.core.domain.Result;
import java.io.File;
public class OssDownload {
// 阿里云 endpoint
private static final String endpoint = "oss-cn-wulanchabu.aliyuncs.com";
// 阿里云 accessKeyId
private static final String accessKeyId = "LTAI5t8mk6aWqDq5w6WA49et";
// 阿里云 accessKeySecret  
private static final String accessKeySecret = "SuLwzfEOruGOs1RSCq4vA1Bcf1849R";
// bucket
private static final String bucketName = "oss-1209";
// OSS文件路径
private static final String filePath = "build/rule/version/";
public static Result<Object> streamingDownload(String fileName) {
String source = null;
// 创建 OSSClient 实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 判断 bucket 是否存在
Boolean flag_bucket = ossClient.doesBucketExist(bucketName);
if (!flag_bucket) {
System.out.println("bucket不存在");
return Result.error("bucket不存在");
}
String objectName = fileName + ".java";
Boolean flag_file = ossClient.doesObjectExist(bucketName, filePath+objectName);
if (!flag_file) {
System.out.println("预下载文件不存在");
return Result.error("预下载文件不存在");
}
// 本地文件下载路径
String localPath = "home/"+objectName;
ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, filePath + objectName), new File(localPath));
System.out.println(object);
ossClient.shutdown();
return Result.success(source);
}
}

View File

@ -22,8 +22,8 @@
<module>cloud-etl-server</module> <module>cloud-etl-server</module>
</modules> </modules>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
<maven.compiler.parameters>true</maven.compiler.parameters> <maven.compiler.parameters>true</maven.compiler.parameters>
</properties> </properties>
</project> </project>