Java Code Examples for java.lang.Class#forName()

The following examples show how to use java.lang.Class#forName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CreateSQLiteService.java    From squirrelAI with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
        try {

            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:SquirrelAI.db");
            statement = connection.createStatement();
//            String sql = "CREATE TABLE message(date varchar(255),name varchar(255),message varchar(512));";
            //UserName、NickName、RemarkName、Province、City
            String sql = "CREATE TABLE user(username varchar(255),nickname varchar(255),remarkname varchar(512),province varchar(512),city varchar(512));";

            statement.executeUpdate(sql);
            statement.close();
            connection.close();
            System.out.println("数据库创建成功!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 2
Source File: GDelegateCenter.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
MomentViewModelDelegate(Context context) {
  try {
    targetClass = Class.forName("com.grouter.demo.delegate.MomentViewModel");
    Constructor<?> constructors = targetClass.getConstructor(Context.class);
    target = constructors.newInstance(context);
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 3
Source File: GDelegateCenter.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
UserViewModelDelegate() {
  try {
    targetClass = Class.forName("com.grouter.demo.delegate.UserViewModel");
    target = targetClass.newInstance();
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 4
Source File: GDelegateCenter.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
Account2ServiceDelegate() {
  try {
    targetClass = Class.forName("com.grouter.demo.Account2Service");
    target = targetClass.newInstance();
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 5
Source File: GDelegateCenter.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
Account2ServiceDelegate(Context context, int uid, int[] uids, Map userMap, List users, User user, String[] names) {
  try {
    targetClass = Class.forName("com.grouter.demo.Account2Service");
    Constructor<?> constructors = targetClass.getConstructor(Context.class,int.class,int[].class,Map.class,List.class,User.class,String[].class);
    target = constructors.newInstance(context,uid,uids,userMap,users,user,names);
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 6
Source File: GDelegateCenter.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
Account2ServiceDelegate(StringBuilder stringBuilder) {
  try {
    targetClass = Class.forName("com.grouter.demo.Account2Service");
    Constructor<?> constructors = targetClass.getConstructor(StringBuilder.class);
    target = constructors.newInstance(stringBuilder);
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 7
Source File: isArray.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
public void checkClass(TestHarness harness, String className, boolean isArray)
{
    try
    {
        Class c = Class.forName(className);
        harness.check(c.isArray() == isArray);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        harness.check(false);
    }
}
 
Example 8
Source File: BagFactory.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * Get a reference to the singleton factory.
 * @return BagFactory
 */
public static BagFactory getInstance() {
    if (gSelf == null) {
        String factoryName =
            System.getProperty("pig.data.bag.factory.name");
        String factoryJar =
            System.getProperty("pig.data.bag.factory.jar");
        if (factoryName != null && factoryJar != null) {
            try {
                URL[] urls = new URL[1];
                urls[0] = new URL(factoryJar);
                ClassLoader loader = new URLClassLoader(urls,
                    BagFactory.class.getClassLoader());
                Class c = Class.forName(factoryName, true, loader);
                Object o = c.newInstance();
                if (!(o instanceof BagFactory)) {
                    throw new RuntimeException("Provided factory " +
                        factoryName + " does not extend BagFactory!");
                }
                gSelf = (BagFactory)o;
            } catch (Exception e) {
                if (e instanceof RuntimeException) {
                    // We just threw this
                    RuntimeException re = (RuntimeException)e;
                    throw re;
                }
                throw new RuntimeException("Unable to instantiate "
                    + "bag factory " + factoryName, e);
            }
        } else {
            gSelf = new DefaultBagFactory();
        }
    }
    return gSelf;
}
 
Example 9
Source File: AnnotationProxyMaker.java    From Mindustry with GNU General Public License v3.0 4 votes vote down vote up
private Map<MethodSymbol, Attribute> getAllValues(){
    LinkedHashMap map = new LinkedHashMap();
    ClassSymbol cl = (ClassSymbol)this.anno.type.tsym;

    //try to use Java 8 API for this if possible
    try{
        Class entryClass = Class.forName("com.sun.tools.javac.code.Scope$Entry");
        Object members = cl.members();
        Field field = members.getClass().getField("elems");
        Object elems = field.get(members);
        Field siblingField = entryClass.getField("sibling");
        Field symField = entryClass.getField("sym");
        for(Object currEntry = elems; currEntry != null; currEntry = siblingField.get(currEntry)){
            handleSymbol((Symbol)symField.get(currEntry), map);
        }

    }catch(Throwable e){
        //otherwise try other API

        try{
            Class lookupClass = Class.forName("com.sun.tools.javac.code.Scope$LookupKind");
            Field nonRecField = lookupClass.getField("NON_RECURSIVE");
            Object nonRec = nonRecField.get(null);
            Scope scope = cl.members();
            Method getSyms = scope.getClass().getMethod("getSymbols", lookupClass);
            Iterable<Symbol> it = (Iterable<Symbol>)getSyms.invoke(scope, nonRec);
            Iterator<Symbol> i = it.iterator();
            while(i.hasNext()){
                handleSymbol(i.next(), map);
            }

        }catch(Throwable death){
            //I tried
            throw new RuntimeException(death);
        }
    }

    for(Pair var7 : this.anno.values){
        map.put(var7.fst, var7.snd);
    }

    return map;
}
 
Example 10
Source File: MoveFactory.java    From eb-java-scorekeep with Apache License 2.0 4 votes vote down vote up
public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException {
  String moveId = Identifiers.random();
  String stateId = Identifiers.random();
  Move move = new Move().setId(moveId)
                        .setSession(sessionId)
                        .setGame(gameId)
                        .setUser(userId)
                        .setMove(moveText);
  String newStateText = "";
  // load game state
  Game game = gameController.getGame(sessionId, gameId);
  List<String> states = game.getStates();
  State oldState = stateController.getState(sessionId, gameId, states.get(states.size() - 1));
  Set<String> oldTurn = oldState.getTurn();
  // check turn 
  // if ( oldTurn.contains(userId) {}
  // load game rules
  //   rules = rulesFactory.getRules(rulesId)
  // apply move
  //   String stateText = rules.move(oldState, move)
  Set<String> newTurn = game.getUsers();
  if (newTurn.size() != 1) {
    newTurn.remove(userId);
  }
  String rulesName = game.getRules();
  if ( !rulesName.matches("[a-zA-Z]{1,16}") ) {
    throw new RulesException(rulesName);
  }
  try {
    Class<?> rules = Class.forName("scorekeep." + rulesName);
    Method moveMethod = rules.getMethod("move", String.class, String.class);
    newStateText = (String) moveMethod.invoke(null, oldState.getState(), moveText);
  } catch ( ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RulesException(rulesName); }
  // save new game state
  State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
  // send notification on game end
  if ( newStateText.startsWith("A") || newStateText.startsWith("B")) {
    Thread comm = new Thread() {
      public void run() {
        Sns.sendNotification("Scorekeep game completed", "Winner: " + userId);
      }
    };
    comm.start();
  }
  // register state and move id to game
  gameController.setGameMove(sessionId, gameId, moveId);
  gameController.setGameState(sessionId, gameId, stateId);
  moveModel.saveMove(move);
  stateModel.saveState(newState);
  return move;
}