Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
reobf committed Sep 26, 2024
1 parent ed42649 commit 134ed3f
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,43 @@ buildscript {
e.printStackTrace()
throw new Exception("Failed setting environment variable <"+key+"> to <"+value+">", e);
} catch ( java.lang.NoSuchFieldException e) {
// we could not find theEnvironment
try{
// we could not find theEnvironment
Map<String, String> env = System.getenv();
java.util.Stream.of(Collections.class.getDeclaredClasses())
// obtain the declared classes of type \$UnmodifiableMap
.filter(c1 -> "java.util.Collections\$UnmodifiableMap".equals(c1.getName()))
.map(c1 -> {
try {
return c1.getDeclaredField("m");
} catch ( Exception e1) {
throw new Exception("Failed setting environment variable <"+key+"> to <"+value+"> when locating in-class memory map of environment", e1);
}
})
.forEach(field -> {
try {
boolean fieldAccessibility = field.isAccessible();
for(var c1:Collections.class.getDeclaredClasses()){
if("java.util.Collections\$UnmodifiableMap".equals(c1.getName())){
var field=c1.getDeclaredField("m");
boolean fieldAccessibility = field.isAccessible();
field.setAccessible(true);
// we obtain the environment
Map<String, String> map = (Map<String, String>) field.get(env);
if (value == null) {
Map<String, String> map = (Map<String, String>) field.get(env);
if (value == null) {
// remove if null
map.remove(key);
} else {
map.put(key, value);
}
// reset accessibility
field.setAccessible(fieldAccessibility);
} catch ( Exception e1) {
// This may happen if we keep backups of the environment before calling this method
// as the map that we kept as a backup may be picked up inside this block.
// So we simply skip this attempt and continue adjusting the other maps
// To avoid this one should always keep individual keys/value backups not the entire map
print("Attempted to modify source map: "+field.getDeclaringClass()+"#"+field.getName(), e1);
} catch ( Exception e1) {
throw new Exception("Failed setting environment variable <"+key+"> to <"+value+">. Unable to access field!", e1);
}
});
}

}





}



}catch (Exception e) {
e.printStackTrace()
throw new Exception("Failed");
}




}
print("Set environment variable <"+key+"> to <"+value+">. Sanity Check: "+System.getenv(key));


Expand Down

0 comments on commit 134ed3f

Please sign in to comment.