feat(05-reflect): Добавлено задание
This commit is contained in:
43
labs/05-reflect/app/build.gradle.kts
Normal file
43
labs/05-reflect/app/build.gradle.kts
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This generated file contains a sample Java application project to get you started.
|
||||
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.10.2/userguide/building_java_projects.html in the Gradle documentation.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
// Apply the application plugin to add support for building a CLI application in Java.
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// This dependency is used by the application.
|
||||
implementation(libs.guava)
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(23)
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass = "ru.mrqiz.reflect.App"
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
tasks.named<JavaExec>("run") {
|
||||
standardInput = System.`in`
|
||||
}
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
package ru.mrqiz.reflect;
|
||||
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.SimpleFormatter;
|
||||
|
||||
class Access {
|
||||
private static final Logger logger = Logger.getLogger(Access.class.getName());
|
||||
|
||||
private void privateMethod() {
|
||||
logger.log(Level.WARNING, "calling the private one");
|
||||
}
|
||||
|
||||
protected void protectedMethod() {
|
||||
logger.log(Level.WARNING, "calling the protected one");
|
||||
}
|
||||
|
||||
public void publicMethod() {
|
||||
logger.log(Level.INFO, "calling the public one");
|
||||
}
|
||||
|
||||
void defMethod() {
|
||||
logger.log(Level.INFO, "default called");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
53
labs/05-reflect/app/src/main/java/ru/mrqiz/reflect/App.java
Normal file
53
labs/05-reflect/app/src/main/java/ru/mrqiz/reflect/App.java
Normal file
@ -0,0 +1,53 @@
|
||||
package ru.mrqiz.reflect;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.SimpleFormatter;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystemException;
|
||||
import java.util.List;
|
||||
|
||||
public class App {
|
||||
private static final Logger logger = Logger.getLogger(App.class.getName());
|
||||
private static FileHandler fileHandler;
|
||||
|
||||
static {
|
||||
try {
|
||||
fileHandler = new FileHandler("/tmp/some.log", true);
|
||||
fileHandler.setFormatter(new SimpleFormatter());
|
||||
logger.addHandler(fileHandler);
|
||||
logger.setLevel(Level.ALL);
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "io is dead: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Class<?> clazz = Access.class;
|
||||
Object clazzObj = clazz.getDeclaredConstructor().newInstance();
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
// something is broken here
|
||||
// the default one still gets called..
|
||||
// TODO: can u fix that?
|
||||
if (!Modifier.isPublic(method.getModifiers()) || !method.isDefault()) {
|
||||
method.setAccessible(true);
|
||||
method.invoke(clazzObj);
|
||||
}
|
||||
}
|
||||
} catch (InstantiationException e) {
|
||||
logger.log(Level.SEVERE, "as i know there's no constructor: ", e);
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.log(Level.SEVERE, "got a rly bad modifier: ", e);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.log(Level.SEVERE, "somebody throws something: ", e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.log(Level.SEVERE, "idk anything about this method: ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user