Skip to content

Commit

Permalink
Making it compatible with JDK 21
Browse files Browse the repository at this point in the history
  • Loading branch information
rgudwin committed Nov 21, 2023
1 parent 4c71757 commit bd01068
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 93 deletions.
19 changes: 11 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ group = 'com.github.CST-Group'
description = "CST"

java {
//sourceCompatibility = JavaVersion.VERSION_11
//targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Expand All @@ -33,14 +35,15 @@ dependencies {
api 'net.openhft:compiler:2.3.0' // Used by representation/owrl/CodeBuilder.java
api group: 'org.json', name: 'json', version: '20230227'
api 'com.google.inject:guice:7.0.0'
api group: 'com.github.sdarg', name: 'opt4j', version: '3.3.0' // Used in GLAS
//api group: 'com.github.sdarg', name: 'opt4j-core', version: '3.3.0' // Used in GLAS
//api group: 'com.github.sdarg', name: 'opt4j-optimizers', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-viewer', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-benchmarks', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-operators', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-satdecoding', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-tutorial', version: '3.3.0'
//api 'com.github.sdarg:opt4j:3.3.0' // Used in GLAS
//api 'com.github.sdarg:opt4j:4780f4e' // Used in GLAS
api group: 'org.opt4j', name: 'opt4j-core', version: '3.1.4' // Used in GLAS
api group: 'org.opt4j', name: 'opt4j-optimizers', version: '3.1.4'
api group: 'org.opt4j', name: 'opt4j-viewer', version: '3.1.4'
api group: 'org.opt4j', name: 'opt4j-benchmarks', version: '3.1.4'
api group: 'org.opt4j', name: 'opt4j-operators', version: '3.1.4'
api group: 'org.opt4j', name: 'opt4j-satdecoding', version: '3.1.4'
api group: 'org.opt4j', name: 'opt4j-tutorial', version: '3.1.4'
api 'org.antlr:antlr4-runtime:4.5.3' // Used in OwrlBaseListener
api 'com.github.masecla22:java-express:0.2.2'
//api 'io.vacco.murmux:murmux:2.2.2'
Expand Down
161 changes: 76 additions & 85 deletions src/main/java/br/unicamp/cst/representation/idea/Idea.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import br.unicamp.cst.support.ToString;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
Expand All @@ -31,6 +30,7 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.DefaultMutableTreeNode;


/**
Expand Down Expand Up @@ -1089,19 +1089,21 @@ private Object checkIfObjectHasSetGet(Object bean, String propertyName) {
// access a no-arg method through reflection
// following bean naming conventions
try {
Method m = bean.getClass().getMethod(
"get"
+propertyName.substring(0,1).toUpperCase()
+propertyName.substring(1)
, null);
return m.invoke(bean);
Class c = bean.getClass();
String methodName = "get"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1);
Method m = c.getMethod(methodName,(Class[])null);
Object o = m.invoke(bean);
return o;
}
catch (Exception e) {
try {
Method m = bean.getClass().getMethod(propertyName, null);
return m.invoke(bean);
Class c = bean.getClass();
Method m = c.getMethod(propertyName,(Class[])null);
Object o = m.invoke(bean);
return o;
}
catch (Exception e2) {
//System.out.println("I got a "+e.getClass().getName()+" Exception in class Idea: "+e.getMessage()+"-->"+e.getLocalizedMessage());
// (gulp) -- swallow exception and move on
}
}
Expand All @@ -1111,22 +1113,30 @@ private Object checkIfObjectHasSetGet(Object bean, String propertyName) {
private void insertAlreadyExistObject(String fullname, String fname, Field field, Object fo, Idea ao) {
String ideaname = getFullName()+"."+ToString.getSimpleName(fullname)+"."+fname;
Idea fi = createIdea(ideaname,"",2);
if (!Modifier.isStatic(field.getModifiers())) {
for (Map.Entry<String,Idea> entry : repo.entrySet()) {
String key = entry.getKey();
Idea v = entry.getValue();
if (ToString.getSimpleName(ideaname).equals(ToString.getSimpleName(key))) {
System.out.println("The Idea "+ideaname+" is already in the repository");
}
}
System.out.println(fo.getClass().getCanonicalName());
// if (!Modifier.isStatic(field.getModifiers())) {
// for (Map.Entry<String,Idea> entry : repo.entrySet()) {
// String key = entry.getKey();
// Idea v = entry.getValue();
// if (ToString.getSimpleName(ideaname).equals(ToString.getSimpleName(key))) {
// System.out.println("The Idea "+ideaname+" is already in the repository");
// }
// }
Idea alternative = repo.get(ideaname);
if (alternative != null) System.out.println("Ah ... I already found "+ideaname);
else System.out.println("Strange ... it seems that "+ideaname+" is already in the repo but I can't find it");
}
if (alternative != null) System.out.println(ideaname+" was already referenced as an earlier idea ... reusing it");
else System.out.println(ideaname+" is already referenced with a different name ... inserted as link !");
// }
ao.add(fi);
}

private boolean trySetAccessibleTrue(Field f) {
try {
f.setAccessible(true);
return(true);
} catch(Exception e) {
return(false);
}
}

/**
* This method uses a Java Object as template, creating an Idea with a similar structure.
* This new Idea is added to the current Idea
Expand Down Expand Up @@ -1263,71 +1273,52 @@ else if (obj instanceof Idea) {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
String fname = field.getName();
int modifiers = field.getModifiers();
//if (Modifier.isProtected(modifiers)) System.out.println(ao.getName()+"."+fname+": PROTECTED");
//if (Modifier.isPrivate(modifiers)) System.out.println(ao.getName()+"."+fname+": PRIVATE");
//if (Modifier.isStatic(modifiers)) System.out.println(ao.getName()+"."+fname+": STATIC");
//if (Modifier.isVolatile(modifiers)) System.out.println(ao.getName()+"."+fname+": VOLATILE");
try {
Object toget = null;
if (!((modifiers & Modifier.STATIC) > 0)) toget = obj;
//if (!field.canAccess(toget)) {
//System.out.println("I can't access field "+field.getName()+" of object "+getFullName()+" of class "+obj.getClass().getCanonicalName());
//System.out.println(field.toString());
//}
boolean trysetaccessible = field.trySetAccessible();
if (!trysetaccessible) {// This is the case the object is inaccessible
//System.out.println("I tried to make the field "+fname+" accessible, but didn't succeeded");
Object fo = checkIfObjectHasSetGet(obj, fname);
if (fo != null) {
//System.out.println("Hey .. it worked ... "+fname+" is a JavaBean");
if (!already_exists(fo)) {
ao.addObject(fo,fname,false);
}
else insertAlreadyExistObject(fullname, fname, field, fo, ao);
}
else {
//System.out.println("OK .. giving up ... "+fname+" is not a JavaBean ... I will not include it in the Idea");
if (!already_exists(fo)) {
ao.addObject(fo,fname,false);
}
else insertAlreadyExistObject(fullname, fname, field, fo, ao);
}
}
else {// This is the case the object is accessible
Object fo=null;
try {
if (Modifier.isStatic(modifiers)) fo = field.get(null);
else fo = field.get(obj);
} catch (Exception e) {
System.out.println(e.getClass().getCanonicalName());
e.printStackTrace();}
if (!already_exists(fo)) {
ao.addObject(fo,fname,false);
}
else {// This the object already exists in listtoavoidloops
String ideaname = getFullName()+"."+ToString.getSimpleName(fullname)+"."+fname;
Idea fi = createIdea(ideaname,"",2);
Idea alternative2 = null;
if (!Modifier.isStatic(field.getModifiers())) {
for (Map.Entry<String,Idea> entry : repo.entrySet()) {
String key = entry.getKey();
Idea v = entry.getValue();
if (ToString.getSimpleName(ideaname).equals(ToString.getSimpleName(key))) {
System.out.println("The Idea "+ideaname+" is already in the repository");
}
}
System.out.println(fo.getClass().getCanonicalName());
Idea alternative = repo.get(ideaname);
if (alternative != null) System.out.println("Ah ... I already found "+ideaname);
else System.out.println("Strange ... it seems that "+ideaname+" is already in the repo but I can't find it");
}
ao.add(fi);
}
}} catch (InaccessibleObjectException ioe) {
System.out.println("I got an InaccessibleObjectException with "+ioe.getMessage());
}
catch (Exception e) {
boolean trysetaccessible = trySetAccessibleTrue(field);
Object fo;
if (trysetaccessible) { // if object is accessible, try to get it
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers)) fo = field.get(null);
else fo = field.get(obj);
}
else { // if it is inaccessible, check if it is a bean, before giving up
fo = checkIfObjectHasSetGet(obj, fname);
}
if (!already_exists(fo)) {
ao.addObject(fo,fname,false);
}
else insertAlreadyExistObject(fullname, fname, field, fo, ao);



// if (!trysetaccessible) {// This is the case the object is inaccessible
// Object fo = checkIfObjectHasSetGet(obj, fname);
// if (fo != null) {
// if (!already_exists(fo)) {
// ao.addObject(fo,fname,false);
// }
// else insertAlreadyExistObject(fullname, fname, field, fo, ao);
// }
// else {
// if (!already_exists(fo)) {
// ao.addObject(fo,fname,false);
// }
// else insertAlreadyExistObject(fullname, fname, field, fo, ao);
// }
// }
// else {// This is the case the object is accessible
// Object fo=null;
// int modifiers = field.getModifiers();
// if (Modifier.isStatic(modifiers)) fo = field.get(null);
// else fo = field.get(obj);
// if (!already_exists(fo)) {
// ao.addObject(fo,fname,false);
// }
// else {// This the object already exists in listtoavoidloops
// insertAlreadyExistObject(fullname, fname, field, fo, ao);
// }
// }
} catch (Exception e) {
System.out.println("I got a "+e.getClass().getName()+" Exception in field "+fname+" in class Idea: "+e.getMessage()+"-->"+e.getLocalizedMessage());
}
}
Expand Down

0 comments on commit bd01068

Please sign in to comment.